luogu P1592 互質(歐拉函數)
阿新 • • 發佈:2018-09-01
ring pan ret string pri bubuko 這樣的 bsp png
題意
(n<=106,k<=108)
題解
一開始以為是搜索。
但想想不對,翻了一眼題解發現是歐拉函數。
因為
gcd(a,b)=gcd(a,a+b)
所以和n互質的數應該是類似a1,a2.....ax,a1+n,a2+n.....ax+n......這樣的。
所以就可以瞎搞了。
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmath> 5 #include<algorithm> 6 using namespacestd; 7 int n,d,phi,c[1500000],tmp,ans,cnt; 8 int gcd(int x,int y){ 9 if(x==0)return y; 10 if(y==0)return x; 11 return gcd(y,x%y); 12 } 13 void get_phi(){ 14 for(int i=1;i<=n;i++){ 15 if(gcd(i,n)==1){ 16 phi++; 17 c[++cnt]=i; 18 } 19 } 20 } 21int main(){ 22 scanf("%d%d",&n,&d); 23 get_phi(); 24 tmp=(d-1)/phi; 25 ans=(d-1)%phi+1; 26 printf("%d",c[ans]+tmp*n); 27 return 0; 28 }
luogu P1592 互質(歐拉函數)