結果

問題 No.310 2文字しりとり
ユーザー tko919tko919
提出日時 2020-04-25 00:48:05
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 505 ms / 6,000 ms
コード長 4,596 bytes
コンパイル時間 2,107 ms
コンパイル使用メモリ 180,508 KB
実行使用メモリ 67,072 KB
最終ジャッジ日時 2024-04-23 04:29:43
合計ジャッジ時間 5,862 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 6 ms
5,376 KB
testcase_21 AC 390 ms
66,920 KB
testcase_22 AC 503 ms
66,984 KB
testcase_23 AC 502 ms
66,912 KB
testcase_24 AC 503 ms
67,072 KB
testcase_25 AC 505 ms
67,032 KB
testcase_26 AC 111 ms
20,608 KB
testcase_27 AC 158 ms
27,776 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];}
};

vector<Mint> BM(vector<Mint> a){
   int n=a.size(); vector<Mint> b({1}),c({1}); Mint d=1;
   rep(j,1,n+1){
      int l=c.size(),m=b.size();
      Mint x=0; rep(i,0,l)x+=c[i]*a[j-l+i];
      b.push_back(0); m++;
      if(x==0)continue; Mint coeff=x/d*-1;
      if(l<m){
         auto tmp=c; c.insert(c.begin(),m-l,0);
         rep(i,0,m)c[m-1-i]+=coeff*b[m-1-i];
         b=tmp; d=x;
      }
      else rep(i,0,m)c[l-1-i]+=coeff*b[m-1-i];
   } return c;
}

typedef pair<Mint,pair<int,int>> T;
factorial<Mint> fact(8020); mt19937 mt(1341398);
vector<T> mat;

vector<Mint> make(int n){
   vector<Mint> res(n);
   rep(i,0,n)res[i]=mt()%(Mint::get_mod()-1)+1;
   return res;
}
Mint det(int n){
   if(n<=0)return 1;
   while(1){
      vector<Mint> v=make(n),m=make(n),r=make(n);
      vector<T> sub=mat;
      rep(i,0,mat.size()){
         int x=mat[i].second.first,y=mat[i].second.second;
         sub[i].first*=v[y];
      }
      vector<Mint> w(n*2);
      rep(i,0,n*2){
         rep(j,0,n)w[i]+=m[j]*r[j];
         vector<Mint> nxt(n); Mint c;
         rep(j,0,n)c-=m[j]*v[j]; rep(j,0,n)nxt[j]=c;
         rep(j,0,sub.size()){
            int x=sub[j].second.first,y=sub[j].second.second;
            nxt[x]+=m[y]*(sub[j].first+v[y]);
         } swap(m,nxt);
      }
      auto ret=BM(w); if(ret[0]==0)return 0;
      if(ret.size()!=n+1)continue;
      Mint res=ret[0]; if(n&1)res*=-1;
      rep(i,0,n)res/=v[i]; return res;
   }
}

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[t][s]--; 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)if(g[v[i]][v[j]]!=-1 or i==j){
      mat.push_back({Mint(g[v[i]][v[j]]),{i,j}});
   }
   res*=det(N-1); printf("%d\n",res.val);
   return 0;
}
0