TDL HDU - 6641 (数学+枚举)

题面:

For a positive integer nn, let's denote function f(n,m)f(n,m) as the mm-th smallest integer xx that x>nx>n and gcd(x,n)=1gcd(x,n)=1. For example, f(5,1)=6f(5,1)=6 and f(5,5)=11f(5,5)=11. 

You are given the value of mm and (f(n,m)n)n(f(n,m)−n)⊕n, where ``⊕'' denotes the bitwise XOR operation. Please write a program to find the smallest positive integer nn that (f(n,m)n)n=k(f(n,m)−n)⊕n=k, or determine it is impossible.

题意:

fn,m)表示比n大的,第m个与n互质的数。给出nkk=(f(n,m)-n)^n。问n是最小值。m最大为100

思路:

可以想到f(n,m)并不会比n大太多,所以我们可以枚举f(n,m)-n

对于每一个i,通过n=i^k,求出当前的n,然后再判断n+i是否恰好是n的第m个互质数(此处不可以判n+i内是否有m个与n互质的数当答案)。

要点:

对于我们枚举的上界。gcd(n+k,n)=gcd(n,k)=gcd(k,n%k),由于在1~k内至少有k/lgk个素数,所以nn+k内至少有k/lgk个与n互质的数。

所以上界up=700(up/lgup>100

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll k,m,n,key,i;
bool  check()
{
    ll s=0,j=1;
    for(;; j++)
    {
        if(__gcd(n,n+j)==1)s++;
        if(s==m) break;
    }
    return j==i;
}
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>k>>m;
        ll ans=-1;
        for( i=1; i<700; i++)
        {
            n=k^i;
            if(n>=2&&check())
            {
                if(ans==-1)ans=n;
                else ans=min(n,ans);
            }
        }
        cout<<ans<<endl;

    }
    return 0;
}
扫描二维码关注公众号,回复: 6992418 查看本文章

)

猜你喜欢

转载自www.cnblogs.com/yzxqq/p/11329620.html