結果

問題 No.502 階乗を計算するだけ
ユーザー shobonvipshobonvip
提出日時 2023-06-11 04:43:34
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 4,228 bytes
コンパイル時間 5,485 ms
コンパイル使用メモリ 288,284 KB
実行使用メモリ 13,648 KB
最終ジャッジ日時 2024-06-11 00:48:07
合計ジャッジ時間 37,857 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 615 ms
12,396 KB
testcase_01 AC 617 ms
12,524 KB
testcase_02 AC 608 ms
12,400 KB
testcase_03 AC 583 ms
12,396 KB
testcase_04 AC 593 ms
12,436 KB
testcase_05 AC 594 ms
12,264 KB
testcase_06 AC 586 ms
12,396 KB
testcase_07 AC 598 ms
12,524 KB
testcase_08 AC 592 ms
12,396 KB
testcase_09 AC 615 ms
12,264 KB
testcase_10 AC 619 ms
12,272 KB
testcase_11 AC 582 ms
12,376 KB
testcase_12 AC 588 ms
12,396 KB
testcase_13 AC 596 ms
12,396 KB
testcase_14 AC 595 ms
12,396 KB
testcase_15 AC 588 ms
12,528 KB
testcase_16 AC 591 ms
12,396 KB
testcase_17 AC 584 ms
12,520 KB
testcase_18 AC 583 ms
12,308 KB
testcase_19 AC 599 ms
12,364 KB
testcase_20 AC 591 ms
12,396 KB
testcase_21 AC 596 ms
12,396 KB
testcase_22 AC 617 ms
12,392 KB
testcase_23 AC 595 ms
12,392 KB
testcase_24 AC 613 ms
12,396 KB
testcase_25 AC 598 ms
12,400 KB
testcase_26 AC 589 ms
12,392 KB
testcase_27 AC 607 ms
12,396 KB
testcase_28 AC 607 ms
12,400 KB
testcase_29 AC 613 ms
12,396 KB
testcase_30 AC 614 ms
12,396 KB
testcase_31 AC 631 ms
12,392 KB
testcase_32 TLE -
testcase_33 TLE -
testcase_34 AC 973 ms
12,480 KB
testcase_35 AC 774 ms
12,136 KB
testcase_36 TLE -
testcase_37 TLE -
testcase_38 TLE -
testcase_39 TLE -
testcase_40 AC 692 ms
12,264 KB
testcase_41 TLE -
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 2 ms
5,376 KB
testcase_44 AC 1 ms
5,376 KB
testcase_45 AC 2 ms
5,376 KB
testcase_46 AC 2 ms
5,376 KB
testcase_47 AC 2 ms
5,376 KB
testcase_48 AC 2 ms
5,376 KB
testcase_49 AC 1 ms
5,376 KB
testcase_50 AC 2 ms
5,376 KB
testcase_51 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;

typedef modint1000000007 mint;
typedef long long ll;

mint Garner(vector<long long> m,vector<long long> r){
	int n = m.size();
	vector<long long> t(n);
	for(int i=0; i<n; i++){
		long long cur = 0LL;
		long long cm = 1LL;
		for(int j=0; j<i; j++){
			cur += t[j] * cm;
			cur %= m[i];
			cm *= m[j];
			cm %= m[i];
		}
		cur = r[i] - cur;
		cur %= m[i];
		if(cur<0)cur += m[i];
		if(i==1)cur *= 416537774;
		if(i==2)cur *= 429847628;
		
		//cur *= inv_mod(cm,m[i]);
		
		cur %= m[i];
		t[i] = cur;
	}
	
	mint ret = 0;
	mint cm = 1;
	for(int i=0; i<n; i++){
		ret += cm * t[i];
		cm *= m[i];
	}
	return ret;
	
}

template <class T>
vector<T> convolution_mint(vector<T> a,vector<T> b){
	
	static constexpr unsigned long long M0 = 998244353;
	static constexpr unsigned long long M1 = 754974721;
	static constexpr unsigned long long M2 = 469762049;
	
	vector<long long> aa(a.size()),bb(b.size());
	for(int i=0; i<a.size(); i++)aa[i] = a[i].val();
	for(int i=0; i<b.size(); i++)bb[i] = b[i].val();
	
	auto c0 = convolution<M0>(aa,bb);
	auto c1 = convolution<M1>(aa,bb);
	auto c2 = convolution<M2>(aa,bb);
	
	vector<mint> ret(c0.size());
	vector<long long> m = {M0,M1,M2};
	for(int i=0; i<c0.size(); i++){
		vector<long long> r = {c0[i],c1[i],c2[i]};
		vector<long long> t(3);
		for(int j=0; j<3; j++){
			long long cur = 0LL;
			long long cm = 1LL;
			for(int k=0; k<j; k++){
				cur += t[k] * cm;
				cur %= m[j];
				cm *= m[k];
				cm %= m[j];
			}
			cur = r[j] - cur;
			cur %= m[j];
			if(cur<0)cur += m[j];
			if(j==1)cur *= 416537774;
			if(j==2)cur *= 429847628;
			cur %= m[j];
			t[j] = cur;
		}
		
		mint cm = 1;
		for(int j=0; j<3; j++){
			ret[i] += cm * t[j];
			cm *= m[j];
		}
	}
	
	return ret;
}

vector<mint> poly_inv(vector<mint> &a, int M = -314159265){
	if (M == -314159265) M = (int)a.size();
	else if (M <= 0) return {};
	int n = a.size();
	mint r = a[0].pow(mint::mod()-2);
	int m = 1;
	vector<mint> res = {r};
	while (m < M){
		vector<mint> f = a;
		f.resize(min(n, 2*m));
		vector<mint> h = convolution_mint(f, res);
		for (int i=0; i<min(2*m, (int)h.size()); i++){
			h[i] = -h[i];
		}
		h[0] += 2;
		h.resize(2*m);
		h = convolution_mint(h, res);
		h.resize(2*m);
		swap(res, h);
		m <<= 1;
	}
	res.resize(M);
	return res;
}


int ceil_pow2(int n) {
	int x = 0;
	while ((1U << x) < (unsigned int)(n)) x++;
	return x;
}

vector<mint> multi_eval(vector<mint> x, vector<mint> a){
	int n = x.size();
	int siz = 1 << ceil_pow2(n);
	vector<vector<mint>> g(2*siz, vector<mint>{1});
	for (int i=0; i<n; i++) g[i + siz] = {-x[i], 1};
	for (int i=siz-1; i>0; i--) g[i] = convolution_mint(g[2*i], g[2*i+1]);
	vector<mint> f;
	for (int i=1; i<2*siz; i++){
		if (i==1) f = a;
		else f = g[i>>1];
		int fs = f.size(), gs = g[i].size();
		int m = fs - gs + 1;
		vector<mint> v = {}, w = {};
		if (m > 0){
			vector<mint> ft(m);
			for (int j=0; j<m; j++) ft[j] = f[fs-1-j];
			vector<mint> gt(gs);
			for (int j=0; j<gs; j++) gt[j] = g[i][gs-1-j];
			v = convolution_mint(ft, poly_inv(gt, m));
			v.resize(m);
			reverse(v.begin(), v.end());
			w = convolution_mint(v, g[i]);
		}
		g[i] = f;
		for (int j=0; j<w.size(); j++){
			g[i][j] -= w[j];
		}
		while (g[i].size() > 1 && g[i][g[i].size() - 1] == 0){
			g[i].pop_back();
		}
	}
	vector<mint> ret(n);
	for (int i=0; i<n; i++){
		ret[i] = g[i+siz][0];
	}
	return ret;
}

int main(){
	ll n; cin >> n;
	if (n >= mint::mod()){
		cout << 0 << endl;
		return 0;
	}

	clock_t start = clock();

	int m = 61200;
	deque<vector<mint>> que;
	for (int i=0; i<m; i++){
		que.push_back({i+1, 1});
	}
	while(que.size() > 1){
		vector<mint> f = que.front(); que.pop_front();
		vector<mint> g = que.front(); que.pop_front();
		que.push_back(convolution_mint(f, g));
	}
	vector<mint> f = que.front();
	vector<mint> g(n/m);
	for (int i=0; i<n/m; i++){
		g[i] = i*m;
	}
	vector<mint> muleval = multi_eval(g, f);
	mint ans = 1;
	for (int i=m*(n/m)+1; i<=n; i++){
		ans *= i;
	}
	for (int i=0; i<n/m; i++){
		ans *= muleval[i];
	}
	cout << ans.val() << endl;

	clock_t end = clock();     // 終了時間
    //std::cout << "duration = " << (double)(end - start) / CLOCKS_PER_SEC << "sec.\n";
}
0