大炮打蚊子——二进制光速GCD
2026-07-30 22:06:52
发布于:广东
7阅读
0回复
0点赞

我用光速GCD来做不过分吧~
AC代码:
#include <bits/stdc++.h>
using namespace std;
int n, m;
inline int gcd(int x, int y) {
if (!x) return y;
if (!y) return x;
int shift = __builtin_ctz(x | y);
x >>= __builtin_ctz(x);
y >>= __builtin_ctz(y);
while (x != y) {
if (x > y) {
swap(x, y);
}
y -= x;
y >>= __builtin_ctz(y);
}
return x << shift;
}
int main() {
cin >> n >> m;
cout << gcd(n, m) << endl;
return 0;
}
这里空空如也



有帮助,赞一个