Happy 2006
| Time Limit: 3000MS |
|
Memory Limit: 65536K |
| Total Submissions: 9170 |
|
Accepted: 3092 |
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
POJ monthly--2006.03.26,static
Main topic:
Given m,k, what is the number of K and M coprime? where m (1 <= m <= 1000000), K (1 <= k <= 100000000).
Problem Solving Ideas:
The method of calculating the number of [1,x] and M coprime using the tolerant principle of the bitwise operation is:
Assuming that the mass factor of M is a,b,c, then the number of non-mutually exclusive with M is F (a) +f (b) +f (c)-F (AB)-F (AC)-fa (BC) +f (ABC), f (t) meaning is (1,x) How many numbers with T not coprime, obviously f (t) =x/t, then The number of mutexes with M is obviously X ( F (a) +f (b) +f (c)-F (AB)-F (AC)-fa (BC) +f (ABC).
So as long as the two points to find just greater than equal to K this number is the answer.
Problem Solving Code:
#include <iostream> #include <cstdio> #include <vector>using namespace std;typedef long Long ll;const int maxn=1100000;int m,n;vector <int> v;vector <int> t;bool f[maxn];void getprime () {for (int i=0;i<maxn; i++) F[i]=true; for (int i=4;i<maxn;i+=2) F[i]=false; for (int i=3;i<maxn;i+=2) {for (int j=i;j<maxn/i;j+=2) {f[i*j]=false; }} for (int i=2;i<maxn;i++) {if (F[i]) v.push_back (i); }}void Getsubprime () {t.clear (); int s=0,now=m; while (Now>=v[s]) {if (now%v[s]==0) {t.push_back (v[s]); while (now%v[s]==0) {now/=v[s]; }} s++; }}ll get (ll y) {ll ans=0; for (int i=0;i< (1<<t.size ()); i++) {int cnt=0,sum=1; for (int k=0;k<t.size (); k++) {if (i& (1<<k)) {sum*=t[k]; cnt++; }} if (cnt%2==0) ans+=y/sum; else ans-=y/sum; } return ans; void Solve () {getsubprime (); ll L=0,r=1e17; while (L<r) {ll mid= (l+r)/2; if (Get (mid) >=n) R=mid; else l=mid+1; } Cout<<r<<endl;} int main () {getprime (); while (scanf ("%d%d", &m,&n)!=eof) {solve (); } return 0;}