Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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.
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).
For each test case, print a single line containing an integer, denoting the smallest n. If there is no solution, output ``-1'' instead.
2 3 5 6 100
5 -1
巧妙的暴力,由于公式的关系可得知i与k值相差不远,我们可以设立一个差值,在差值中遍历i即可。(实测500也可以)
#include
using namespace std;
#define ll long long
const int MAXN = 1e3 + 7;
ll gcd(ll a, ll b)
{
return b ? gcd(b, a%b) : a;
}
ll fun(ll n, ll m)
{
ll i = n;
while(m)
{
i++;
if(gcd(i, n) == 1) m--;
}
return i;
}
int main()
{
ios::sync_with_stdio(0);
ll t, k, m;
int flag;
cin>>t;
while(t--)
{
cin>>k>>m;
flag = 1;
for(ll i = max((ll)1, k-MAXN); i < k+MAXN; i++)
{
if(((fun(i, m) - i)^i) == k)
{
cout<