POJ Happy 2006 //gcd性质,欧拉
Happy 2006
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
题意求:求与n互质的第m个数gcd(a,b)== gcd(a+b*t,b)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
#define LL long long
int gcd(int x,int y){
if(y==0) return x;
return gcd(y,x%y);
}
int a[1000005];
int main(){
int m,k;
while(scanf("%d%d",&m,&k)!=EOF){
int p=-1;
for(int i=1;i<=m;i++){
if(gcd(i,m)==1) a[++p]=i;
}
if(k%(p+1)==0) cout<<k/(p+1)*m-m+a[p]<<endl;
else cout<<k/(p+1)*m+a[k%(p+1)-1]<<endl;
}
return 0;
}
可以用欧拉函数,先求互质个数。(能快一点,但是要卡死还是得卡死)