# Sol

$$设x，y且gcd(x, y)=1$$若使$$\frac{x}{y}$$$$k$$进制小数是纯循环小数

$$x，y互质$$那么同时乘上x的逆元则$$k^L\equiv1(mod\ y)$$

$$y$$$$a*y+1$$互质，辗转相除法可证

$=\sum_{i=1}^{m}[gcd(i, k)==1]\sum_{j=1}^{n}[gcd(i, j)==1]$

$=\sum_{i=1}^{m}[gcd(i, k)==1]\sum_{j=1}^{n}\sum_{d|i,d|j}\mu(d)$

$=\sum_{i=1}^{m}[gcd(i, k)==1]\sum_{d|i}^n\mu(d)\lfloor\frac{n}{d}\rfloor$

$=\sum_{d=1}^{n}\mu(d)\lfloor\frac{n}{d}\rfloor\sum_{d|i}[gcd(i, k)==1]$

$=\sum_{d=1}^{n}\mu(d)\lfloor\frac{n}{d}\rfloor[gcd(d, k)==1]\sum_{i=1}^{\lfloor\frac{m}{d}\rfloor}[gcd(i, k)==1]$

$$x=\lfloor\frac{m}{d}\rfloor$$

$O(n)$84分常数很大

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e7 + 1);

RG ll x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}

int N, num, prime[_], mu[_], k, f[2010];
bool isprime[_];

IL int Gcd(RG int x, RG int y){  return !y ? x : Gcd(y, x % y);  }

IL void Prepare(){
isprime[1] = 1; mu[1] = 1;
for(RG int i = 2; i < N; ++i){
if(!isprime[i]) prime[++num] = i, mu[i] = -1;
for(RG int j = 1; j <= num && i * prime[j] < N; ++j){
isprime[i * prime[j]] = 1;
if(i % prime[j]) mu[i * prime[j]] = -mu[i];
else{  mu[i * prime[j]] = 0; break;  }
}
}
for(RG int i = 1; i <= k; ++i) f[i] = f[i - 1] + (Gcd(i, k) == 1);
}

int main(RG int argc, RG char* argv[]){
N = min(m + 1, _); Prepare(); RG ll ans = 0;
for(RG int i = 1; i <= n; ++i){
if(Gcd(i, k) != 1) continue;
RG ll x = m / i, F = f[x % k] + 1LL * (x / k) * f[k];
ans += 1LL * mu[i] * (n / i) * F;
}
printf("%lld\n", ans);
return 0;
}



$\sum_{d=1}^{n}\mu(d)\lfloor\frac{n}{d}\rfloor[gcd(d, k)==1]\sum_{i=1}^{\lfloor\frac{m}{d}\rfloor}[gcd(i, k)==1]$

$\sum_{d=1}^{n}\mu(d)\sum_{i|d, i|k}\mu(i)$

$=\sum_{i|k}^{k}\mu(i)\sum_{i|d}^{n}\mu(d)$

$=\sum_{i|k}^{k}\mu(i)\sum_{j=1}^{\lfloor\frac{n}{i}\rfloor}\mu(i*j)$

$=\sum_{i|k}^{k}\mu(i)\sum_{j=1}^{\lfloor\frac{n}{i}\rfloor}\mu(i)\mu(j)[gcd(i,j)==1]$

$=\sum_{i|k}^{k}\mu^2(i)\sum_{j=1}^{\lfloor\frac{n}{i}\rfloor}\mu(j)[gcd(i,j)==1]$

$=\sum_{i|k}^{k}\mu^2(i)G(\lfloor\frac{n}{i}\rfloor,i)$

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(4e6 + 1);

RG ll x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}

int N, num, prime[_], mu[_], k, f[2010], smu[_];
map < pair <int, int> , ll > G;
bool isprime[_];

IL int Gcd(RG int x, RG int y){  return !y ? x : Gcd(y, x % y);  }

IL void Sieve(){
isprime[1] = 1; smu[1] = mu[1] = 1;
for(RG int i = 2; i < N; ++i){
if(!isprime[i]) prime[++num] = i, mu[i] = -1;
for(RG int j = 1; j <= num && i * prime[j] < N; ++j){
isprime[i * prime[j]] = 1;
if(i % prime[j]) mu[i * prime[j]] = -mu[i];
else{  mu[i * prime[j]] = 0; break;  }
}
smu[i] = smu[i - 1] + mu[i];
}
for(RG int i = 1; i <= k; ++i) f[i] = f[i - 1] + (Gcd(i, k) == 1);
}

IL ll Du_Sieve(RG int n, RG int m){
if(!n) return 0;
if(m == 1 && n < _) return smu[n];
if(G[make_pair(n, m)]) return G[make_pair(n, m)];
RG ll ans = 0;
if(m == 1){
ans = 1;
for(RG int i = 2, j; i <= n; i = j + 1){
j = n / (n / i);
ans -= Du_Sieve(n / i, 1) * (j - i + 1);
}
}
else{
for(RG int i = 1; i * i <= m; ++i){
if(m % i) continue;
if(mu[i] != 0) ans += Du_Sieve(n / i, i);
if(i * i != m && mu[m / i] != 0) ans += Du_Sieve(n / (m / i), m / i);
}
}
return G[make_pair(n, m)] = ans;
}

int main(RG int argc, RG char* argv[]){
N = min(m + 1, _); Sieve(); RG ll ans = 0, nxt, lst = 0;
for(RG int i = 1, j; i <= n; i = j + 1){
j = n / (n / i); if(m / i) j = min(j, m / (m / i));
RG ll x = m / i, F = f[x % k] + 1LL * (x / k) * f[k];
nxt = Du_Sieve(j, k);
ans += 1LL * (nxt - lst) * (n / i) * F;
lst = nxt;
}
printf("%lld\n", ans);
return 0;
}



## 柿子比较多，如有错误请指正

posted @ 2018-01-17 20:38  Cyhlnj  阅读(291)  评论(0编辑  收藏  举报