Two positive integers are said to be relatively prime to each other if the Great Common Divisor (GCD) is 1. For instance, 1, 3, 5, 7, 9...are all relatively prime to 2006.
Now your job is easy: for the given integer m, find the K-th element which is relatively prime to m when these elements are sorted in ascending order.
Input
The input contains multiple test cases. For each test case, it contains two integers m (1 <= m <= 1000000), K (1 <= K <= 100000000).
typedeflonglong LL; constint maxn = 1e6 +10; int a[maxn];
intgcd(int a, int b){ return b ? gcd(b, a% b) : a; }
intmain(){ int k, m; ios::sync_with_stdio(false); while(cin >> m >> k){ int cnt = 0; k--; for(int i = 1; i <= m; ++i) if(gcd(i, m) == 1) a[cnt++] = i; cout<<a[k % cnt] + k / cnt * m<<endl; }return0; }
inteuler(int n){ int ans = n; int m = n; memset(prime, false, sizeof(prime)); for(int i = 2; i * i <= n; ++i) if(n % i == 0){ ans -= ans / i; for(int j = 1; j * i <= m; ++j) prime[j * i] = true; while(n % i == 0) n /= i; }if(n > 1){ ans -= ans / n; for(int j = 1; j * n <= m; ++j) prime[j * n] = true; }return ans; }
intmain(){ int n, k; ios::sync_with_stdio(false); while(cin >> n >> k){ int t = euler(n); int tt = k / t; if(k % t == 0) tt--; int num = 0,cur; t = k - t * tt; for(int i = 1; i <= n; ++i){ if(!prime[i]) ++num; if(num == t){ cur = i; break; } }cout<<cur + n * tt<<endl; }return0; }