結果

問題 No.329 全射
ユーザー btk
提出日時 2015-12-22 19:56:01
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 1,812 bytes
コンパイル時間 1,462 ms
コンパイル使用メモリ 168,056 KB
実行使用メモリ 13,096 KB
最終ジャッジ日時 2024-09-18 18:43:40
合計ジャッジ時間 4,294 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL c[1001][1001];
LL dp[200][1001];
int z[300];
int w[300];
int d[300][300];
const LL MOD=1e9+7;

void init_comb(int n){
    n++;for(int i=0;i<n;i++)c[i][0]=1;
    for(int i=1;i<n;i++)for(int j=1;j<=i;j++)c[i][j]=(c[i-1][j]+c[i-1][j-1])%MOD;
    return;
}

LL pow_mod(LL a,LL n){
    LL res=1,b=1;
    while(n>=b){
        if(n&b)res=(res*a)%MOD;
        a=(a*a)%MOD;b<<=1;
    }return res;
}

inline void calc(int id,int n){
    dp[id][0]=0;
    for(int m=1;m<=n;m++){
        LL tmp=pow_mod(m,n);
        for(int i=1;i<m;i++){
            tmp-=dp[id][i]*c[m][i]%MOD;
            tmp+=(tmp<0)*MOD;
        }
        dp[id][m]=tmp;
    //    printf("%d->%d=%lld\n",n,m,tmp);
    }
}
int main() {
    fill((int*)d,(int*)d+300*300,0);
    int N,M;
    cin>>N>>M;
    for(int i=0;i<N;i++){
        cin>>w[i];
        d[i][i]=w[i];
    }
    for(int i=0;i<M;i++){
        int from,to;
        cin>>from>>to;
        d[from-1][to-1]=min(w[from-1],w[to-1]);
    }
    int sz;
    map<int,int> _z;
    /*init*/
    {

        init_comb(1000);
        for(int i=0;i<N;i++)_z[w[i]]=0;
        int sz=_z.size();
        int x=0;
        for(auto& it : _z){
            z[x]=it.first;
            it.second=x++;
        }
        for(int i=0;i<sz;i++)calc(i,z[i]);
    }
    for(int k = 0; k < N; k++)
    for(int i = 0; i < N; i++)
    for(int j = 0; j < N; j++)
    d[i][j]=max(d[i][j],min(d[i][k],d[k][j]));
    LL res=0;
    for(int i = 0; i < N; i++)for(int j = 0; j < N; j++){
    //    printf("(%d:%d)->(%d)=%d:",i,w[i],j,d[i][j]);
        if(d[i][j]==w[j]){
            res+=dp[_z[w[i]]][d[i][j]];
            res%=MOD;
    //        printf("%lld",dp[_z[w[i]]][d[i][j]]);
        }
        //cout<<endl;
    }
    cout<<res<<endl;
    return 0;
}
0