HDU 5175 Misaki's Kiss again(暴力枚举+异或运算)

题意

找到满足gcd(N,M)==NxorM的M值(1<=M<=N)

解析:

令M = N xor K,原式:gcd(N,N xor K) == N xor (N xor K) == K
由此我们可以发现K是N的约数,找到所有N的约数,判断是不是满足那个等式即可,因为是异或运算,结果可能比约数本身大,如1xor2==3,还有异或出来结果等于0的舍掉,gcd(n,n) != n xor n。

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
ll n;
ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}
void solve() {
    vector<ll> ans;
    ans.clear();
    for(ll i = 1; i*i <= n; i++) {
        if(n % i == 0) {
            ll m = n^i;
            if(1 <= m && m <= n && gcd(m, n) == i) {
                ans.push_back(m);
            }
            m = n^(n/i);
            if(1 <= m && m <= n && gcd(m, n) == (n/i)) {
                ans.push_back(m);
            }
        }
    }
    sort(ans.begin(), ans.end());
    int sz = ans.size();
    printf("%d\n",sz);
    for(int i = 0; i < sz; i++) {
        printf("%lld",ans[i]);
        if(i != sz-1) putchar(' ');
    }
    puts("");
}
int main() {
    int cas = 1;
    while(scanf("%lld", &n) != EOF) {
        printf("Case #%d:\n", cas++);
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值