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 <iostream>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdio>
#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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值