Test instructions: Give a number M, let's find the number of K and M coprime.
Methods: This problem has two methods, one is Euler function + tolerance principle, but the code is large, another method is Euclidean algorithm, relatively easy to understand, but inefficient.
I use the Euclidean algorithm here, Euclidean algorithm also known as the Division method, originally pure for greatest common divisor, here is also a small expansion of the application, the problem of the use of Euclidean algorithm of the important nature, if A and B coprime, then b*t+a and B also must coprime, So we can enumerate all the qualifying numbers between the 1~m, and then hit a table to find all the qualifying numbers, as shown in the following table (5,5), this table is a periodic top-down rule table, with this rule, after the table, you can quickly find out the answer.
5 5
PRI[1] = 1 6 11 16 ...
PRI[2] = 2 7 12 17 ...
PRI[3] = 3 8 13 18 ...
PRI[4] = 4 9 14 19 ...
6
The code is as follows:
#include <iostream>#include<cstdio>using namespacestd;intpri[1000010];intGCD (intAintb) { returnb = =0? A:GCD (b, a%b);}intMain () {intm, K; while(~SCANF ("%d%d",&m,&k)) {intI, J; for(i =1, j =1; I <= m; i + + ) { if(GCD (m, i) = =1) {pri [j++ ] =i; /*printf ("pri[%d] =%d", j-1,i); for (int k = 1; k <= 3; k++) {printf ("%d", k*m+i); } printf ("... \ n");*/}} J--; if(K%j = =0) printf ("%d\n", (k/j-1) *m +Pri[j]); Elseprintf"%d\n", K/j*m + pri[k%J]); } return 0;}
POJ 2773 Happy 2006 (Euclidean algorithm)