1 solutions
-
0
gcd&xor
设满足条件的数对是 ,满足 。
算法一
直接枚举两个数并判断。
时间 。预处理最大公约数的话就是 。
算法二
先枚举 ,再枚举 的倍数作为 。那么根据异或的性质必然有 ,直接判断即可。
枚举数量等于 以内一对倍数的数对个数,等于 $\sum_{g=1}^n \lfloor\frac{n}{g}\rfloor = O(n\log n)$。暴力求最大公约数的话总时间是 。
算法三
仔细考虑一下:当 时 不可能满足条件。当 时,。所以满足条件当且仅当两者都等于 。这样在算法二基础上检查的时间变为 ,总时间变为 。
思考:这个题的约束实际上限制更强。想一想,你能否继续降低枚举数量得到更低的复杂度?(没有标准答案。)
#include <iostream> #include <algorithm> using namespace std; int n; int main() { freopen("gcdxor.in", "r", stdin); freopen("gcdxor.out", "w", stdout); ios::sync_with_stdio(false); cin >> n; int ans = 0; for (int c = 1; c <= n; ++c) { for (int a = 2 * c; a <= n; a += c) { if ( (a ^ c) == a - c ) ++ans; } } cout << ans << endl; return 0; }
- 1
Information
- ID
- 26
- Time
- 1000ms
- Memory
- 512MiB
- Difficulty
- 8
- Tags
- (None)
- # Submissions
- 12
- Accepted
- 10
- Uploaded By