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 nthat (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)==k的范围为(k-1000,k+1000),m的范围0-100,当m最大为100时,与n互质的数也在范围之内,所以只要枚举范围之内的数就可以了。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <cstdio>
using namespace std;
typedef long long ll;
ll n,m,x,y,k,q;
ll flag;
ll ppp(ll x,ll m)
{
ll y=0,ans;
for(ll i=x+1;;i++)
{
if(__gcd(x,i)==1)//互质
{
y++;
ans=i;
}
if(y==m)
return ans;
}
}
int main()
{
ll t;
cin>>t;
while(t--)
{
flag=0;
cin>>k>>m;
if(k<1001)
q=1;
else
q=k-1000;
for(ll i=q; i<k+1000; i++)
{
ll s=i^k;
if((ppp(i,m)-i)==s)
{
cout<<i<<endl;
flag=1;
break;
}
}
if(!flag)
{
cout<<"-1"<<endl;
}
}
}