結果

問題 No.1302 Random Tree Score
ユーザー 👑 kmjpkmjp
提出日時 2020-11-28 01:52:41
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,354 ms / 3,000 ms
コード長 2,373 bytes
コンパイル時間 2,608 ms
コンパイル使用メモリ 207,056 KB
実行使用メモリ 10,740 KB
最終ジャッジ日時 2023-10-09 23:18:43
合計ジャッジ時間 28,226 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
10,332 KB
testcase_01 AC 111 ms
9,644 KB
testcase_02 AC 1,676 ms
10,672 KB
testcase_03 AC 2,050 ms
10,596 KB
testcase_04 AC 1,588 ms
10,608 KB
testcase_05 AC 2,354 ms
10,604 KB
testcase_06 AC 1,849 ms
10,696 KB
testcase_07 AC 1,515 ms
10,432 KB
testcase_08 AC 2,307 ms
10,484 KB
testcase_09 AC 1,749 ms
10,608 KB
testcase_10 AC 1,903 ms
10,428 KB
testcase_11 AC 1,487 ms
10,424 KB
testcase_12 AC 2,170 ms
10,660 KB
testcase_13 AC 111 ms
9,244 KB
testcase_14 AC 1,750 ms
10,572 KB
testcase_15 AC 2,092 ms
10,740 KB
testcase_16 AC 56 ms
9,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
const int mo=998244353;
const int NUM_=100201;
static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];

ll modpow(ll a, ll n = mo-2) {
	ll r=1;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}


template<class T> vector<T> fft(vector<T> v, bool rev=false) {
	int n=v.size(),i,j,m;
	for(int m=n; m>=2; m/=2) {
		T wn=modpow(5,(mo-1)/m);
		if(rev) wn=modpow(wn);
		for(i=0;i<n;i+=m) {
			T w=1;
			for(int j1=i,j2=i+m/2;j2<i+m;j1++,j2++) {
				T t1=v[j1],t2=v[j2];
				v[j1]=t1+t2;
				v[j2]=ll(t1+mo-t2)*w%mo;
				while(v[j1]>=mo) v[j1]-=mo;
				//while(v[j2]>=mo) v[j2]-=mo;
				w=(ll)w*wn%mo;
			}
		}
	}
	for(i=0,j=1;j<n-1;j++) {
		for(int k=n>>1;k>(i^=k);k>>=1);
		if(i>j) swap(v[i],v[j]);
	}
	/*
	if(rev) {
		ll rv = modpow(n);
		FOR(i,n) v[i]=(ll)v[i]*rv%mo;
	}
	*/
	return v;
}
template<class T> vector<T> MultPoly(vector<T> P,vector<T> Q) {
	P=fft(P), Q=fft(Q);
	for(int i=0;i<P.size();i++) P[i]=(ll)P[i]*Q[i]%mo;
	P=fft(P,true);
	ll rv=modpow(P.size());
	for(int i=0;i<P.size();i++) {
		if(i<N) P[i]=rv*P[i]%mo;
		else P[i]=0;
	}
	return P;
}


void solve() {
	int i,j,k,l,r,x,y; string s;
	inv[1]=fact[0]=factr[0]=1;
	for (int i=2;i<=NUM_;++i) inv[i] = inv[mo % i] * (mo - mo / i) % mo;
	for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mo, factr[i]=factr[i-1]*inv[i]%mo;
	
	cin>>N;
	vector<int> A(1<<18),R;
	FOR(i,N+1) A[i]=((i+1)*factr[i]%mo);
	x=N;
	while(x) {
		if(x%2==1) {
			if(R.empty()) R=A;
			else R=MultPoly(R,A);
		}
		x/=2;
		if(x==0) break;
		A=MultPoly(A,A);
	}
	ll ret=R[N-2]*fact[N-2]%mo*modpow(modpow(N,N-2))%mo;
	cout<<ret<<endl;
	
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0