bzoj3601 一个人的数论 高斯消元&莫比乌斯反演

bzoj3601 一个人的数论 高斯消元&莫比乌斯反演_第1张图片

AC代码如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#define ll long long
#define mod 1000000007
#define inv(x) ksm(x,1000000005)
using namespace std;

int m,n,a[105][105],p[1005],q[1005];
int ksm(int x,int y){
	int t=1; if (y<0){ x=inv(x); y=-y; }
	for (; y; y>>=1,x=(ll)x*x%mod) if (y&1) t=(ll)t*x%mod;
	return t;
}
void pfs(){
	int i,j,k,tmp;
	for (i=0; i<=m+1; i++){
		a[i][0]=1;
		for (j=1; j<=m+1; j++) a[i][j]=(ll)a[i][j-1]*(i+1)%mod;
		if (i) a[i][m+2]=a[i-1][m+2];
		a[i][m+2]=(a[i][m+2]+a[i][m])%mod;
	}
	for (i=0; i<=m+1; i++){
		for (j=i; j<=m+1; j++) if (a[j][i]) break;
		if (i!=j) swap(a[i],a[j]);
		for (j=i+1; j<=m+1; j++){
			tmp=(ll)a[j][i]*inv(a[i][i])%mod;
			for (k=i; k<=m+2; k++)
				a[j][k]=(a[j][k]+mod-(ll)a[i][k]*tmp%mod)%mod;
		}
	}
	for (i=m+1; i>=0; i--){
		for (j=i+1; j<=m+1; j++)
			a[i][m+2]=(a[i][m+2]+mod-(ll)a[i][j]*a[j][m+2]%mod)%mod;
		a[i][m+2]=(ll)a[i][m+2]*inv(a[i][i])%mod;
	}
}
int main(){
	scanf("%d%d",&m,&n); pfs(); int i,j,tmp,ans=0;
	for (i=1; i<=n; i++) scanf("%d%d",&p[i],&q[i]);
	for (i=0; i<=m+1; i++){
		for (j=tmp=1; j<=n; j++)
			tmp=(ll)(mod+1-ksm(p[j],m-i))%mod*tmp%mod*ksm(p[j],(ll)q[j]*i%(mod-1))%mod;
		ans=(ans+(ll)a[i][m+2]*tmp%mod)%mod;
	}
	printf("%d\n",ans);
	return 0;
}


by lych

2016.4.11

你可能感兴趣的:(数论,高斯消元,莫比乌斯反演)