結果
問題 | No.1195 数え上げを愛したい(文字列編) |
ユーザー | fumofumofuni |
提出日時 | 2021-11-21 17:37:57 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,784 ms / 3,000 ms |
コード長 | 5,813 bytes |
コンパイル時間 | 2,683 ms |
コンパイル使用メモリ | 221,044 KB |
実行使用メモリ | 48,612 KB |
最終ジャッジ日時 | 2024-06-23 00:06:49 |
合計ジャッジ時間 | 17,906 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 875 ms
46,668 KB |
testcase_01 | AC | 874 ms
46,748 KB |
testcase_02 | AC | 869 ms
46,716 KB |
testcase_03 | AC | 417 ms
20,060 KB |
testcase_04 | AC | 157 ms
20,712 KB |
testcase_05 | AC | 1,784 ms
43,564 KB |
testcase_06 | AC | 8 ms
8,064 KB |
testcase_07 | AC | 8 ms
8,064 KB |
testcase_08 | AC | 142 ms
13,152 KB |
testcase_09 | AC | 831 ms
48,612 KB |
testcase_10 | AC | 459 ms
27,284 KB |
testcase_11 | AC | 770 ms
47,124 KB |
testcase_12 | AC | 719 ms
44,652 KB |
testcase_13 | AC | 602 ms
28,504 KB |
testcase_14 | AC | 387 ms
25,440 KB |
testcase_15 | AC | 436 ms
26,040 KB |
testcase_16 | AC | 393 ms
25,628 KB |
testcase_17 | AC | 150 ms
13,216 KB |
testcase_18 | AC | 718 ms
42,916 KB |
testcase_19 | AC | 716 ms
44,716 KB |
testcase_20 | AC | 604 ms
27,192 KB |
testcase_21 | AC | 742 ms
45,644 KB |
testcase_22 | AC | 569 ms
27,468 KB |
testcase_23 | AC | 9 ms
8,064 KB |
testcase_24 | AC | 8 ms
8,064 KB |
testcase_25 | AC | 8 ms
7,936 KB |
ソースコード
#include<bits/stdc++.h> using namespace std; #define rep(i,n) for(ll i=0;i<n;i++) #define repl(i,l,r) for(ll i=(l);i<(r);i++) #define per(i,n) for(ll i=(n)-1;i>=0;i--) #define perl(i,r,l) for(ll i=r-1;i>=l;i--) #define fi first #define se second #define pb push_back #define ins insert #define pqueue(x) priority_queue<x,vector<x>,greater<x>> #define all(x) (x).begin(),(x).end() #define CST(x) cout<<fixed<<setprecision(x) #define rev(x) reverse(x); using ll=long long; using vl=vector<ll>; using vvl=vector<vector<ll>>; using pl=pair<ll,ll>; using vpl=vector<pl>; using vvpl=vector<vpl>; const ll MOD=1000000007; const ll MOD9=998244353; const int inf=1e9+10; const ll INF=4e18; const ll dy[8]={-1,0,1,0,1,1,-1,-1}; const ll dx[8]={0,-1,0,1,1,-1,1,-1}; template <typename T> inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template <typename T> inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } const int mod = MOD9; const int max_n = 300015; struct mint { ll x; // typedef long long ll; mint(ll x=0):x((x%mod+mod)%mod){} mint operator-() const { return mint(-x);} mint& operator+=(const mint a) { if ((x += a.x) >= mod) x -= mod; return *this; } mint& operator-=(const mint a) { if ((x += mod-a.x) >= mod) x -= mod; return *this; } mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;} mint operator+(const mint a) const { return mint(*this) += a;} mint operator-(const mint a) const { return mint(*this) -= a;} mint operator*(const mint a) const { return mint(*this) *= a;} mint pow(ll t) const { if (!t) return 1; mint a = pow(t>>1); a *= a; if (t&1) a *= *this; return a; } bool operator==(const mint &p) const { return x == p.x; } bool operator!=(const mint &p) const { return x != p.x; } // for prime mod mint inv() const { return pow(mod-2);} mint& operator/=(const mint a) { return *this *= a.inv();} mint operator/(const mint a) const { return mint(*this) /= a;} }; istream& operator>>(istream& is, mint& a) { return is >> a.x;} ostream& operator<<(ostream& os, const mint& a) { return os << a.x;} using vm=vector<mint>; using vvm=vector<vm>; struct combination { vector<mint> fact, ifact; combination(int n):fact(n+1),ifact(n+1) { assert(n < mod); fact[0] = 1; for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i; ifact[n] = fact[n].inv(); for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i; } mint operator()(int n, int k) { if (k < 0 || k > n) return 0; return fact[n]*ifact[k]*ifact[n-k]; } }comb(max_n); namespace NTT { //MOD9のNTT auto c=NTT::mul(a,b)で受け取り。 std::vector<ll> tmp; size_t sz = 1; inline ll powMod(ll n, ll p, ll m) { ll res = 1; while (p) { if (p & 1) res = res * n % m; n = n * n % m; p >>= 1; } return res; } inline ll invMod(ll n, ll m) { return powMod(n, m - 2, m); } ll extGcd(ll a, ll b, ll &p, ll &q) { if (b == 0) { p = 1; q = 0; return a; } ll d = extGcd(b, a%b, q, p); q -= a/b * p; return d; } pair<ll, ll> ChineseRem(const vector<ll> &b, const vector<ll> &m) { ll r = 0, M = 1; for (int i = 0; i < (int)b.size(); ++i) { ll p, q; ll d = extGcd(M, m[i], p, q); // p is inv of M/d (mod. m[i]/d) if ((b[i] - r) % d != 0) return make_pair(0, -1); ll tmp = (b[i] - r) / d * p % (m[i]/d); r += M * tmp; M *= m[i]/d; } return make_pair((r+M+M)%M, M); } template <ll Mod, ll PrimitiveRoot> struct NTTPart { static std::vector<ll> ntt(std::vector<ll> a, bool inv = false) { size_t mask = sz - 1; size_t p = 0; for (size_t i = sz >> 1; i >= 1; i >>= 1) { auto& cur = (p & 1) ? tmp : a; auto& nex = (p & 1) ? a : tmp; ll e = powMod(PrimitiveRoot, (Mod - 1) / sz * i, Mod); if (inv) e = invMod(e, Mod); ll w = 1; for (size_t j = 0; j < sz; j += i) { for (size_t k = 0; k < i; ++k) { nex[j + k] = (cur[((j << 1) & mask) + k] + w * cur[(((j << 1) + i) & mask) + k]) % Mod; } w = w * e % Mod; } ++p; } if (p & 1) std::swap(a, tmp); if (inv) { ll invSz = invMod(sz, Mod); for (size_t i = 0; i < sz; ++i) a[i] = a[i] * invSz % Mod; } return a; } static std::vector<ll> mul(std::vector<ll> a, std::vector<ll> b) { a = ntt(a); b = ntt(b); for (size_t i = 0; i < sz; ++i) a[i] = a[i] * b[i] % Mod; a = ntt(a, true); return a; } }; std::vector<ll> mul(std::vector<ll> a, std::vector<ll> b) { size_t m = a.size() + b.size() - 1; sz = 1; while (m > sz) sz <<= 1; tmp.resize(sz); a.resize(sz, 0); b.resize(sz, 0); vector<ll> c=NTTPart<998244353,3>::mul(a, b); c.resize(m); return c; } std::vector<ll> mul_ll(std::vector<ll> a, std::vector<ll> b) { size_t m = a.size() + b.size() - 1; sz = 1; while (m > sz) sz <<= 1; tmp.resize(sz); a.resize(sz, 0); b.resize(sz, 0); vector<ll> c=NTTPart<998244353,3>::mul(a, b); vector<ll> d=NTTPart<1224736769,3>::mul(a, b); c.resize(m);d.resize(m); vector<ll> e(m); rep(i,m)e[i]=ChineseRem({c[i],d[i]},{998244353,1224736769}).first; return e; } }; vm conv(vm a,vm b){ ll n=a.size(),m=b.size(); vector<ll> na(n),nb(m); rep(i,n)na[i]=a[i].x; rep(i,m)nb[i]=b[i].x; auto nc=NTT::mul(na,nb); vm c(n+m-1); rep(i,n+m-1)c[i]=nc[i]; return c; } int main(){ string s;cin >> s; vl alp(26); rep(i,s.size())alp[s[i]-'a']++; ll n=s.size(); vm dp;dp.emplace_back(1); rep(i,26){ vm ndp; rep(j,alp[i]+1)ndp.emplace_back(comb.ifact[j]); dp=conv(dp,ndp); } rep(i,n+1)dp[i]*=comb.fact[i]; mint ans=0; rep(i,n+1)ans+=dp[i]; cout << ans-1 << endl; }