# *LOJ#2085. 「NOI2016」循环之美

$n \leq 1e9,m \leq 1e9,k \leq 2000$，求$k$进制下$\frac{x}{y}$有多少种不同的纯循环数取值，$1 \leq x \leq n,1 \leq y \leq m$。纯循环数是指小数点后直接就开始循环，整数也算。

$\\ \sum_{x=1}^{n}\sum_{y=1}^m[(x,y)=1][(k,y)=1]$
$\\ =\sum_{y=1}^{m}[(k,y)=1]\sum_{x=1}^{n}[(x,y)=1]$
$\\ =\sum_{y=1}^{m}[(k,y)=1]\sum_{x=1}^{n}\sum_{d|x,d|y}\mu(d)$
$\\ =\sum_{y=1}^{m}[(k,y)=1]\sum_{d|y}\mu(d)\left \lfloor \frac{n}{d} \right \rfloor$
$\\ =\sum_{d=1}^{m}\mu(d)\left \lfloor \frac{n}{d} \right \rfloor\sum_{d|y,y \leq m}[(k,y)=1]$
$\\ =\sum_{d=1}^{m}\mu(d)\left \lfloor \frac{n}{d} \right \rfloor\sum_{i=1}^{\left \lfloor \frac{m}{d} \right \rfloor}[(k,i)=1][(k,d)=1]$
$\\ =\sum_{d=1}^{min(n,m)}\mu(d)\left \lfloor \frac{n}{d} \right \rfloor[(k,d)=1]\sum_{i=1}^{\left \lfloor \frac{m}{d} \right \rfloor}[(k,i)=1]$

 1 //#include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 //#include<math.h>
5 //#include<set>
6 //#include<queue>
7 //#include<bitset>
8 //#include<vector>
9 #include<algorithm>
10 #include<stdlib.h>
11 using namespace std;
12
13 #define LL long long
15 {
16     char c; int s=0,f=1; while ((c=getchar())<'0' || c>'9') (c=='-') && (f=-1);
17     do s=s*10+c-'0'; while ((c=getchar())>='0' && c<='9'); return s*f;
18 }
19
20 //Pay attention to '-' , LL and double of qread!!!!
21
22 int n,m,K;
23 int f[2011];
24
25 #define maxm 20000011
26 int miu[maxm],prime[maxm],lp=0; bool notprime[maxm];
27 void pre(int n)
28 {
29     miu[1]=1;
30     for (int i=2;i<=n;i++)
31     {
32         if (!notprime[i]) {prime[++lp]=i; miu[i]=-1;}
33         for (int tmp,j=1;j<=lp && 1ll*i*prime[j]<=n;j++)
34         {
35             notprime[tmp=i*prime[j]]=1;
36             if (i%prime[j]) miu[tmp]=-miu[i];
37             else {miu[tmp]=0; break;}
38         }
39     }
40 }
41
42 int gcd(int a,int b) {while (b^=a^=b^=a%=b); return a;}
43 int getf(int x) {return x/K*f[K]+f[x%K];}
44
45 int main()
46 {
48     for (int i=1;i<=K;i++) f[i]=f[i-1]+(gcd(i,K)==1);
49     pre(min(n,m));
50     LL ans=0;
51     for (int i=1,to=min(n,m);i<=to;i++) if ((i%K>0) && (f[i%K]-f[(i-1)%K])>0)
52         ans+=miu[i]*(n/i)*1ll*getf(m/i);
53     printf("%lld\n",ans);
54     return 0;
55 }
View Code

！$k=p^cq$，设$p$为$k$的一个质因子，则$k$可以这么表示，其中$(q,p)=1$。要求与$k$互质的，那就求与$q$互质的，挑掉与$p$不互质和与$q$互质的。与$p$不互质的有一定是$p$的倍数，因为$p$是质数嘛。所以

$\\ g(t,k)=\sum_{i=1}^{t}[(k,i)=1]\mu(i)$
$\\ =\sum_{i=1}^{t}[(i,q)=1]\mu(i)-\sum_{j=1}^{\left \lfloor \frac{t}{p} \right \rfloor}[(jp,q)=1]\mu(jp)$
$\\ =g(t,q)-\mu(p)g(\left \lfloor \frac{t}{p} \right \rfloor,q)$
$\\ =g(t,q)+g(\left \lfloor \frac{t}{p} \right \rfloor,q)$

posted @ 2018-07-02 12:40  Blue233333  阅读(235)  评论(0编辑  收藏  举报