码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-02-17 14:07:57      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

题目大意:

After the Ferries Wheel, many friends hope to receive the Misaki‘s kiss again,so Misaki numbers them 1,2...N?1,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;
}


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

标签:

原文地址:http://blog.csdn.net/moguxiaozhe/article/details/43865875

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!