H-Happy 2006
Time
limit:MS
Memory Limit:65536KB
64bit IO Format:%i64d &A mp %i64u Submit Status
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 Meaning: The topic is simple and easy to understand, but it takes a bit of effort to solve the problem. First of all know that if a number and another number of GCD is not 1, then they have a public quality factor, so first of all the minimum quality of n is calculated, and then the block, the addition of the allowance can be. How to divide? It can be understood that for a certain number of interval {l,r}, you can know how many with n is not coprime, this can be done with the principle of tolerance, and the rest is to extend the interval. Shape such as [L + x,r + y], this form; look at the code! AC Code:#include <iostream> #include <cstdio> #include <cstring> #include <vector>using namespace std; typedef long Long Ll;vector<int>tp;int main () {int n,k,t; while (~SCANF ("%d%d", &n,&k)) {tp.clear (); t = N; for (int i = 2; I * I <= n; i++) {if (t% i = = 0) {tp.push_back (i); while (!) ( t i)) T/= i; }} if (t! = 1) tp.push_back (t); ll Now,next,len,ans; Len = Tp.size (); now = 1; Next = now + k-1; while (k) {ans = 0; for (int i = 1; i < (1LL << Len); i++) {int mul = 1,tmp = 0; for (int j = 0; J < Len; J + +) if ((I >> J & 1)) Mul *= tp[j],tmp++; if (TMP & 1) ans + = Next/mul-(now-1)/mul; else ans-= next/mul-(now-1)/mul; } K = k-(Next-now + 1-ans); if (!k) break; now = next + 1; Next = now + k-1; } printf ("%i64d\n", next); } return 0;}
H-happy 2006