HDU 6641 TDL 质数密度、暴力枚举

TDL

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


 

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 nthat (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

 

 

Source

2019 Multi-University Training Contest 6

 

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6742 6741 6740 6739 6738 

 


OJ题号

 HDU - 6641TDL

简单题意

f(n, m) 表示比n大的第m小的与n互质的数

给你k,m   求 (f(n,m)-n)^n = k的最小n

正解思路

枚举d,因为通过质数密度可以知道,一个数的与第一百个比他大且他互质的数之间的差值绝对不会超过1000。

 

#include <bits/stdc++.h>
using namespace std;
#define N 100000+5
typedef long long LL;
LL gcd(LL a,LL b)
{
    return b==0?a:gcd(b,a%b);
}
LL f(LL n,LL m)
{

    int cnt=0;
    for(LL i=n+1;; i++)
    {
        if(gcd(n,i)==1)
        {
            cnt++;
            if(cnt==m)
            {
                return i;
            }
        }
    }
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        LL m,k;
        scanf("%lld%lld",&k,&m);
        int flag=0;
        LL ans=-1;
        for(int d=1; d<=1000; d++)
        {
            //cout<<d<<endl;
            LL n=d^k;
            if(n==0)
                continue;
            if(f(n,m)-n==d)
            {

                if(ans==-1)
                    ans=n;
                else
                {
                    ans=min(ans,n);
                }
            }

        }

        printf("%lld\n",ans);
    }

    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值