杭电6TDL(暴力)

杭电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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值