結果

問題 No.1195 数え上げを愛したい(文字列編)
ユーザー roarisroaris
提出日時 2020-12-07 15:13:27
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 584 ms / 3,000 ms
コード長 3,361 bytes
コンパイル時間 2,465 ms
コンパイル使用メモリ 212,016 KB
実行使用メモリ 16,976 KB
最終ジャッジ日時 2023-10-17 16:31:09
合計ジャッジ時間 11,531 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 584 ms
16,844 KB
testcase_01 AC 581 ms
16,844 KB
testcase_02 AC 574 ms
16,844 KB
testcase_03 AC 62 ms
12,300 KB
testcase_04 AC 72 ms
12,316 KB
testcase_05 AC 67 ms
16,976 KB
testcase_06 AC 9 ms
10,680 KB
testcase_07 AC 9 ms
10,680 KB
testcase_08 AC 104 ms
11,176 KB
testcase_09 AC 580 ms
16,840 KB
testcase_10 AC 343 ms
13,512 KB
testcase_11 AC 513 ms
16,824 KB
testcase_12 AC 469 ms
16,808 KB
testcase_13 AC 388 ms
13,624 KB
testcase_14 AC 237 ms
13,488 KB
testcase_15 AC 304 ms
13,508 KB
testcase_16 AC 260 ms
13,492 KB
testcase_17 AC 103 ms
11,176 KB
testcase_18 AC 482 ms
16,808 KB
testcase_19 AC 471 ms
16,808 KB
testcase_20 AC 401 ms
13,628 KB
testcase_21 AC 510 ms
16,820 KB
testcase_22 AC 387 ms
13,576 KB
testcase_23 AC 9 ms
10,680 KB
testcase_24 AC 9 ms
10,680 KB
testcase_25 AC 10 ms
10,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i=0; i<n; i++)
#define pb push_back
typedef long long ll;
const int MAX = 300010;
const ll MOD = 998244353;

ll fact[MAX], finv[MAX], inv[MAX];

void Cinit() {
    fact[0] = fact[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i=2; i<MAX; i++) {
        fact[i] = fact[i-1]*i%MOD;
        inv[i] = MOD-inv[MOD%i]*(MOD/i)%MOD;
        finv[i] = finv[i-1]*inv[i]%MOD;
    }
}

//https://yukicoder.me/submissions/536413
template<const int md>
struct NTT{
	inline void add(int &a, int b) { a += b; if(a >= md) a -= md; }
	inline void sub(int &a, int b) { a -= b; if(a < 0) a += md; }
	inline int mul(int a, int b) { return (int)((ll)a*b%md); }
	inline int power(int a, long long b) {
		int res = 1;
		while (b > 0) {
			if (b & 1) res = mul(res, a);
	    	a = mul(a, a);
			b >>= 1;
		}
		return res;
	}
	inline int inv(int a) {
		a %= md;
		if (a < 0) a += md;
		int b = md, u = 0, v = 1;
		while (a) {
			int t = b / a;
			b -= t * a; swap(a, b);
			u -= t * v; swap(u, v);
		}
		assert(b == 1);
		if (u < 0) u += md;
		return u;
	}
	
 	int max_base, root;
	vector<int> dw, idw;
	NTT() {
		int tmp = md - 1;
		max_base = 0;
		while (tmp % 2 == 0) {
			tmp /= 2;
			max_base++;
		}
		root = 2;
		while (power(root, (md-1)>>1) == 1) root++;
		dw.resize(max_base); idw.resize(max_base);
		rep(i, max_base){
			sub(dw[i], power(root, (md-1) >> (i+2)));
			idw[i] = inv(dw[i]);
		}
	}
	void fft(vector<int> &a, bool inv) {
		const int n = a.size();
		assert((n & (n - 1)) == 0);
		assert(__builtin_ctz(n) <= max_base);
		if(!inv){
			for(int m=n;m>>=1;){
				int w = 1;
				for(int s=0,k=0; s<n; s += 2*m){
					for(int i=s, j=s+m ; i < s+m; ++i, ++j) {
						int x = a[i], y = mul(a[j], w);
						a[j] = (x>=y?x-y:x+md-y);
						a[i] = (x+y>=md?x+y-md:x+y);
					}
					w = mul(w, dw[__builtin_ctz(++k)]);
				}
			}
		}
		else{
			for(int m=1;m<n;m*=2){
				int w = 1;
				for(int s=0,k=0; s<n; s += 2*m){
					for(int i=s, j=s+m ; i < s+m; ++i, ++j) {
						int x = a[i], y = a[j];
						a[j] = (x>=y?x-y:x+md-y);
						a[j] = mul(a[j], w);
						a[i] = (x+y>=md?x+y-md:x+y);
					}
					w = mul(w, idw[__builtin_ctz(++k)]);
				}
			}
		}
	}
	vector<int> multiply(vector<int> a, vector<int> b, int eq = 0) {
		int need = a.size() + b.size() - 1;
		int nbase = 0;
		while ((1 << nbase) < need) nbase++;
		int sz = 1 << nbase;
		a.resize(sz);
		b.resize(sz);
		fft(a, 0);
		if (eq) b = a; else fft(b, 0);
		int inv_sz = inv(sz);
		for (int i = 0; i < sz; i++) {
			a[i] = mul(mul(a[i], b[i]), inv_sz);
		}
		fft(a, 1);
		a.resize(need);
		return a;
	}
	vector<int> square(vector<int> a) {
		return multiply(a, a, 1);
	}
};

string S;
int cnt[26];

int main() {
    //ループ変数が被っていないか?
    //制約を確認しているか?
    //変数のtypoがないか?
    cin.tie(0); ios::sync_with_stdio(false);
    cin >> S;
    int N = S.size();
    rep(i, N) cnt[S[i]-'a']++;
    sort(cnt, cnt+26);
    
    NTT<998244353> ntt;
    Cinit();
    vector<int> f = {1};
    
    rep(i, 26) {
        vector<int> g;
        rep(j, cnt[i]+1) g.pb(finv[j]);
        f = ntt.multiply(f, g);
        while (!f.back()) f.pop_back();
    }
    
    ll ans = 0;
    for (int i=1; i<=N; i++) {
        ans += fact[i]*f[i];
        ans %= MOD;
    }
    cout << ans << endl;
}
0