hdu 5175 亦或运算和gcd

来源:互联网 发布:java 网络编程框架 编辑:程序博客网 时间:2024/05/18 00:31

gcd ( n ,m ) == n^m

0<m<=n,设gcd(n,m)==d;

n^m == d   =>  n^d = m

if (  0 < n^d <=n ) gcd ( n , d^n ) == d =>gcd(n,m)==n^m

所以我们可以sqrt(n)的枚举n的约数然后根据公式判断即可

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#define MAX 100009using namespace std;typedef long long LL;LL ans[MAX];LL n;int cnt = 1;LL gcd ( LL a , LL b ){    return b==0?a:gcd(b,a%b);}void solve ( ){    cnt = 1;    for ( LL i = 1 ; i*i<= n ; i++ )    {        if ( n%i ) continue;        LL x = n^i;        if ( x > 0 && x <= n && gcd ( n, x ) == i )             ans[cnt++] = x;        if ( n/i == i ) continue;        if ( i == 1 ) continue;        x = n^(n/i);        if ( x > 0 && x <= n && gcd ( n , x ) == n/i )            ans[cnt++] = x;    }}int main ( ){    int c = 1;    while ( ~scanf ( "%lld" , &n ) )    {        solve ( );        printf ( "Case #%d:\n" , c++ );        sort ( ans+1 , ans + cnt );        cnt--;        printf ( "%d\n" , cnt );        for ( int i = 1 ; i < cnt ; i++ )            printf ( "%lld " , ans[i] );        if ( cnt == 0 ) puts ( "" );        else printf ( "%lld\n" ,ans[cnt] );    }}


0 0
原创粉丝点击