結果

問題 No.1195 数え上げを愛したい(文字列編)
ユーザー 👑 NachiaNachia
提出日時 2020-11-17 16:15:00
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 487 ms / 3,000 ms
コード長 3,559 bytes
コンパイル時間 1,823 ms
コンパイル使用メモリ 179,972 KB
実行使用メモリ 34,468 KB
最終ジャッジ日時 2023-09-30 14:18:24
合計ジャッジ時間 10,704 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 483 ms
33,552 KB
testcase_01 AC 486 ms
34,468 KB
testcase_02 AC 487 ms
34,136 KB
testcase_03 AC 110 ms
14,828 KB
testcase_04 AC 123 ms
16,240 KB
testcase_05 AC 171 ms
21,204 KB
testcase_06 AC 10 ms
10,524 KB
testcase_07 AC 11 ms
10,424 KB
testcase_08 AC 80 ms
14,064 KB
testcase_09 AC 484 ms
32,408 KB
testcase_10 AC 216 ms
21,936 KB
testcase_11 AC 469 ms
32,192 KB
testcase_12 AC 465 ms
31,840 KB
testcase_13 AC 369 ms
25,656 KB
testcase_14 AC 210 ms
20,724 KB
testcase_15 AC 216 ms
22,084 KB
testcase_16 AC 216 ms
22,180 KB
testcase_17 AC 80 ms
14,508 KB
testcase_18 AC 466 ms
31,564 KB
testcase_19 AC 459 ms
32,204 KB
testcase_20 AC 368 ms
25,620 KB
testcase_21 AC 464 ms
31,564 KB
testcase_22 AC 233 ms
23,272 KB
testcase_23 AC 10 ms
10,544 KB
testcase_24 AC 11 ms
10,404 KB
testcase_25 AC 11 ms
10,592 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using LL = long long;
using ULL = unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

template<ULL M>
struct static_modint {
	ULL x;
	static_modint(ULL val = 0) : x(val) {}
	template<class intTy, enable_if_t<is_integral<intTy>::value&& is_unsigned<intTy>::value, void*> isUnsignedInt = nullptr>
	static static_modint mod_construct(intTy val) { return static_modint(val % M); }
	template<class intTy, enable_if_t<is_integral<intTy>::value&& is_signed<intTy>::value, void*> isSignedInt = nullptr>
	static static_modint mod_construct(intTy val) { LL buf = val % (LL)M; if (buf < 0) buf += M; return static_modint((ULL)buf); }

	static_modint operator-() const { if (x == 0) return 0; else return M - x; }
	static_modint& operator+=(static_modint r) { x += r.x; if (x >= M) x -= M; return *this; }
	static_modint operator+(static_modint r) const { static_modint res = x; return res += r; }
	static_modint& operator-=(static_modint r) { x += M - r.x; if (x >= M) x -= M; return *this; }
	static_modint operator-(static_modint r) const { static_modint res = x; return res -= r; }
	static_modint& operator*=(static_modint r) { x = x * r.x % M; return *this; }
	static_modint operator*(static_modint r) const { return static_modint(x * r.x % M); }
	static_modint pow(ULL r) const {
		if (r == 0) return static_modint(1);
		static_modint res = pow(r / 2);
		res *= res;
		if (r % 2) res *= *this;
		return res;
	}
	static_modint inv() const { return pow(M - 2); }
	static_modint& operator/=(static_modint r) { *this *= r.inv(); return *this; }
	static_modint operator/(static_modint r) const { return *this * r.inv(); }
	ULL& operator*() { return x; }
	const ULL& operator*() const { return x; }
	bool operator==(static_modint r) const { return x == *r; }
	bool operator!=(static_modint r) const { return x != *r; }
};

const ULL M = 998244353;
using MLL = static_modint<M>;

void NTT(vector<MLL>& A,MLL g){
  int N=A.size();
  for(int i=0,j=0; j<N; j++){
    if(i<j) swap(A[i],A[j]);
    for(int k=N>>1; k>(i^=k); k>>=1);
  }
  for(int i=1; i<N; i<<=1){
    MLL q=g.pow((M-1)/i/2), qj=1;
    rep(j,i){
      for(int k=j; k<N; k+=i*2){
        MLL l=A[k],r=A[k+i]*qj;
        A[k]=(l+r);
        A[k+i]=(l+M-r);
      }
      qj*=q;
    }
  }
}

struct Fuse{ ULL M,G; };

vector<MLL> convolution(const vector<MLL>& A,const vector<MLL>& B) {
  const MLL g=3;
  int Z=1; while(Z<A.size()+B.size()) Z<<=1;
  vector<MLL> Ax(Z),Bx(Z);
  MLL iZ=MLL(Z).inv();
  rep(i,Z) Ax[i]=Bx[i]=0;
  rep(i,A.size()) Ax[i]=A[i];
  rep(i,B.size()) Bx[i]=B[i];
  NTT(Ax,g);
  NTT(Bx,g);
  rep(i,Z) Ax[i]=Ax[i]*Bx[i];
  NTT(Ax,g.inv());
  rep(i,Z) Ax[i]=Ax[i]*iZ;
  Ax.resize(A.size()+B.size()-1);
  return move(Ax);
}

const int Z = 300000;
MLL F[Z+1], I[Z+1], iF[Z+1];

vector<MLL> A[26];

int main() {
  rep(i,26) A[i] = {1};
  {
    string S; cin>>S;
    for(int c:S) A[c-'a'].push_back(1);
  }

  F[0]=I[1]=iF[0]=1;
  for(int i=1; i<=Z; i++) F[i]=F[i-1]*i;
  for(int i=2; i<=Z; i++) I[i]=-MLL(M/i)*I[M%i];
  for(int i=1; i<=Z; i++) iF[i]=iF[i-1]*I[i];

  rep(c,26) rep(i,A[c].size()) A[c][i] *= iF[i];

  priority_queue<pair<int,int>> Q;
  rep(i,26) Q.push({ -(int)A[i].size(), i });
  while(Q.size()>=2){
    int p1=Q.top().second; Q.pop();
    int p2=Q.top().second; Q.pop();
    A[p1]=convolution(A[p1],A[p2]);
    Q.push({-(int)A[p1].size(),p1});
  }

  int p=Q.top().second; Q.pop();
  rep(i,A[p].size()) A[p][i] *= F[i];
  MLL ans = 0;
  for(int i=1; i<A[p].size(); i++) ans += A[p][i];
  cout<<*ans<<endl;
  
  return 0;
}
0