TDL
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 618 Accepted Submission(s): 277
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位的质数,求(f(n,m)-n)^n=k的n的最小值。
因为m比较小 相差100位的质数,不会大小超过1024,就是10位二进制。
所以只需要遍历k的二进制的后十位就行。
Code:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,m,t;
ll k;
ll gcd(ll a,ll b)
{
ll t;
while(b)
{
t=a%b;
a=b;
b=t;
}
return a;
}
int main()
{
cin>>t;
while(t--)
{
cin>>k>>m;
int f=0;
ll n=(k>>10)<<10;
for(int i=1; i<=1024; i++)
{
ll c=1;//(f(n,m)-n)差值
int cnt=m;
while(cnt>0)
{
ll tmp=gcd(n+i,n+i+c);
if(tmp==1)
{
cnt--;
}
if(cnt==0)
{
break;
}
c++;
}
if((c^(n+i))==k)
{
f=1;
cout<<n+i<<endl;
break;
}
}
if(!f)
{
cout<<"-1"<<endl;
}
}
return 0;
}