HDU 5175 Misaki'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
again,so Misaki numbers them 1 , 2 , .... , N , if someone number is M
and satisfied the  GCD(N,M) equal to N XOR M , he will be kissed again .

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

Node that :

GCD( a , b ) means the greatest common divisor of a and b .

A XOR B means A exclusive or B

Input

There are multiple test cases.

For each testcase, contains a integets N(0<N<=1010)

Output

For each test case,
first line output Case #X:,
second line output k means the number of friends will get a kiss.
third line contains k number mean the friends‘ number, sort them in ascending and separated by a space between two numbers

Sample Input

3

5

15

Sample Output

Case #1:

1

2

Case #2:

1

4

Case #3:

3

10 12 14

Hint

In the third sample, gcd(15,10)=5 and (15 xor 10)=5, gcd(15,12)=3 and (15 xor 12)=3,gcd(15,14)=1 and (15 xor 14)=1

sqrt(n) 求出n的所有约数ei。

然后求出 a 符合: a^n == ei  。

再判断 a 跟 n 的 GCD 是否等于ei 即可。

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <vector>
#include <queue>

using namespace std ;
typedef long long LL ;
typedef pair<int,int> pii;
#define X first
#define Y second

const int N = 100010;
LL n , e[N] , x[N] ;
int aa[100] , bb[100] , res[100] ;
inline LL gcd( LL a , LL b ) { return b == 0 ? a : gcd( b , a % b );}
LL decode( LL a , LL b ) {
    int lena = 0 , lenb = 0 ;
    memset( aa , 0 , sizeof aa );
    memset( bb , 0 , sizeof bb );
    LL x = a ;
    while( x > 0 ) { aa[lena++] = x % 2 ; x /= 2 ; }
    x = b ;
    while( x > 0 ) { bb[lenb++] = x % 2 ; x /= 2 ; }

    LL ans = 0 ;
    for( int i = 0 ; i < lena ; ++i ) {
        if( bb[i] == 0 ) res[i] = aa[i];
        else res[i] = aa[i]^1;
        if( res[i] ) ans += (1LL<<i);
    }
    return ans ;
}

void solve() {
    int tot = 0 , cnt = 0  ;
    for( LL i = 1 ; i * i <= n ; ++i ) {
        if( n % i == 0 ){
            e[tot++] = i ;
            if( i != n/i ) e[tot++] = n/i ;
        }
    }
    for( int i = 0 ; i < tot ; ++i ) {
       LL tmp = decode( n , e[i] );
       if( tmp < 1 || tmp > n || tmp < e[i] || gcd( n , tmp ) != e[i] ) continue ;
       x[cnt++] = tmp ;
    }
    sort( x , x + cnt );
    printf("%d\n",cnt);
    for( int i = 0 ; i < cnt ; ++i ) {
        printf("%I64d",x[i]);
        if( i < cnt -1 ) printf(" ");
    }
    puts("");
}
int main ()  {
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
    int _ , cas =1 ;
    while( scanf("%I64d",&n) != EOF ) {
        printf("Case #%d:\n",cas++);
        solve();
    }
}

HDU 5175 Misaki's Kiss again

时间: 2024-08-21 15:36:27

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

题目大意: 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<=

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,那么他以可以得

Misaki&#39;s Kiss again(hdu5175)

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

HDU 5175

我想了很久了,后来还是把N分解质因数,枚举各种组合,反正也不多吧,按题目条件,然后就过了. #include <cstdio> #include <iostream> #include <cstring> #include <cctype> #include <algorithm> #define LL __int64 using namespace std; LL prime[100010],np; int fac[100010],nf; in

个人代码问题以及总结

1.二进制枚举超限 不能超过50.总结,可以采取其他枚举子集的方法,不过如果子集数过多的话,说明应该换一种思路了 2.if语句的前后对应 例题1:hdu 5175 //循环体{ m=(n^i); if(m<=0||m>n) continue; //错误在这里,本来希望判断(n^i)在不在1到n之间,却直接把接下来待判断的n^(n/i)判断漏掉了,直接跳过了这次循环 if(kgcd(n,m)==i) nt[k++]=m; if(i*i==n) continue; m=n^(n/i); if(m&

BestCoder Valentine&#39;s Day Round

昨晚在开赛前5分钟注册的,然后比赛刚开始就掉线我就不想说了(蹭网的下场……),只好用手机来看题和提交,代码用电脑打好再拉进手机的(是在傻傻地用手机打了一半后才想到的办法). 1001,也就是 hdu 5174,题意很难叙述了,自己看题吧,这题有数据溢出的风险,我竟然是AC了一发才发觉的(只过了小数据),幸好后来改后赶紧再交一遍才不至于被人hack,因为需要对数据去重,我不想用数组模拟,便尝试下用 map了,可是我的奇葩 map ( ~′⌒`~),连我自己都感到无语了: 1 #include<cs

HDU5175Misaki&#39;s Kiss again——数论

http://acm.split.hdu.edu.cn/showproblem.php?pid=5175 若gcd(a,b)= a xor b=c,则b=a-c (打表发现的) c是a的因子,则枚举a的所有因子判断b是否满足a^b=c 93MS 1104K 893 B #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<vector>