結果

問題 No.1195 数え上げを愛したい(文字列編)
ユーザー fumofumofunifumofumofuni
提出日時 2021-11-21 17:37:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,841 ms / 3,000 ms
コード長 5,813 bytes
コンパイル時間 2,724 ms
コンパイル使用メモリ 218,408 KB
実行使用メモリ 48,436 KB
最終ジャッジ日時 2023-09-05 03:22:12
合計ジャッジ時間 18,701 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 901 ms
46,520 KB
testcase_01 AC 893 ms
46,476 KB
testcase_02 AC 892 ms
46,596 KB
testcase_03 AC 423 ms
19,844 KB
testcase_04 AC 162 ms
20,440 KB
testcase_05 AC 1,841 ms
43,228 KB
testcase_06 AC 7 ms
7,564 KB
testcase_07 AC 8 ms
7,792 KB
testcase_08 AC 152 ms
13,088 KB
testcase_09 AC 854 ms
48,436 KB
testcase_10 AC 477 ms
27,172 KB
testcase_11 AC 797 ms
47,044 KB
testcase_12 AC 751 ms
44,444 KB
testcase_13 AC 630 ms
27,620 KB
testcase_14 AC 406 ms
25,216 KB
testcase_15 AC 452 ms
26,128 KB
testcase_16 AC 406 ms
25,588 KB
testcase_17 AC 159 ms
13,128 KB
testcase_18 AC 753 ms
42,684 KB
testcase_19 AC 750 ms
44,380 KB
testcase_20 AC 623 ms
27,088 KB
testcase_21 AC 800 ms
45,480 KB
testcase_22 AC 584 ms
27,168 KB
testcase_23 AC 8 ms
7,688 KB
testcase_24 AC 7 ms
7,580 KB
testcase_25 AC 8 ms
7,724 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
} 
0