Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 11161 | Accepted: 3893 |
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
Source
POJ Monthly--2006.03.26,static
想说一下这题的大意,就是找出与m互质的第k个数
这明显是一道数论的题,开始没有想到什么好的方法就暴力,毫无疑问TLE(注:正确的暴力是不会超时的)
后来找了几份题解看了看,找的了正确的做法,用容斥原理求出和它不互质的个数,拿n一减,个数就出来了,
不过这仍然存在一个问题,上面的TLE又跳了出来,又因为这里1到n(1<=n<=m)中间,n的值越大,与m互质
的数更多,很符合二分的特点
下面问题就变成了给了你一个从小到大排列的数组,找到k最早出现的位置(想想为什么,因为当从k-1增加到
第k个的时候,k一定与m互质,所以才会增加一个互质的数)
接下来问题就变得十分简单了,附上秒过的代码(第一次提交手抽,把某个地方的limit打成m,导致速度110ms)
Code:
/**
* poj.org
* Problem#2773
* Accepted
* Time:0ms
* Memory:172k
*/
#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
typedef bool boolean;
typedef long long ll;
int m;
ll k;
int factor[];
int _count;
/***
* 分解质因数
*/
void init(int n){
_count = ;
int limit = (int) sqrt(n + 0.5);
for(int i = ;i <= limit;i++){ //不用考虑i是否为指数
if(n == ) break;
if(n % i == ){
factor[_count++] = i; //保存质因数
while(n % i == ) n /= i; //除干净
}
}
if(n > ) factor[_count++] = n;
}
ll getCount(ll n){
if(m == ) return n;
if(n == ) return ;
long long result = n;
for(int i = ;i < ( << _count );i++){ //遍历所有情况
long long temp = i, a = , b = ;
for(int j = ;j < _count&&temp != ;j++){
if((temp & )== ){ //用1来表示取第i个质数,0表示不去
a *= factor[j]; //分母的乘积
b++; //统计个数
}
temp >>= ;
}
if((b&)==) result -= n/a; //个数为奇数,根据容斥原理,应该减
else result += n/a; //个数为偶数,应该加
}
return result;
}
int main(){
while(~scanf("%d%ld",&m,&k)){ //当没有收到数据的时候是EOF(-1)取反后是0(false)
if(m == ){
printf("%ld\n",k);
continue;
}
if(k == ){
printf("1\n"); //特殊处理,加快速度
continue;
}
ll from = ;
ll end = 1LL<<;
ll result;
init(m);
while(from <= end){ //二分查找
ll mid = (from + end) >> ;
ll c = getCount(mid); //计算个数
if(c > k) end = mid - ;
else if(c < k) from = mid + ;
else{ //这里不可以break,二分找到的第一个不一定是答案
result = mid; //例如数列1 1 1 2 3查找1,第一次找到的1不一定是最左边的
end = mid - ;
}
}
printf("%ld\n",result);
}
return ;
}