题意
找到满足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;
}