結果
問題 | No.1303 Inconvenient Kingdom |
ユーザー | sigma425 |
提出日時 | 2020-11-28 05:19:34 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 16,292 bytes |
コンパイル時間 | 3,328 ms |
コンパイル使用メモリ | 234,536 KB |
実行使用メモリ | 6,948 KB |
最終ジャッジ日時 | 2024-09-12 22:01:47 |
合計ジャッジ時間 | 10,615 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | RE | - |
testcase_01 | RE | - |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 2 ms
6,944 KB |
testcase_04 | RE | - |
testcase_05 | RE | - |
testcase_06 | RE | - |
testcase_07 | RE | - |
testcase_08 | RE | - |
testcase_09 | AC | 309 ms
6,944 KB |
testcase_10 | AC | 313 ms
6,944 KB |
testcase_11 | AC | 316 ms
6,944 KB |
testcase_12 | AC | 317 ms
6,940 KB |
testcase_13 | AC | 319 ms
6,944 KB |
testcase_14 | AC | 322 ms
6,940 KB |
testcase_15 | AC | 325 ms
6,940 KB |
testcase_16 | AC | 324 ms
6,944 KB |
testcase_17 | AC | 325 ms
6,940 KB |
testcase_18 | AC | 225 ms
6,944 KB |
testcase_19 | AC | 225 ms
6,940 KB |
testcase_20 | AC | 226 ms
6,940 KB |
testcase_21 | AC | 320 ms
6,940 KB |
testcase_22 | AC | 320 ms
6,944 KB |
testcase_23 | AC | 321 ms
6,940 KB |
testcase_24 | AC | 320 ms
6,944 KB |
testcase_25 | AC | 321 ms
6,940 KB |
testcase_26 | AC | 3 ms
6,940 KB |
testcase_27 | AC | 2 ms
6,940 KB |
testcase_28 | RE | - |
testcase_29 | RE | - |
testcase_30 | AC | 2 ms
6,940 KB |
testcase_31 | AC | 2 ms
6,944 KB |
testcase_32 | AC | 2 ms
6,940 KB |
testcase_33 | AC | 2 ms
6,944 KB |
testcase_34 | RE | - |
testcase_35 | AC | 2 ms
6,944 KB |
testcase_36 | AC | 2 ms
6,940 KB |
testcase_37 | AC | 2 ms
6,940 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; using uint = unsigned int; #define rep(i,n) for(int i=0;i<int(n);i++) #define rep1(i,n) for(int i=1;i<=int(n);i++) #define per(i,n) for(int i=int(n)-1;i>=0;i--) #define per1(i,n) for(int i=int(n);i>0;i--) #define all(c) c.begin(),c.end() #define si(x) int(x.size()) #define pb emplace_back #define fs first #define sc second template<class T> using V = vector<T>; template<class T> using VV = vector<vector<T>>; template<class T,class U> void chmax(T& x, U y){if(x<y) x=y;} template<class T,class U> void chmin(T& x, U y){if(y<x) x=y;} template<class T> void mkuni(V<T>& v){sort(all(v));v.erase(unique(all(v)),v.end());} template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){ return o<<"("<<p.fs<<","<<p.sc<<")"; } template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){ o<<"{"; for(const T& v:vc) o<<v<<","; o<<"}"; return o; } constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); } #ifdef LOCAL #define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl void dmpr(ostream& os){os<<endl;} template<class T,class... Args> void dmpr(ostream&os,const T&t,const Args&... args){ os<<t<<" ~ "; dmpr(os,args...); } #define shows(...) cerr << "LINE" << __LINE__ << " : ";dmpr(cerr,##__VA_ARGS__) #define dump(x) cerr << "LINE" << __LINE__ << " : " << #x << " = {"; \ for(auto v: x) cerr << v << ","; cerr << "}" << endl; #else #define show(x) void(0) #define dump(x) void(0) #define shows(...) void(0) #endif template<unsigned int mod_> struct ModInt{ using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr static uint mod = mod_; uint v; ModInt():v(0){} ModInt(ll _v):v(normS(_v%mod+mod)){} explicit operator bool() const {return v!=0;} static uint normS(const uint &x){return (x<mod)?x:x-mod;} // [0 , 2*mod-1] -> [0 , mod-1] static ModInt make(const uint &x){ModInt m; m.v=x; return m;} ModInt operator+(const ModInt& b) const { return make(normS(v+b.v));} ModInt operator-(const ModInt& b) const { return make(normS(v+mod-b.v));} ModInt operator-() const { return make(normS(mod-v)); } ModInt operator*(const ModInt& b) const { return make((ull)v*b.v%mod);} ModInt operator/(const ModInt& b) const { return *this*b.inv();} ModInt& operator+=(const ModInt& b){ return *this=*this+b;} ModInt& operator-=(const ModInt& b){ return *this=*this-b;} ModInt& operator*=(const ModInt& b){ return *this=*this*b;} ModInt& operator/=(const ModInt& b){ return *this=*this/b;} ModInt& operator++(int){ return *this=*this+1;} ModInt& operator--(int){ return *this=*this-1;} ll extgcd(ll a,ll b,ll &x,ll &y) const{ ll p[]={a,1,0},q[]={b,0,1}; while(*q){ ll t=*p/ *q; rep(i,3) swap(p[i]-=t*q[i],q[i]); } if(p[0]<0) rep(i,3) p[i]=-p[i]; x=p[1],y=p[2]; return p[0]; } ModInt inv() const { ll x,y; extgcd(v,mod,x,y); return make(normS(x+mod)); } ModInt pow(ll p) const { if(p<0) return inv().pow(-p); ModInt a = 1; ModInt x = *this; while(p){ if(p&1) a *= x; x *= x; p >>= 1; } return a; } bool operator==(const ModInt& b) const { return v==b.v;} bool operator!=(const ModInt& b) const { return v!=b.v;} friend istream& operator>>(istream &o,ModInt& x){ ll tmp; o>>tmp; x=ModInt(tmp); return o; } friend ostream& operator<<(ostream &o,const ModInt& x){ return o<<x.v;} }; using mint = ModInt<998244353>; //using mint = ModInt<1000000007>; V<mint> fact,ifact,invs; mint Choose(int a,int b){ if(b<0 || a<b) return 0; return fact[a] * ifact[b] * ifact[a-b]; } void InitFact(int N){ //[0,N] N++; fact.resize(N); ifact.resize(N); invs.resize(N); fact[0] = 1; rep1(i,N-1) fact[i] = fact[i-1] * i; ifact[N-1] = fact[N-1].inv(); for(int i=N-2;i>=0;i--) ifact[i] = ifact[i+1] * (i+1); rep1(i,N-1) invs[i] = fact[i-1] * ifact[i]; } bool iszero(mint v){return v==0;} template<class T> struct Matrix{ int H,W; VV<T> a; Matrix() : H(0),W(0){} Matrix(int H,int W) : H(H),W(W),a( VV<T>(H,V<T>(W)) ){} Matrix(const VV<T>& v) : H(v.size()), W(v[0].size()), a(v){} static Matrix E(int n){ Matrix a(n,n); rep(i,n) a[i][i] = 1; return a; } V<T>& operator[](int i){return a[i];} const V<T>& operator[](int i) const {return a[i];} Matrix operator+(const Matrix& r) const { assert(H==r.H && W==r.W); VV<T> v(H,V<T>(W)); rep(i,H) rep(j,W) v[i][j] = a[i][j] + r.a[i][j]; return Matrix(v); } Matrix operator-(const Matrix& r) const { assert(H==r.H && W==r.W); VV<T> v(H,V<T>(W)); rep(i,H) rep(j,W) v[i][j] = a[i][j] - r.a[i][j]; return Matrix(v); } Matrix operator*(const Matrix& r) const { assert(W==r.H); VV<T> v(H,V<T>(r.W)); rep(i,H) rep(k,W) rep(j,r.W) v[i][j] += a[i][k] * r.a[k][j]; return Matrix(v); } Matrix& operator+=(const Matrix& r){return (*this)=(*this)+r;} Matrix& operator-=(const Matrix& r){return (*this)=(*this)-r;} Matrix& operator*=(const Matrix& r){return (*this)=(*this)*r;} Matrix pow(ll p) const { assert(H == W); Matrix a = E(H); Matrix x = *this; while(p){ if(p&1) a *= x; x *= x; p >>= 1; } return a; } friend ostream& operator<<(ostream &o,const Matrix& A){ rep(i,A.H){ rep(j,A.W) o<<A.a[i][j]<<" "; o<<endl; } return o; } /* 副作用がある, 基本的に自分でこれを呼ぶことはない 掃き出し法をする 左からvar列が掃き出す対象で、それより右は同時に値を変更するだけ(e.g. 逆行列は右に単位行列おいてから掃き出す) 行swap, 列swap は行わない rank を返す */ int sweep(int var){ int rank = 0; vector<bool> used(H); rep(j,var){ int i=0; while(i<H && (used[i]||iszero(a[i][j]))) i++; if(i==H) continue; used[i] = true; rank++; T t = a[i][j]; rep(k,W) a[i][k] = a[i][k]/t; rep(k,H) if(k!=i){ T t = a[k][j]; rep(l,W) a[k][l] = a[k][l]-a[i][l]*t; } } return rank; } }; /* determinant */ template<class T> T det(VV<T> a){ const int N = a.size(); assert(N>0 && int(a[0].size()) == N); T ans(1); rep(i,N){ for(int j=i+1;j<N;j++) if(!iszero(a[j][i])){ ans = -ans; swap(a[j],a[i]); break; } if(iszero(a[i][i])) return T(0); ans *= a[i][i]; for(int j=i+1;j<N;j++){ mint w = -a[j][i]/a[i][i]; for(int k=i;k<N;k++) a[j][k] += a[i][k]*w; } } return ans; } struct UnionFind{ vector<int> par,sz; UnionFind(int N){ par.assign(N,0); sz.assign(N,1); rep(i,N) par[i]=i; } int find(int x){ if(par[x]==x) return x; return par[x]=find(par[x]); } bool same(int x,int y){ return find(x)==find(y); } void unite(int x,int y){ x=find(x),y=find(y); if(x==y) return; par[y]=x; sz[x] += sz[y]; } }; int bsr(int x) { return 31 - __builtin_clz(x); } void ntt(bool type, V<mint>& c) { const mint G = 3; //primitive root int N = int(c.size()); int s = bsr(N); assert(1 << s == N); V<mint> a = c, b(N); rep1(i,s){ int W = 1 << (s - i); mint base = G.pow((mint::mod - 1)>>i); if(type) base = base.inv(); mint now = 1; for(int y = 0; y < N / 2; y += W) { for (int x = 0; x < W; x++) { auto l = a[y << 1 | x]; auto r = now * a[y << 1 | x | W]; b[y | x] = l + r; b[y | x | N >> 1] = l - r; } now *= base; } swap(a, b); } c = a; } V<mint> multiply_ntt(const V<mint>& a, const V<mint>& b) { int A = int(a.size()), B = int(b.size()); if (!A || !B) return {}; int lg = 0; while ((1 << lg) < A + B - 1) lg++; int N = 1 << lg; V<mint> ac(N), bc(N); for (int i = 0; i < A; i++) ac[i] = a[i]; for (int i = 0; i < B; i++) bc[i] = b[i]; ntt(false, ac); ntt(false, bc); for (int i = 0; i < N; i++) { ac[i] *= bc[i]; } ntt(true, ac); V<mint> c(A + B - 1); mint iN = mint(N).inv(); for (int i = 0; i < A + B - 1; i++) { c[i] = ac[i] * iN; } return c; } template<class D> struct Poly{ vector<D> v; int size() const{ return v.size();} //deg+1 Poly(){} Poly(vector<D> _v) : v(_v){shrink();} Poly& shrink(){ while(!v.empty()&&v.back()==D(0)) v.pop_back(); return *this; } D& operator[](int i){return v[i];} const D& operator[](int i) const {return v[i];} D at(int i) const{ return (i<size())?v[i]:D(0); } void set(int i,const D& x){ //v[i] := x if(i>=size() && !x) return; while(i>=size()) v.push_back(D(0)); v[i]=x; shrink(); return; } D operator()(D x) const { D res = 0; int n = size(); D a = 1; rep(i,n){ res += a*v[i]; a *= x; } return res; } Poly operator+(const Poly &r) const{ int N=max(size(),r.size()); vector<D> ret(N); rep(i,N) ret[i]=at(i)+r.at(i); return Poly(ret); } Poly operator-(const Poly &r) const{ int N=max(size(),r.size()); vector<D> ret(N); rep(i,N) ret[i]=at(i)-r.at(i); return Poly(ret); } Poly operator-() const{ int N=size(); vector<D> ret(N); rep(i,N) ret[i] = -at(i); return Poly(ret); } Poly operator*(const Poly &r) const{ if(size()==0||r.size()==0) return Poly(); return mul_ntt(r); // FFT or NTT ? } Poly operator*(const D &r) const{ int N=size(); vector<D> ret(N); rep(i,N) ret[i]=v[i]*r; return Poly(ret); } Poly operator/(const D &r) const{ return *this * r.inv(); } Poly operator/(const Poly &y) const{ return div_fast(y); } Poly operator%(const Poly &y) const{ return rem_fast(y); // return rem_naive(y); } Poly operator<<(const int &n) const{ // *=x^n assert(n>=0); int N=size(); vector<D> ret(N+n); rep(i,N) ret[i+n]=v[i]; return Poly(ret); } Poly operator>>(const int &n) const{ // /=x^n assert(n>=0); int N=size(); if(N<=n) return Poly(); vector<D> ret(N-n); rep(i,N-n) ret[i]=v[i+n]; return Poly(ret); } bool operator==(const Poly &y) const{ return v==y.v; } bool operator!=(const Poly &y) const{ return v!=y.v; } Poly& operator+=(const Poly &r) {return *this = *this+r;} Poly& operator-=(const Poly &r) {return *this = *this-r;} Poly& operator*=(const Poly &r) {return *this = *this*r;} Poly& operator*=(const D &r) {return *this = *this*r;} Poly& operator/=(const Poly &r) {return *this = *this/r;} Poly& operator/=(const D &r) {return *this = *this/r;} Poly& operator%=(const Poly &y) {return *this = *this%y;} Poly& operator<<=(const int &n) {return *this = *this<<n;} Poly& operator>>=(const int &n) {return *this = *this>>n;} Poly diff() const { int n = size(); if(n == 0) return Poly(); V<D> u(n-1); rep(i,n-1) u[i] = at(i+1) * (i+1); return Poly(u); } Poly intg() const { int n = size(); V<D> u(n+1); rep(i,n) u[i+1] = at(i) / (i+1); return Poly(u); } Poly pow(long long n, int L) const { // f^n, ignoring x^L,x^{L+1},.. Poly a({1}); Poly x = *this; while(n){ if(n&1){ a *= x; a = a.strip(L); } x *= x; x = x.strip(L); n /= 2; } return a; } /* [x^0~n] exp(f) = 1 + f + f^2 / 2 + f^3 / 6 + .. f(0) should be 0 O((N+n) log n) (N = size()) NTT, -O3 - N = n = 100000 : 200 [ms] - N = n = 200000 : 400 [ms] - N = n = 500000 : 1000 [ms] */ Poly exp(int n) const { assert(at(0) == 0); Poly f({1}), g({1}); for(int i=1;i<=n;i*=2){ g = (g*2 - f*g*g).strip(i); Poly q = (this->diff()).strip(i-1); Poly w = (q + g * (f.diff() - f*q)) .strip(2*i-1); f = (f + f * (*this - w.intg()).strip(2*i)) .strip(2*i); } return f.strip(n+1); } /* [x^0~n] log(f) = log(1-(1-f)) = - (1-f) - (1-f)^2 / 2 - (1-f)^3 / 3 - ... f(0) should be 1 O(n log n) NTT, -O3 1e5 : 140 [ms] 2e5 : 296 [ms] 5e5 : 640 [ms] 1e6 : 1343 [ms] */ Poly log(int n) const { assert(at(0) == 1); auto f = strip(n+1); return (f.diff() * f.inv(n)).strip(n).intg(); } /* [x^0~n] sqrt(f) f(0) should be 1 いや平方剰余なら何でもいいと思うけど探すのがめんどくさいので +- 2通りだけど 定数項が 1 の方 O(n log n) NTT, -O3 1e5 : 234 [ms] 2e5 : 484 [ms] 5e5 : 1000 [ms] 1e6 : 2109 [ms] */ Poly sqrt(int n) const { assert(at(0) == 1); Poly f = strip(n+1); Poly g({1}); for(int i=1; i<=n; i*=2){ g = (g + f.strip(2*i)*g.inv(2*i-1)) / 2; } return g.strip(n+1); } /* [x^0~n] f^-1 = (1-(1-f))^-1 = (1-f) + (1-f)^2 + ... f * f.inv(n) = 1 + x^n * poly f(0) should be non0 O(n log n) */ Poly inv(int n) const { assert(at(0) != 0); Poly f = strip(n+1); Poly g({at(0).inv()}); for(int i=1; i<=n; i*=2){ //need to strip!! g *= (Poly({2}) - f.strip(2*i)*g).strip(2*i); } return g.strip(n+1); } Poly exp_naive(int n) const { assert(at(0) == 0); Poly res; Poly fk({1}); rep(k,n+1){ res += fk; fk *= *this; fk = fk.strip(n+1) / (k+1); } return res; } Poly log_naive(int n) const { assert(at(0) == 1); Poly res; Poly g({1}); rep1(k,n){ g *= (Poly({1}) - *this); g = g.strip(n+1); res -= g / k; } return res; } Poly mul_naive(const Poly &r) const{ int N=size(),M=r.size(); vector<D> ret(N+M-1); rep(i,N) rep(j,M) ret[i+j]+=at(i)*r.at(j); return Poly(ret); } Poly mul_ntt(const Poly &r) const{ return Poly(multiply_ntt(v,r.v)); } Poly mul_fft(const Poly &r) const{ return Poly(multiply_fft(v,r.v)); } Poly div_fast_with_inv(const Poly &inv, int B) const { return (*this * inv)>>(B-1); } Poly div_fast(const Poly &y) const{ if(size()<y.size()) return Poly(); int n = size(); return div_fast_with_inv(y.inv_div(n-1),n); } Poly rem_naive(const Poly &y) const{ Poly x = *this; while(y.size()<=x.size()){ int N=x.size(),M=y.size(); D coef = x.v[N-1]/y.v[M-1]; x -= (y<<(N-M))*coef; } return x; } Poly rem_fast(const Poly &y) const{ return *this - y * div_fast(y); } Poly strip(int n) const { //ignore x^n , x^n+1,... vector<D> res = v; res.resize(min(n,size())); return Poly(res); } Poly rev(int n = -1) const { //ignore x^n ~ -> return x^(n-1) * f(1/x) vector<D> res = v; if(n!=-1) res.resize(n); reverse(all(res)); return Poly(res); } /* f.inv_div(n) = x^n / f f should be non0 O((N+n) log n) for division */ Poly inv_div(int n) const { n++; int d = size() - 1; assert(d != -1); if(n < d) return Poly(); Poly a = rev(); Poly g({at(d).inv()}); for(int i=1; i+d<=n; i*=2){ //need to strip!! g *= (Poly({2})-a.strip(2*i)*g).strip(2*i); } return g.rev(n-d); } friend ostream& operator<<(ostream &o,const Poly& x){ if(x.size()==0) return o<<0; rep(i,x.size()) if(x.v[i]!=D(0)){ o<<x.v[i]<<"x^"<<i; if(i!=x.size()-1) o<<" + "; } return o; } }; Poly<mint> interpolate(V<mint> x, V<mint> y){ assert(x.size() == y.size()); int N = x.size(); Poly<mint> f; rep(i,N){ Poly<mint> g({y[i]}); mint coef = 1; rep(j,N) if(j!=i){ g *= Poly<mint>({-x[j],1}); coef *= (x[i]-x[j]); } g *= coef.inv(); f += g; } return f; } int main(){ cin.tie(0); ios::sync_with_stdio(false); //DON'T USE scanf/printf/puts !! cout << fixed << setprecision(20); int N,M; cin >> N >> M; VV<int> e(N,V<int>(N)); UnionFind UF(N); rep(i,M){ int x,y; cin >> x >> y; x--,y--; e[x][y] = e[y][x] = 1; UF.unite(x,y); } V<int> szs; rep(i,N) if(UF.find(i) == i){ szs.pb(UF.sz[i]); } auto numSpT = [&](VV<int> e){ int n = si(e); V<int> deg(n); rep(i,n) rep(j,n) deg[i] += e[i][j]; Matrix<mint> A(n-1,n-1); rep(i,n-1) rep(j,n-1){ if(i == j){ A[i][j] = deg[i]; }else{ A[i][j] = -e[i][j]; } } return det(A.a); }; if(si(szs) == 1){ V<mint> xs,ys; rep(x,N){ VV<int> ee(N,V<int>(N)); rep(i,N) rep(j,N) if(i != j){ if(e[i][j]) ee[i][j] = 1; else ee[i][j] = x; } xs.pb(x); ys.pb(numSpT(ee)); } auto f = interpolate(xs,ys); cout << 0 << endl; cout << f[0] + f[1] << endl; }else{ V<int> f = szs; sort(all(f)); { int x = f[si(f)-1] + f[si(f)-2]; f.pop_back(); f.pop_back(); f.pb(x); int sqsum = 0; for(int v: f) sqsum += v*v; cout << N*N - sqsum << endl; } mint ans = 1; rep(s,N) if(UF.find(s) == s){ V<int> vs; rep(v,N) if(UF.same(s,v)) vs.pb(v); int n = si(vs); VV<int> ee(n,V<int>(n)); rep(i,n) rep(j,n) ee[i][j] = e[vs[i]][vs[j]]; ans *= numSpT(ee); } sort(all(szs)); int K = si(szs); int way = 0; rep(i,K) rep(j,i) if(szs[i] == szs[K-1] && szs[j] == szs[K-2]) way++; show(szs); show(way); cout << ans * way * szs[K-1] * szs[K-2] << endl; } }