HDU 5175 Misaki's Kiss again(数学,暴力枚举)

题目大意:

After the Ferries Wheel, many friends hope to receive the Misaki's kiss again,so Misaki numbers them 1,2...N1,N,if someone's number is M and satisfied the GCD(N,M) equals to N XOR M,he will be kissed again.

Please help Misaki to find all M(1<=M<=N).

Note that:
GCD(a,b) means the greatest common divisor of a and b.
A XOR B means A exclusive or B


解题思路:

gcd(N,M) == N XOR M ,则 N XOR M 是 N的一个约数,枚举N的所有约数,然后求GCD判断。

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define LL long long
using namespace std;
const int maxn = 10000000 + 10;
LL ans[maxn];
LL gcd(LL a, LL b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    LL N;
    int kcase = 1;
    while(scanf("%I64d", &N)!=EOF)
    {
        LL sum = 0;
        LL R = (LL)(sqrt(N + 0.5));
        for(LL i=1;i<=R;i++)
        {
            if(N % i == 0)
            {
                if((N ^ i) >=1 && (N ^ i) <= N && gcd((N ^ i),N) == i)
                    ans[++sum] = (N ^ i);
                if(i != N / i)
                {
                    LL x = N / i;
                    if((N ^ x)>=1 && (N ^ x) <= N && gcd((N ^ x),N)==x)
                        ans[++sum] = (N ^ x);
                }
            }
        }
        printf("Case #%d:\n", kcase++);
        printf("%I64d\n", sum);
        sort(ans+1,ans+1+sum);
        if(sum > 0)
        {
            printf("%I64d", ans[1]);
            for(int i=2;i<=sum;i++) printf(" %I64d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(数学题)