結果

問題 No.2613 Sum of Combination
ユーザー 👑 kmjpkmjp
提出日時 2024-01-22 21:53:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,110 bytes
コンパイル時間 4,576 ms
コンパイル使用メモリ 216,120 KB
実行使用メモリ 45,908 KB
最終ジャッジ日時 2024-01-22 21:53:53
合計ジャッジ時間 17,361 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
11,736 KB
testcase_01 AC 3 ms
11,736 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 3 ms
11,736 KB
testcase_05 AC 3 ms
11,736 KB
testcase_06 AC 3 ms
11,736 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
11,736 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
testcase_45 AC 3 ms
11,736 KB
testcase_46 AC 3 ms
11,736 KB
testcase_47 WA -
testcase_48 WA -
testcase_49 WA -
testcase_50 AC 382 ms
31,756 KB
testcase_51 AC 353 ms
31,756 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;}
//-------------------------------------------------------

ll N,P;
const ll mo=998244353;

const int NUM_=2000003;
static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];


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

template <class T> using vec=vector<T>; //using vec=valarray<T>;

template<class T> vec<T> fft(vec<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+mo)%mo;
				v[j2]=ll(t1+mo-t2)*w%mo;
				while(v[j1]>=mo) v[j1]-=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> vec<T> MultPoly(vec<T> P,vec<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;
		
		if(s<=64) { //fastpath
			vec<T> R(s*2);
			for(int x=0;x<=pi;x++) for(int y=0;y<=qi;y++) (R[x+y]+=P[x]*Q[y])%=mo;
			return R;
		}
		vec<T> P2(s*2),Q2(s*2);
		FOR(i,pi+1) P2[i]=P[i];
		FOR(i,qi+1) Q2[i]=Q[i];
		swap(P,P2),swap(Q,Q2);
	}
	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);
}

ll get_root(ll p) {
	if(p==2) return 1;
	for(int i=2;i<p;i++) {
		set<int> S;
		int x=1,j;
		while(1) {
			if(S.size()==p-1) return i;
			if(S.count(x)) break;
			S.insert(x);
			x=x*i%p;
		}
		
	}
	assert(0);
}

ll po[202020],re[202020];

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>P;
	ll Q=get_root(P);
	
	ll cur=1;
	FOR(i,P) {
		po[i]=cur;
		re[cur]=i;
		cur=cur*Q%P;
	}
	
	
	inv[1]=fact[0]=factr[0]=1;
	for (int i=2;i<P;++i) inv[i] = inv[P % i] * (P - P / i) % P;
	for (int i=1;i<P;++i) fact[i]=fact[i-1]*i%P, factr[i]=factr[i-1]*inv[i]%P;
	vector<ll> V={1};
	while(N) {
		vector<ll> W(P);
		ll a=N%P;
		FOR(i,a+1) {
			ll b=fact[a]*factr[i]%P*fact[a-i]%P;
			W[re[b]]++;
		}
		V=MultPoly(V,W,1);
		while(V.size()>=P) {
			x=V.size()-1;
			(V[x-(P-1)]+=V[x])%=mo;
			V.pop_back();
		}
		N/=P;
	}
	ll ret=0;
	FOR(i,V.size()) (ret+=po[i]*V[i])%=mo;
	
	cout<<ret%mo<<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