結果

問題 No.1195 数え上げを愛したい(文字列編)
ユーザー HIR180HIR180
提出日時 2020-08-22 13:04:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 317 ms / 3,000 ms
コード長 4,328 bytes
コンパイル時間 3,921 ms
コンパイル使用メモリ 246,460 KB
実行使用メモリ 18,752 KB
最終ジャッジ日時 2023-08-05 09:42:37
合計ジャッジ時間 10,690 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 279 ms
18,592 KB
testcase_01 AC 278 ms
18,644 KB
testcase_02 AC 279 ms
18,656 KB
testcase_03 AC 122 ms
10,588 KB
testcase_04 AC 122 ms
10,712 KB
testcase_05 AC 317 ms
18,304 KB
testcase_06 AC 48 ms
8,088 KB
testcase_07 AC 47 ms
8,100 KB
testcase_08 AC 77 ms
9,212 KB
testcase_09 AC 276 ms
18,752 KB
testcase_10 AC 161 ms
13,456 KB
testcase_11 AC 245 ms
17,988 KB
testcase_12 AC 250 ms
18,044 KB
testcase_13 AC 182 ms
13,244 KB
testcase_14 AC 147 ms
12,968 KB
testcase_15 AC 163 ms
13,484 KB
testcase_16 AC 163 ms
13,360 KB
testcase_17 AC 81 ms
9,120 KB
testcase_18 AC 253 ms
17,900 KB
testcase_19 AC 255 ms
17,908 KB
testcase_20 AC 184 ms
13,664 KB
testcase_21 AC 256 ms
18,028 KB
testcase_22 AC 166 ms
13,540 KB
testcase_23 AC 48 ms
8,048 KB
testcase_24 AC 47 ms
8,068 KB
testcase_25 AC 47 ms
8,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Let's join Kaede Takagaki Fan Club !!
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
	rep(i,a.size()) cout << a[i] << " ";
	cout << endl;
}
template<class T>
bool chmax(T&a, T b){
	if(a < b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a, T b){
	if(a > b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const ll mod = 998244353;
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}

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);
	}
};

ntt<998244353>kaede;

string s;
int cnt[26];
vector<int>vec[26];
ll modpow(ll x,ll n){
	ll res=1;
	while(n>0){
		if(n&1) res=res*x%mod;
		x=x*x%mod;
		n>>=1;
	}
	return res;
}
ll F[300005],R[300005];
void make(){
	F[0] = 1;
	for(int i=1;i<300005;i++) F[i] = F[i-1]*i%mod;
	for(int i=0;i<300005;i++) R[i] = modpow(F[i],mod-2);
}
ll C(int a,int b){
	return F[a]*R[b]%mod*R[a-b]%mod;
}

vector<int>calc(int a, int b){
	if(a == b) return vec[a];
	auto L = calc(a, (a+b)/2);
	auto R = calc((a+b)/2+1, b);
	return kaede.multiply(L, R);
}
int main(){
	make();
	cin >> s;
	rep(i, s.size()) cnt[s[i]-'a']++;
	rep(i, 26){
		rep(j, cnt[i]+1){
			vec[i].pb((int)(R[j]));
		}
	}
	auto ret = calc(0, 25);
	ll ans = 0;
	rep(i, ret.size()){
		ans += (ll)(ret[i]) * F[i] % mod;
	}
	ans += mod-1;
	cout << ans%mod << endl;
}
0