結果

問題 No.310 2文字しりとり
ユーザー tko919tko919
提出日時 2020-04-25 00:44:42
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,662 bytes
コンパイル時間 1,589 ms
コンパイル使用メモリ 172,316 KB
実行使用メモリ 129,676 KB
最終ジャッジ日時 2024-04-23 04:29:25
合計ジャッジ時間 5,576 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 18 ms
67,916 KB
testcase_01 AC 19 ms
67,408 KB
testcase_02 AC 19 ms
67,024 KB
testcase_03 AC 18 ms
68,044 KB
testcase_04 AC 19 ms
67,144 KB
testcase_05 AC 19 ms
67,404 KB
testcase_06 AC 19 ms
67,536 KB
testcase_07 AC 18 ms
68,044 KB
testcase_08 AC 18 ms
67,152 KB
testcase_09 AC 19 ms
66,892 KB
testcase_10 AC 19 ms
66,892 KB
testcase_11 AC 19 ms
67,024 KB
testcase_12 AC 19 ms
67,016 KB
testcase_13 AC 18 ms
67,788 KB
testcase_14 AC 18 ms
67,660 KB
testcase_15 AC 19 ms
66,896 KB
testcase_16 AC 19 ms
67,020 KB
testcase_17 AC 18 ms
67,272 KB
testcase_18 AC 20 ms
67,408 KB
testcase_19 AC 20 ms
67,276 KB
testcase_20 AC 20 ms
68,552 KB
testcase_21 AC 286 ms
129,672 KB
testcase_22 AC 342 ms
129,484 KB
testcase_23 AC 641 ms
129,676 KB
testcase_24 WA -
testcase_25 AC 297 ms
129,488 KB
testcase_26 AC 217 ms
84,428 KB
testcase_27 AC 257 ms
91,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

//template
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<class T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

template<unsigned mod=1000000007>struct mint {
   unsigned val;
   static unsigned get_mod(){return mod;}
   unsigned inv() const{
      int tmp,a=val,b=mod,x=1,y=0;
      while(b)tmp=a/b,a-=tmp*b,swap(a,b),x-=tmp*y,swap(x,y);
      if(x<0)x+=mod; return x;
   }
   mint():val(0){}
   mint(ll x):val(x>=0?x%mod:mod+(x%mod)){}
   mint pow(ll t){mint res=1,b=*this; while(t){if(t&1)res*=b;b*=b;t>>=1;}return res;}
   mint& operator+=(const mint& x){if((val+=x.val)>=mod)val-=mod;return *this;}
   mint& operator-=(const mint& x){if((val+=mod-x.val)>=mod)val-=mod; return *this;}
   mint& operator*=(const mint& x){val=ll(val)*x.val%mod; return *this;}
   mint& operator/=(const mint& x){val=ll(val)*x.inv()%mod; return *this;}
   mint operator+(const mint& x)const{return mint(*this)+=x;}
   mint operator-(const mint& x)const{return mint(*this)-=x;}
   mint operator*(const mint& x)const{return mint(*this)*=x;}
   mint operator/(const mint& x)const{return mint(*this)/=x;}
   bool operator==(const mint& x)const{return val==x.val;}
   bool operator!=(const mint& x)const{return val!=x.val;}
}; using Mint=mint<>;
template<typename T>struct factorial {
   vector<T> Fact,Finv,Inv;
   factorial(int maxx){
      Fact.resize(maxx); Finv.resize(maxx); Inv.resize(maxx);
      Fact[0]=Fact[1]=Finv[0]=Finv[1]=Inv[1]=1; unsigned mod=Mint::get_mod();
      rep(i,2,maxx){
         Fact[i]=Fact[i-1]*i;
         Inv[i]=Inv[mod%i]*(mod-mod/i);
         Finv[i]=Finv[i-1]*Inv[i];
      }
   }
   T fact(int n,bool inv=0){if(inv)return Finv[n];else return Fact[n];}
   T inv(int n){return Inv[n];}
   T nPr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[n-r];}
   T nCr(int n,int r){if(n<0||n<r||r<0)return Mint(0);else return Fact[n]*Finv[r]*Finv[n-r];}
};

mt19937 mt(1341398); Mint mat[4010][4010];
Mint det(int n){
   Mint res=1;
   rep(i,0,n){
      vector<int> v; rep(j,i,n)if(mat[j][i]!=0)v.push_back(j);
      if(v.empty())return 0;
      int idx=v[mt()%v.size()];
      if(idx!=i){
         rep(j,0,n)swap(mat[i][j],mat[idx][j]);
         res*=-1;
      } res*=mat[i][i]; Mint inv=mat[i][i].inv();
      rep(j,0,n)mat[i][j]*=inv;
      rep(j,i+1,n)if(mat[j][i]!=0){
         inv=mat[j][i];
         rep(k,i,n)if(mat[i][k]!=0)mat[j][k]-=mat[i][k]*inv;
      }
   } return res;
}


factorial<Mint> fact(8020); int g[4010][4010]={},in[4010],out[4010];
void fail(){puts("0"); exit(0);}

int main(){
   int n,m; scanf("%d%d",&n,&m);
   if(n*n==m){puts("1"); return 0;}
   rep(i,0,n){
      g[i][i]=in[i]=out[i]=n;
      rep(j,0,n)g[i][j]--;
   }
   rep(_,0,m){
      int x,y; scanf("%d%d",&x,&y); x--; y--;
      in[y]--; out[x]--;
      g[x][y]++; g[y][y]--;
   }
   int s=-1,t=-1,N=0,v[4010];
   rep(i,0,n){
      if(in[i]==0 and out[i]==0)continue;
      v[N++]=i;
      if(abs(in[i]-out[i])>1)fail();
      if(out[i]==in[i]+1){
         if(s!=-1)fail(); s=i;
      }
      if(out[i]+1==in[i]){
         if(t!=-1)fail(); t=i;
      }
   }
   Mint res=n*n-m;
   if(s!=-1 and t!=-1){
      in[s]++; out[t]++;
      g[s][t]--; g[s][s]++;
      res=1;
   }
   rep(i,0,N)res*=fact.fact(in[v[i]]-1);
   rep(i,0,N-1)rep(j,0,N-1)mat[i][j]=g[v[i]][v[j]];
   res*=det(N-1); printf("%d\n",res.val);
   return 0;
}
0