結果

問題 No.2613 Sum of Combination
ユーザー shobonvipshobonvip
提出日時 2024-01-25 17:19:36
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 266 ms / 4,500 ms
コード長 2,288 bytes
コンパイル時間 3,996 ms
コンパイル使用メモリ 252,520 KB
実行使用メモリ 17,644 KB
最終ジャッジ日時 2024-01-25 17:19:48
合計ジャッジ時間 11,001 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 6 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 1 ms
6,676 KB
testcase_06 AC 1 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 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 9 ms
6,676 KB
testcase_14 AC 8 ms
6,676 KB
testcase_15 AC 6 ms
6,676 KB
testcase_16 AC 8 ms
6,676 KB
testcase_17 AC 9 ms
6,676 KB
testcase_18 AC 8 ms
6,676 KB
testcase_19 AC 9 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 2 ms
6,676 KB
testcase_22 AC 13 ms
6,676 KB
testcase_23 AC 207 ms
16,040 KB
testcase_24 AC 208 ms
16,004 KB
testcase_25 AC 196 ms
14,336 KB
testcase_26 AC 212 ms
17,532 KB
testcase_27 AC 108 ms
10,756 KB
testcase_28 AC 204 ms
17,308 KB
testcase_29 AC 190 ms
16,768 KB
testcase_30 AC 217 ms
17,556 KB
testcase_31 AC 206 ms
16,656 KB
testcase_32 AC 223 ms
16,368 KB
testcase_33 AC 199 ms
17,644 KB
testcase_34 AC 204 ms
17,644 KB
testcase_35 AC 212 ms
17,644 KB
testcase_36 AC 210 ms
17,644 KB
testcase_37 AC 236 ms
17,644 KB
testcase_38 AC 195 ms
17,236 KB
testcase_39 AC 202 ms
17,288 KB
testcase_40 AC 210 ms
17,272 KB
testcase_41 AC 191 ms
17,488 KB
testcase_42 AC 266 ms
17,344 KB
testcase_43 AC 185 ms
17,644 KB
testcase_44 AC 170 ms
17,644 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 2 ms
6,676 KB
testcase_49 AC 2 ms
6,676 KB
testcase_50 AC 169 ms
17,644 KB
testcase_51 AC 174 ms
17,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/modint>
#include<atcoder/math>
#include<atcoder/convolution>
using namespace std;
typedef long long ll;
typedef atcoder::modint998244353 mint;

vector<int> pfact(int n){
	vector<int> ret;
	for (int i=2; i*i<=n; i++){
		if (n % i == 0){
			ret.push_back(i);
			while (n % i == 0){
				n /= i;
			}
		}
	}
	if (n > 1) ret.push_back(n);
	return ret;
}

ll modpow(ll n, ll m, ll p){
	ll ret = 1;
	ll tmp = n;
	while(m > 0){
		if (m & 1){
			ret *= tmp;
			ret %= p;
		}
		tmp *= tmp;
		tmp %= p;
		m >>= 1;
	}
	return ret;
}

bool is_p_root(int x, int p, vector<int> &v){
	for (int m: v){
		if (modpow(x, (p-1)/m, p) == 1) return false;
	}
	return true;
}

int findpr(int p){
	random_device seed_gen;
	mt19937 engine(seed_gen());
	uniform_int_distribution<int> dist(1, p-1);
	vector<int> v = pfact(p-1);
	int x;
	do{
		x = dist(engine);
	}while(!is_p_root(x, p, v));
	return x;
}

int main(){
	ll n; cin >> n;
	int p; cin >> p;

	ll mx = p-1;
	vector<ll> fact(mx + 1, 1);
	vector<ll> factinv(mx + 1, 1);
	fact[0] = 1;
	for (int i=1; i<=mx; i++){
		fact[i] = fact[i-1] * i % p;
	}
	factinv[mx] = modpow(fact[mx], p-2, p);
	for (int i=mx; i>=1; i--){
		factinv[i-1] = factinv[i] * i % p;
	}

	ll g = findpr(p);
	vector<ll> taio(p, -1);
	vector<ll> fuku(p-1, 0);

	{
		ll tmp = 1;
		for (int i=0; i<p-1; i++){
			taio[tmp] = i;
			fuku[i] = tmp;
			tmp *= g;
			tmp %= p;
		}
	}


	vector<ll> a;
	while(n > 0){
		a.push_back(n % p);
		n /= p;
	}
	reverse(a.begin(), a.end());

	auto cmb = [&](ll n, ll r) -> ll {
		if (n < r) return 0;
		return fact[n] * factinv[r] % p * factinv[n-r] % p;
	};

	vector<mint> dp(p-1);
	for (int num=0; num<(int)a.size(); num++){
		vector<mint> ndp(p-1);
		vector<mint> g(p-1);
		for (int i=0; i<=a[num]; i++){
			ll k = taio[cmb(a[num], i)];
			assert(k != -1);
			g[k] += 1;
		}
		
		vector<mint> h = atcoder::convolution(g, dp);
		for (int i=0; i<(int)h.size(); i++){
			ndp[i%(p-1)] += h[i];
		}
		
		if (num > 0){
			for (int i=1; i<=a[num]; i++){
				ndp[taio[cmb(a[num], i)]] += 1;
			}
		}

		for (int i=(num<1); i<a[num]; i++){
			ndp[taio[cmb(a[num], i)]%(p-1)] += 1;
		}

		dp = ndp;
	}

	dp[0] += 2;

	mint ans = 0;
	for (int i=0; i<p-1; i++){
		ans += mint(dp[i]) * fuku[i];
	}
	cout << ans.val() << '\n';	
}
0