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...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(数学,暴力枚举)

时间: 2024-10-16 18:42:40

HDU 5175 Misaki's Kiss again(数学,暴力枚举)的相关文章

HDU 5175 Misaki&#39;s Kiss again (异或运算,公式变形)

Misaki's Kiss again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 201    Accepted Submission(s): 57 Problem Description After the Ferries Wheel, many friends hope to receive the Misaki's kiss

hdu 5175 Misaki&#39;s Kiss again(GCD和异或)

题意: 给一个数N. 如果GCD(N,M) = N XOR M,则称M是一个kiss   1<=M<=N 问总共有多少个kiss.并且列出所有的值. 思路: 思路一:枚举M.有大量的GCD(N,M)值相等.但是N XOR M=X.当X是定值是,M一定只有一个.所以这个方法明显有大量重复. 发现知道GCD(N,M)的值,可直接求出M.搞定. 令gcd(n,m)=d,可知道d是n的约数 有d=(n xor m)=(m xor n),所以m=(d xor n). 可发现gcd(n,(d xor n)

HDU 5175 Misaki&#39;s Kiss again

Misaki's Kiss again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 513    Accepted Submission(s): 121 Problem Description After the Ferries Wheel, many friends hope to receive the Misaki's kiss

hdu 4932 Miaomiao&amp;#39;s Geometry(暴力枚举)

Miaomiao's Geometry                                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description There are N point on X-axis . Miaomiao would like

HDU 2058 The sum problem (数学+暴力)

题意:给定一个N和M,N表示从1到N的连续序列,让你求在1到N这个序列中连续子序列的和为M的子序列区间. 析:很明显最直接的方法就是暴力,可是不幸的是,由于N,M太大了,肯定会TLE的.所以我们就想能不能优化一下,找一个范围.想到这是一个连续的序列而且是从1开始的,这不就是一个等差数列么,公差是1罢了.由求和公式得Sn = (a1+an) * n / 2;所以说n最大就是sqrt(M*2)(想一想为什么),因为a1+an 一定是大于n的.如果我们取区间的和,那么Sn = (ai+aj) * (j

poj 1543 &amp; HDU 1334 &amp; ZOJ 1331 Perfect Cubes(数学 暴力大法好)

题目链接:http://poj.org/problem?id=1543 Description For hundreds of years Fermat's Last Theorem, which stated simply that for n > 2 there exist no integers a, b, c > 1 such that a^n = b^n + c^n, has remained elusively unproven. (A recent proof is believ

Valentine&#39;s Day Round 1002 Misaki&#39;s Kiss again

题意 Misaki's Kiss again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 571    Accepted Submission(s): 75 问题描述 摩天轮后,一些朋友希望再次得到Misaki的吻,所以Misaki把他们分别编号从1到N,如果他们中有人的编号是M,而且gcd(N,M)=N xor M,那么他以可以得

hdu 5247 找连续数【暴力枚举】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5247 分析:这道题是2015百度之星初赛1的2题,当时没看这道题 是队友看的,比完以后也做了一下,思路大体都是一样的,就是 暴力枚举,因为k<=1000,那么我们可以每一点x为起点跑[x,x+999] 这段区间,把每得到一段连续的子区间[x,?],则num[len]++(len=size([x,?])); 这样就可以了,最后num数组里就是对应的答案 献上代码: #include<stdio.h&

HDU 4930 Fighting the Landlords(暴力枚举+模拟)

HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型,如果先手能一步走完,或者一步让后手无法管上,就赢 思路:先枚举出两个人所有可能的牌型的最大值,然后再去判断即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Player { int rank[15]; } p1, p2; int t, h