TDL
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
For a positive integer n, let’s denote function f(n,m) as the m-th smallest integer x that x>n and gcd(x,n)=1. For example, f(5,1)=6 and f(5,5)=11.
You are given the value of m and (f(n,m)−n)⊕n, where ``⊕’’ denotes the bitwise XOR operation. Please write a program to find the smallest positive integer n that (f(n,m)−n)⊕n=k, or determine it is impossible.
Input
The first line of the input contains an integer T(1≤T≤10), denoting the number of test cases.
In each test case, there are two integers k,m(1≤k≤1018,1≤m≤100).
Output
For each test case, print a single line containing an integer, denoting the smallest n. If there is no solution, output ``-1’’ instead.
Sample Input
2
3 5
6 100
Sample Output
5
-1
题意
f (n, m) 代表比 n 大的第 m 个与 n 互质的数,现已知 (f(n,m)−n)⊕n 的值为 k,求 n 的值
分析
(f(n,m)−n)⊕n=k 可以写成 f(n,m)-n=n⊕k,因为 m 的值为100,所以(f(n,m)−n) 的值比较小,则 n⊕k 的值比较小,说明n 与 k 相接近,所以对 k-512 到 k+512 之间的数进行遍历,找到适合的 n 即是答案。
代码
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
scanf("%d",&t);
long long k,i;
int m;
while(t--)
{
scanf("%lld%d",&k,&m);
if(k<512) i=1;
else i=k-512;
long long ans=0;
for( ;i<k+512;i++){
long long kk=k^i;
int cnt=0;
for(long long j=i+1;;j++){
if(__gcd(j,i)==1){
cnt++;
}
if(cnt==m){
if(j==kk+i)
ans=i;
break;
}
}
if(ans)
break;
}
if(ans)
printf("%lld\n",ans);
else
printf("-1\n");
}
return 0;
}