2019 Multi-University Training Contest 6 TDL

本文介绍了一种解决特定数学问题的算法,该问题是找到满足条件(f(n,m)−n)⊕n=k的最小正整数n,其中f(n,m)是比n大的第m个与n互质的数。通过分析k的值与n的关系,采用遍历k附近数值的方法寻找符合条件的n。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TDL

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0

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 个与 n 互质的数,现已知 (f(n,m)−n)⊕n 的值为 k,求 n 的值

分析

(f(n,m)−n)⊕n=k 可以写成 f(n,m)-n=n⊕k,因为 m 的值为100,所以(f(n,m)−n) 的值比较小,则 n⊕k 的值比较小,说明n 与 k 相接近,所以对 k-512 到 k+512 之间的数进行遍历,找到适合的 n 即是答案。

代码

#include<bits/stdc++.h>
using namespace std;
int main(){
    int t;
    scanf("%d",&t);
    long long k,i;
    int m;
    while(t--)
	{
        scanf("%lld%d",&k,&m);
        if(k<512)	i=1;
        else 		i=k-512;
        long long ans=0;
        for( ;i<k+512;i++){
                long long kk=k^i;
                int cnt=0;
                for(long long j=i+1;;j++){
                    if(__gcd(j,i)==1){
                        cnt++;
                    }
                    if(cnt==m){
                        if(j==kk+i)
                            ans=i;
                        break;
                    }
                }
                if(ans)
                    break;
            }
        if(ans)
                printf("%lld\n",ans);
            else
                printf("-1\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值