結果

問題 No.2613 Sum of Combination
ユーザー 沙耶花沙耶花
提出日時 2024-01-19 22:38:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 565 ms / 4,500 ms
コード長 2,244 bytes
コンパイル時間 4,814 ms
コンパイル使用メモリ 280,196 KB
実行使用メモリ 19,220 KB
最終ジャッジ日時 2024-01-19 22:38:57
合計ジャッジ時間 19,298 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 13 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 3 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 16 ms
6,676 KB
testcase_14 AC 18 ms
6,676 KB
testcase_15 AC 12 ms
6,676 KB
testcase_16 AC 17 ms
6,676 KB
testcase_17 AC 19 ms
6,676 KB
testcase_18 AC 17 ms
6,676 KB
testcase_19 AC 18 ms
6,676 KB
testcase_20 AC 3 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 34 ms
6,676 KB
testcase_23 AC 525 ms
17,680 KB
testcase_24 AC 513 ms
17,636 KB
testcase_25 AC 526 ms
15,956 KB
testcase_26 AC 544 ms
19,096 KB
testcase_27 AC 277 ms
12,644 KB
testcase_28 AC 531 ms
18,964 KB
testcase_29 AC 514 ms
18,328 KB
testcase_30 AC 557 ms
19,108 KB
testcase_31 AC 529 ms
18,216 KB
testcase_32 AC 531 ms
18,092 KB
testcase_33 AC 537 ms
19,220 KB
testcase_34 AC 520 ms
19,220 KB
testcase_35 AC 554 ms
19,220 KB
testcase_36 AC 530 ms
19,220 KB
testcase_37 AC 540 ms
19,220 KB
testcase_38 AC 565 ms
18,864 KB
testcase_39 AC 548 ms
18,932 KB
testcase_40 AC 562 ms
18,916 KB
testcase_41 AC 513 ms
19,040 KB
testcase_42 AC 563 ms
19,020 KB
testcase_43 AC 533 ms
19,220 KB
testcase_44 AC 530 ms
19,220 KB
testcase_45 AC 2 ms
6,676 KB
testcase_46 AC 2 ms
6,676 KB
testcase_47 AC 2 ms
6,676 KB
testcase_48 AC 3 ms
6,676 KB
testcase_49 AC 2 ms
6,676 KB
testcase_50 AC 541 ms
19,220 KB
testcase_51 AC 514 ms
19,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <bits/stdc++.h>
#include <atcoder/all>
using namespace atcoder;
using mint = modint998244353;
using namespace std;
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define Inf32 1000000001
#define Inf64 1000000000000000001
long long N,P;
bool check(long long g){
	vector<bool> f(P,false);
	long long tg = g;
	g = 1;
	rep(i,P-1){
		if(f[g])return false;
		f[g] = true;
		g *= tg;
		g %= P;
	}
	return true;
}
long long frac[200005];
long long ifrac[200005];

long long get(long long n,long long r){
	if(n<r)return 0;
	long long ret = frac[n] * ifrac[n-r] * ifrac[r];
	return ret%P;
}

int main(){
	cin>>N>>P;
	long long g;
	for(long long i=1;true;i=(i+(P/2))%P){
		if(check(i)){
			g = i;
			break;
		}
	}
	vector<int> pos(P);
	pos[1] = 0;
	vector<long long> t(1,1);
	rep(i,P-2){
		t.push_back(t.back()*g%P);
		pos[t.back()] = t.size()-1;
	}
		
	vector<long long> bs;
	while(N!=0){
		bs.push_back(N%P);
		N /= P;
	}
	reverse(bs.begin(),bs.end());
	frac[0] = 1;
	rep(i,P){
		frac[i+1] = frac[i] * (i+1);
		frac[i+1] %= P;
	}
	rep(i,P)ifrac[i] = inv_mod(frac[i],P);
	
	vector dp(2,vector<mint>(P-1,0));
	dp[0][0] = 1;
	rep(i,bs.size()){
		vector ndp(2,vector<mint>(P-1,0));
		{
			vector<mint> x = dp[1];
			vector<mint> y(P);
			rep(j,P){
				int tt = get(bs[i],j);
				if(tt!=0)y[pos[tt]] ++;
			}
			x = convolution(x,y);
			while(x.size()>=P){
				mint tt = x.back();
				x.pop_back();
				x[x.size()%(P-1)] += tt;
			}
			ndp[1] = x;
		}
		{
			vector<mint> x = dp[0];
			vector<mint> y(P);
			rep(j,bs[i]){
				int tt = get(bs[i],j);
				if(tt!=0)y[pos[tt]] ++;
			}
			x = convolution(x,y);
			while(x.size()>=P){
				mint tt = x.back();
				x.pop_back();
				x[x.size()%(P-1)] += tt;
			}
			rep(j,x.size())ndp[1][j] += x[j];
		}
		{
			vector<mint> x = dp[0];
			vector<mint> y(P);
			rep(j,bs[i]+1){
				if(j<bs[i])continue;
				int tt = get(bs[i],j);
				if(tt!=0)y[pos[tt]] ++;
			}
			x = convolution(x,y);
			while(x.size()>=P){
				mint tt = x.back();
				x.pop_back();
				x[x.size()%(P-1)] += tt;
			}
			rep(j,x.size())ndp[0][j] += x[j];
		}
		swap(dp,ndp);
	}
	mint ans = 0;
	rep(i,2){
		rep(j,P-1){
			mint tt = t[j];
			tt *= dp[i][j];
			ans += tt;
			
		}
	}
	cout<<ans.val()<<endl;
	
	return 0;
}
0