小D的Lemon
小D的Lemon
https://ac.nowcoder.com/acm/problem/22592
小D的Lemon
这里还是给出的朴素版本地写法...
/* Author : lifehappy */ #include <bits/stdc++.h> using namespace std; const int N = 3e5 + 10, mod = 1e9 + 7, Mod = mod - 1; int prime[N], g[N], invg[N], mu[N], cnt; bool st[N]; int quick_pow(int a, int n) { int ans = 1; while(n) { if(n & 1) ans = 1ll * ans * a % mod; a = 1ll * a * a % mod; n >>= 1; } return ans; } int inv(int x) { return quick_pow(x, mod - 2); } void init() { g[1] = mu[1] = 1; for(int i = 2; i < N; i++) { if(!st[i]) { prime[++cnt] = i; g[i] = 1; mu[i] = -1; } for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) { st[i * prime[j]] = 1; g[i * prime[j]] = g[i] + 1; if(i % prime[j] == 0) { break; } mu[i * prime[j]] = -mu[i]; } } g[0] = invg[0] = 1; for(int i = 1; i < N; i++) { g[i] = 1ll * g[i - 1] * g[i] % mod; invg[i] = inv(g[i]); mu[i] = (mu[i] + mu[i - 1] + Mod) % Mod; } } int calc(int n, int m) { int ans = 0; for(int l = 1, r; l <= n; l = r + 1) { r = min(n / (n / l), m / (m / l)); ans = (ans + 1ll * (mu[r] - mu[l - 1]) * (n / l) % Mod * (m / l) % Mod + Mod) % Mod; } return ans; } int main() { // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); init(); int T; scanf("%d", &T); while(T--) { int n, m; scanf("%d %d", &n, &m); if(n > m) swap(n, m); int ans = 1; for(int l = 1, r; l <= n; l = r + 1) { r = min(n / (n / l), m / (m / l)); ans = 1ll * ans * quick_pow(1ll * g[r] * invg[l - 1] % mod, calc(n / l, m / l)) % mod; } printf("%d\n", ans); } return 0; }