結果

問題 No.1145 Sums of Powers
ユーザー 👑 kmjpkmjp
提出日時 2020-08-03 19:22:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,127 bytes
コンパイル時間 2,587 ms
コンパイル使用メモリ 215,524 KB
実行使用メモリ 36,356 KB
最終ジャッジ日時 2023-10-11 15:06:25
合計ジャッジ時間 12,061 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,116 KB
testcase_01 AC 4 ms
8,248 KB
testcase_02 AC 19 ms
8,328 KB
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#undef _P
#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 ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------


int N,M;
int A[101010];

const int mo=998244353;
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(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]);
	}
	for(int m=2; m<=n; 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=(ll)w*v[j2]%mo;
				v[j1]=t1+t2;
				v[j2]=t1+mo-t2;
				while(v[j1]>=mo) v[j1]-=mo;
				while(v[j2]>=mo) v[j2]-=mo;
				w=(ll)w*wn%mo;
			}
		}
	}
	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,bool resize=false) {
	if(resize) {
		int maxind=0,pi=0,qi=0,i;
		int s=2;
		FOR(i,P.size()) if(norm(P[i])) pi=i;
		FOR(i,Q.size()) if(norm(Q[i])) qi=i;
		maxind=pi+qi+1;
		while(s*2<maxind) s*=2;
		P.resize(s*2);Q.resize(s*2);
	}
	P=fft(P), Q=fft(Q);
	for(int i=0;i<P.size();i++) P[i]=(ll)P[i]*Q[i]%mo;
	return fft(P,true);
}

template<class T> pair<vector<T>,vector<T>> add(pair<vector<T>,vector<T>> a, pair<vector<T>,vector<T>> b) {
	vector<T> c=MultPoly(a.first,b.second,true);
	vector<T> d=MultPoly(b.first,a.second,true);
	vector<T> e=MultPoly(a.second,b.second,true);
	if(c.size()<d.size()) swap(c,d);
	int i;
	FOR(i,d.size()) {
		c[i]+=d[i];
		if(c[i]>=mo) c[i]-=mo;
	}
	
	return {c,e};
}

template<class T> vector<T> inverse(vector<T> a) {
	assert(a[0]>0);
	vector<T> b={(T)modpow(a[0])};
	while(b.size()<a.size()) {
		vector<T> c(a.begin(),a.begin()+min(a.size(),2*b.size()));
		vector<T> d=MultPoly(b,b,true);
		if(d.size()>a.size()) d.resize(a.size());
		c = MultPoly(c,d,true);
		b.resize(2*b.size());
		int i;
		for(i=b.size()/2;i<b.size();i++) b[i]=(mo-c[i])%mo;
	}
	b.resize(a.size());
	return b;
	
}

pair<vector<int>,vector<int>> PQ[101010];

void solve() {
	int i,j,k,l,x,y; string s;
	
	cin>>N>>M;
	queue<int> Q;
	FOR(i,N) {
		cin>>A[i];
		PQ[i].first={1};
		PQ[i].second={1,(mo-A[i])%mo};
		Q.push(i);
	}
	while(Q.size()>=2) {
		x=Q.front();
		Q.pop();
		y=Q.front();
		Q.pop();
		PQ[x]=add(PQ[x],PQ[y]);
		Q.push(x);
	}
	
	pair<vector<int>,vector<int>> a=PQ[Q.front()];
	vector<int> p=a.first;
	a.second.resize(M+1);
	vector<int> q=inverse(a.second);
	vector<int> r=MultPoly(p,q,true);
	
	for(i=1;i<=M;i++) cout<<r[i]<<" ";
	cout<<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