Happy 2006 poj2773

Happy 2006
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 9049   Accepted: 3031

Description

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).

Output

Output the K-th element in a single line.

Sample Input

2006 1
2006 2
2006 3

Sample Output

1
3
5


 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #include<math.h>
 5 #include<iostream>
 6 #include<algorithm>
 7 using namespace std;
 8 #define maxn 10000010
 9 bool a[maxn];
10 int euler(int n)
11 {
12     int i,j,m=n,ann=n;
13     memset(a,0,sizeof(a));
14     int size=sqrt(m+0.5);
15     for(i=2; i<=size; i++)
16     {
17         if(n%i==0)
18         {
19             ann=ann/i*(i-1);
20             for(j=i; j<=m; j+=i)a[j]=1;
21             while(n%i==0)n/=i;
22         }
23     }
24     if(n>1)
25     {
26         ann=ann/n*(n-1);
27         for(j=n; j<=m; j+=n)a[j]=1;
28     }
29     return ann;
30 }
31 int main()
32 {
33     int n,k,i,m,t,q;
34     while(~scanf("%d%d",&n,&k))
35     {
36         m=euler(n);
37         if(k%m==0)
38         {
39            t=k/m-1;
40         }else t=k/m;
41         k=k-m*t;
42         q=0;
43         for(i=1;i<=n;i++)
44         {
45             if(!a[i])q++;
46             if(q==k)break;
47         }
48         printf("%lld
",(long long)i+n*t);
49     }
50 }
View Code
原文地址:https://www.cnblogs.com/ERKE/p/3652824.html