bzoj3288 Mato矩阵 找规律

Description


Mato同学最近正在研究一种矩阵,这种矩阵有n行n列第i行第j列的数为gcd(i,j)。
例如n=5时,矩阵如下:
1 1 1 1 1
1 2 1 2 1
1 1 3 1 1
1 2 1 4 1
1 1 1 1 5
Mato想知道这个矩阵的行列式的值,你能求出来吗?

Solution


惊了这居然是结论题,还以为有什么奇妙的方法
这样一个矩阵消元后对角线上是 φ ,那么行列式就是它们的乘积

Code


#include <stdio.h>
#include <string.h>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)

typedef long long LL;
const int MOD=1000000007;
const int N=2000005;

int phi[N],pri[N];

bool npri[N];

void pre_work(int n) {
    phi[1]=1;
    for (int i=2;i<=n;i++) {
        if (!npri[i]) {
            pri[++pri[0]]=i;
            phi[i]=i-1;
        }
        for (int j=1;j<=pri[0]&&i*pri[j]<=n;j++) {
            npri[i*pri[j]]=1;
            if (i%pri[j]==0) {
                phi[i*pri[j]]=phi[i]*pri[j];
                break;
            }
            phi[i*pri[j]]=phi[i]*(pri[j]-1);
        }
    }
}

int main(void) {
    pre_work(N-5);
    int n; scanf("%d",&n);
    LL ans=1;
    rep(i,1,n) ans=(ans*phi[i])%MOD;
    printf("%lld\n", ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/jpwang8/article/details/80313282