天天看點

Another Filling the Grid

​​E. Another Filling the Grid​​

參考:Codeforces Round #589 (Div. 2)-E. Another Filling the Grid-容斥定理

容斥這個東西可以了解,但是運用到實際的時候,還是覺得有點迷迷糊糊的,不知道套公式會不會是一種可行的辦法。

是時候也得把以前的知識溫習一下了....

具體的思路看參考的部落格就可以了解了。

代碼:

// Created by CAD on 2019/10/2.
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int mod=1e9+7;
ll qpow(ll x,ll n)
{
    ll re=1;
    while(n)
    {
        if(n&1) re=(re*x)%mod;
        n>>=1,x=(x*x)%mod;
    }
    return re;
}
ll c[300][300];
int main()
{
    ll n,k; cin>>n>>k;
    for(int i=0;i<=250;++i)
    {
        c[i][0]=c[i][i]=1;
        for(int j=1;j<i;++j)
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod;
    }
    ll ans=0;
    for(int i=0;i<=n;++i)
        for(int j=0;j<=n;++j)
        {
            ans=(ans+((i+j)&1?-1:1)*c[n][i]%mod*c[n][j]%mod*
                    qpow(k-1,n*(i+j)-i*j)%mod*qpow(k,n*n-n*(i+j)+i*j)%mod+mod)%mod;
        }
        cout<<ans<<endl;
}      

繼續閱讀