先考虑如何判断 是否在直径为 的地毯的角上。
不难发现一个必要条件是 ,即 。
满足这个条件的 在图上是这些:
然后猜出充分条件是 。重申一遍:
考察第二个条件。可以改写成:
- 或 。
于是可以对 的奇偶性分类讨论。
- 是奇数:那么第二个条件相当于无效了。于是答案为 。( 表示 的约数个数。)
- 是偶数:问题变成了求 的约数和 的约数的补集的交集的大小(有多少 满足 )。根据 容斥原理,答案为 。原因如下:
的约数是 部分,非 的约数是 部分,它们的交是第 部分,也就是 的约数除去 的约数。
于是需要预处理 的约数个数。
// Problem:
// P9031 [COCI2022-2023#1] Iksevi
//
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P9031
// Memory Limit: 512 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#define tests
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 10;
int D[N];
void solve() {
int x, y;
cin >> x >> y;
if ((x + y) % 2) cout << D[__gcd(x, y)] << '\n';
else cout << D[__gcd(x, y)] - D[__gcd(__gcd(x, y), x + y >> 1)] << '\n';
}
signed main() {
for (int i = 1; i < N; ++ i )
for (int j = i; j < N; j += i) D[j] ++ ;
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T = 1;
#ifdef tests
cin >> T;
#endif
while (T -- ) solve();
return 0;
}
