杭电6TDL(暴力)
题目描述
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.
输入
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
题解:暴力f(n,m)−n,这一坨因为m比较小,所以这一坨也比较小,所以可以枚举n,找符合条件的答案。复杂度也不高。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){
if(b==0) return a;
return gcd (b,a%b);
}
ll check(ll n,ll m){
ll cnt=0;
for(ll i=n+1;;i++) {
if(gcd (i,n)==1){
cnt++;
}
if(cnt==m) return i;
}
return 0;
}
int main()
{
int t;
cin>>t;
while (t--) {
ll k;
int m;
cin>>k>>m;
ll p=0;
if(k<700) p=1;
else p=k-700;
ll ans=0;
for(ll i=p;i<=k+700;i++) {
if(check(i,m)-i==(k^i)) {
ans=i;
break;
}
}
if(ans) cout<<ans<<endl;
else cout<<-1<<endl;
}
return 0;
}