Happy 2006
Time Limit: 3000MS |
|
Memory Limit: 65536K |
Total Submissions: 9936 |
|
Accepted: 3411 |
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
Title translation: Give M,k, ask K and M coprime numbers!
Problem-solving ideas: A large amount of data, so all using a long long, want to find out the number of K coprime, the data is too large, so the use of 2-point search, to find D K number, want to know the current number T is the number of coprime with M, need to ask for a few and M before T coprime number, the use of , then you can compare!
#include <cstdio> #define LL long Longll p[20],top;void getp (ll m) { ll i; for (i=2,top=0;i*i<=m;i++) if (m%i==0) { p[top++]=i; while (m%i==0) m/=i; } if (m>1) p[top++]=m;} ll NOP (ll Mid,ll t) { ll i,sum=0; for (i=t;i<top;i++) Sum+=mid/p[i]-nop (mid/p[i],i+1); return sum;} int main () { LL k,m; while (scanf ("%lld%lld", &m,&k) ==2) { LL mid,l=0,r=0x3f3f3f3f3f3f3f3f,t,ans=0; GETP (m); while (l<=r) { mid= (l+r) >>1; T=mid-nop (mid,0); if (t>=k) { r=mid-1; if (t==k) ans=mid; } else l=mid+1; } printf ("%lld\n", ans); } return 0;}
POJ 2006 Happy2006