[SDOI2017]数字表格

[SDOI2017]数字表格

https://ac.nowcoder.com/acm/problem/20391

[SDOI2017]数字表格

图片说明

/*
  Author : lifehappy
*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e6 + 10, mod = 1e9 + 7, Mod = mod - 1;

ll prime[N], mu[N], fib[N], phi[N], inv_fib[N], f[N], cnt;

bool st[N];

ll quick_pow(ll a, int n) {
    ll ans = 1;
    while(n) {
        if(n & 1) ans = ans * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return ans;
}

ll inv(ll a) {
    return quick_pow(a, mod - 2);
}

void init() {
    fib[1] = mu[1] = f[1] = 1;
    for(int i = 2; i < N; i++) {
        f[i] = 1;
        fib[i] = (fib[i - 1] + fib[i - 2]) % mod;
        if(!st[i]) {
            prime[++cnt] = i;
            mu[i] = -1;
        }
        for(int j = 1; j <= cnt && 1ll * i * prime[j] < N; j++) {
            st[i * prime[j]] = 1;
            if(i % prime[j] == 0) {
                break;
            }
            mu[i * prime[j]] = -mu[i];
        }
    }
    for(int i = 1; i < N; i++) {
        inv_fib[i] = inv(fib[i]);
    }
    for(int i = 1; i < N; i++) {
        for(int j = i; j < N; j += i) {
            if(mu[j / i] == 1) {
                f[j] = f[j] * fib[i] % mod;
            }
            else if(mu[j / i] == -1) {
                f[j] = f[j] * inv_fib[i] % mod;
            }
        }
    }
    f[0] = 1;
    for(int i = 1; i < N; i++) {
        f[i] = f[i] * f[i - 1] % mod;
    }
}

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);
        ll ans = 1;
        for(int l = 1, r; l <= n; l = r + 1) {
            r = min(n / (n / l), m / (m / l));
            ans = ans * quick_pow(f[r] * inv(f[l - 1]) % mod, 1ll * (n / l) * (m / l) % Mod) % mod;
        }
        printf("%lld\n", ans);
    }
    return 0;
}
全部评论

相关推荐

11-24 19:04
已编辑
湖南工商大学 Java
点赞 评论 收藏
分享
评论
3
收藏
分享
牛客网
牛客企业服务