Title Description:
Analysis:
According to Euclid, we have gcd (bxt+a,b) =GCD (A, B)
If A and B coprime, then bxt+a and B must also coprime, if A and B do not coprime, then Bxt+a and B must not coprime.
So the number of M coprime with M is periodic, then according to this method we can quickly find out the number of K and M coprime.
Assuming that the number is less than m and the number of M coprime has L, where the i is AI, then the number of K*l+i and M coprime is K*m+ai.
So, I'm going to find out the number of M coprime within M and then the periodic solution. (Feeling a little violent, right?)
The code is as follows, very short:
1#include <cstdio>2#include <cstring>3#include <cstring>4#include <iostream>5#include <algorithm>6#include <cmath>7 using namespacestd;8 #defineMAXN 10000109 #defineLL Long LongTen One intP[maxn],len; A - intgcdintAintb) - { the if(b==0)returnA; - returnGCD (b,a%b); - } - + intMain () - { + intm,k; A while(SCANF ("%d%d", &m,&k)! =EOF) at { - if(m==1) {printf ("%d\n", k);Continue;} -len=0; - for(intI=1; i<=m;i++)if(GCD (i,m) = =1) p[++len]=i; - intans; - if(k%len==0) ans= (k/len-1) *m+P[len]; in Elseans=k/len*m+p[k%Len]; -printf"%d\n", ans); to } + return 0; -}
poj2773
2016-02-05 16:21:38
"poj2773" Happy 2006 Euclid