Happy 2006
| Time Limit: 3000MS |
|
Memory Limit: 65536K |
| Total Submissions: 11956 |
|
Accepted: 4224 |
Description
The positive integers is said to being relatively prime to all other if the great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are-relatively prime to 2006.
Now your job is easy:for the given integer m, find the k-th element which was relatively prime to m when these elements ar e sorted in ascending order.
Input
The input contains multiple test cases. For each test case, it contains integers m (1 <= m <= 1000000), K (1 <= k <= 100000000).
Output
Output the k-th element in a single line.
Sample Input
2006 12006 22006 3
Sample Output
135
Source
Periodic!
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>using namespaceStd;typedefLong LongLL;#defineMAXN 1000002/*required to find the number gcd (A, b) = gcd (b*t+a,b) with M coprime's K-Large*/LL A[MAXN]; ll GCD (ll A,ll b) {if(b==0) returnA; returnGCD (b,a%b);}intMain () {LL m,k; while(SCANF ("%lld%lld", &m,&k)! =EOF) {LL P=0; for(intI=1; i<=m;i++) { if(GCD (i,m) = =1) a[++P] =i; } if(k%p==0) printf ("%lld\n", (k/p-1) *m+a[p]); Elseprintf ("%lld\n", (k/p) *m+a[k%p]); }}
Happy 2006 Euclidean theorem