結果
問題 |
No.3040 Aoiスコア
|
ユーザー |
![]() |
提出日時 | 2025-02-28 22:28:19 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 4 ms / 1,000 ms |
コード長 | 13,054 bytes |
コンパイル時間 | 5,394 ms |
コンパイル使用メモリ | 336,272 KB |
実行使用メモリ | 7,844 KB |
最終ジャッジ日時 | 2025-06-20 20:59:12 |
合計ジャッジ時間 | 6,356 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 26 |
ソースコード
#include <bits/stdc++.h> using namespace std; #if __has_include(<atcoder/all>) #include <atcoder/all> #endif using ll=long long; using ull=unsigned long long; using P=pair<ll,ll>; template<typename T>using minque=priority_queue<T,vector<T>,greater<T>>; template<typename T>bool chmax(T &a,const T &b){return (a<b?(a=b,true):false);} template<typename T>bool chmin(T &a,const T &b){return (a>b?(a=b,true):false);} template<typename T1,typename T2>istream &operator>>(istream &is,pair<T1,T2>&p){is>>p.first>>p.second;return is;} template<typename T1,typename T2,typename T3>istream &operator>>(istream &is,tuple<T1,T2,T3>&a){is>>std::get<0>(a)>>std::get<1>(a)>>std::get<2>(a);return is;} template<typename T>istream &operator>>(istream &is,vector<T> &a){for(auto &i:a)is>>i;return is;} template<typename T1,typename T2>void operator++(pair<T1,T2>&a,int n){a.first++,a.second++;} template<typename T1,typename T2>void operator--(pair<T1,T2>&a,int n){a.first--,a.second--;} template<typename T>void operator++(vector<T>&a,int n){for(auto &i:a)i++;} template<typename T>void operator--(vector<T>&a,int n){for(auto &i:a)i--;} #define overload3(_1,_2,_3,name,...) name #define rep1(i,n) for(int i=0;i<(int)(n);i++) #define rep2(i,l,r) for(int i=(int)(l);i<(int)(r);i++) #define rep(...) overload3(__VA_ARGS__,rep2,rep1)(__VA_ARGS__) #define reps(i,l,r) rep2(i,l,r) #define all(x) x.begin(),x.end() #define pcnt(x) __builtin_popcountll(x) #define fin(x) return cout<<(x)<<'\n',static_cast<void>(0) #define yn(x) cout<<((x)?"Yes\n":"No\n") #define uniq(x) sort(all(x)),x.erase(unique(all(x)),x.end()) ll myceil(ll a,ll b){return (a+b-1)/b;} template<typename T,size_t n,size_t id=0> auto vec(const int (&d)[n],const T &init=T()){ if constexpr (id<n)return vector(d[id],vec<T,n,id+1>(d,init)); else return init; } #ifdef LOCAL #include<debug.h> #else #define debug(...) static_cast<void>(0) #define debugg(...) static_cast<void>(0) template<typename T1,typename T2>ostream &operator<<(ostream &os,const pair<T1,T2>&p){os<<p.first<<' '<<p.second;return os;} #endif struct Timer{ clock_t start; Timer(){ start=clock(); ios::sync_with_stdio(false); cin.tie(nullptr); cout<<fixed<<setprecision(16); } inline double now(){return (double)(clock()-start)/1000;} #ifdef LOCAL ~Timer(){ cerr<<"time:"; cerr<<now(); cerr<<"ms\n"; } #endif }timer; void SOLVE(); int main(){ int testcase=1; //cin>>testcase; for(int i=0;i<testcase;i++){ SOLVE(); } } #include<type_traits> #include<optional> template<typename T> constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>msb(T n){return n==0?-1:31-__builtin_clz(n);} template<typename T> constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>msb(T n){return n==0?-1:63-__builtin_clzll(n);} template<typename T> constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>lsb(T n){return n==0?-1:__builtin_ctz(n);} template<typename T> constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>lsb(T n){return n==0?-1:__builtin_ctzll(n);} template<typename T> constexpr std::enable_if_t<std::is_integral_v<T>,T>floor_pow2(T n){return n==0?0:T(1)<<msb(n);} template<typename T> constexpr std::enable_if_t<std::is_integral_v<T>,T>ceil_pow2(T n){return n<=1?1:T(1)<<(msb(n-1)+1);} constexpr unsigned long long binary_gcd(unsigned long long a,unsigned long long b){ if(a==0||b==0||a==b)return a<b?b:a; int n=lsb(a),m=lsb(b); while(a!=b){ if(a>b)a=(a-b)>>lsb(a-b); else b=(b-a)>>lsb(b-a); } return a<<(n<m?n:m); } struct BarrettReduction{ private: using i64=long long; using u64=unsigned long long; using u32=unsigned int; using u128=__uint128_t; u32 m; u64 im; public: BarrettReduction():m(0),im(0){} BarrettReduction(u32 n):m(n),im(u64(-1)/n+1){} inline i64 quo(u64 x)const{ if(m==1)return x; u64 y=u64((u128(x)*im)>>64); u32 r=x-y*m; return m<=r?y-1:y; } inline u32 rem(u64 x)const{ if(m==1)return 0; u64 y=u64((u128(x)*im)>>64); u32 r=x-y*m; return m<=r?r+m:r; } inline std::pair<u64,u32>quo_rem(u64 x)const{ if(m==0)return std::make_pair(x,0); u64 y=u64((u128(x)*im)>>64); u32 r=x-y*m; return m<=r?std::make_pair(y-1,r+m):std::make_pair(y,r); } inline u32 pow(u32 a,u64 p)const{ u32 res=m!=1; while(p){ if(p&1)res=rem(u64(res)*a); a=rem(u64(a)*a); p>>=1; } return res; } }; namespace prime_impl{ constexpr int table_size=1<<16; bool table[table_size]; struct prime_table_init{ prime_table_init(){ table[0]=table[1]=true; for(int i=2;i<table_size;i++)if(!table[i]){ for(int j=i*2;j<table_size;j+=i)table[j]=true; } } }dummy; } bool isprime(unsigned long long n)noexcept{ if(n<prime_impl::table_size)return !prime_impl::table[n]; if(n%2==0)return false; if(n<(1ull<<31)){ BarrettReduction br(n); unsigned long long d=n-1; while(!(d&1))d>>=1; for(unsigned long long base:{2,7,61}){ unsigned long long t=d; unsigned long long y=1; while(t>0){ if(t&1)y=br.rem(y*base); base=br.rem(base*base); t>>=1; } t=d; while(t!=n-1&&y!=1&&y!=n-1){ y=br.rem(y*y); t<<=1; } if(y!=n-1&&t%2==0)return false; } return true; } unsigned long long d=n-1; int s=0; while(!(d&1))d>>=1,s++; int q=63; while(!(d>>q))q--; unsigned long long r=n; for(int i=0;i<5;i++)r*=2-r*n; auto redc=[&r,&n](__uint128_t x)->unsigned long long { x=(x+__uint128_t((unsigned long long)x*-r)*n)>>64; return x>=n?x-n:x; }; __uint128_t r2=-__uint128_t(n)%n; unsigned long long one=redc(r2); for(unsigned long long base:{2,325,9375,28178,450775,9780504,1795265022}){ if(base%n==0)continue; unsigned long long a=base=redc((base%n)*r2); for(int i=q-1;i>=0;i--){ a=redc(__uint128_t(a)*a); if(d>>i&1)a=redc(__uint128_t(a)*base); } if(a==one)continue; for(int i=1;a!=n-one;i++){ if(i>=s)return false; a=redc(__uint128_t(a)*a); } } return true; } std::vector<unsigned long long>factorize(unsigned long long n)noexcept{ std::vector<unsigned long long>ret; auto div=[](unsigned long long x)noexcept->unsigned long long { unsigned long long r=x; for(int i=0;i<5;i++)r*=2-r*x; unsigned long long r2=-__uint128_t(x)%x; auto redc=[&r,&x](__uint128_t t)->unsigned long long { t=(t+__uint128_t((unsigned long long)t*-r)*x)>>64; return t>=x?t-x:t; }; unsigned long long a=0,b=0; const unsigned long long one=redc(r2); unsigned long long e=one; int m=1ll<<((63-__builtin_clzll(x))>>3); while(true){ unsigned long long ca=a,cb=b; unsigned long long sk=one; for(int i=0;i<m;i++){ a=redc(__uint128_t(a)*a+e); b=redc(__uint128_t(b)*b+e); b=redc(__uint128_t(b)*b+e); unsigned long long c=redc(a),d=redc(b); sk=redc(__uint128_t(sk)*(c>d?c-d:d-c)); } unsigned long long g=binary_gcd(redc(sk),x); if(g>1){ if(g<x)return g; for(int i=0;i<m;i++){ ca=redc(__uint128_t(ca)*ca+e); cb=redc(__uint128_t(cb)*cb+e); cb=redc(__uint128_t(cb)*cb+e); unsigned long long c=redc(ca),d=redc(cb); unsigned long long cg=binary_gcd(c>d?c-d:d-c,x); if(cg>1){ if(cg<x)return cg; else{ e+=one; a=b=0; break; } } } } } }; static unsigned long long st[64]; int p=0; while(!(n&1)){ n>>=1; ret.push_back(2); } if(n==1)return ret; st[p++]=n; while(p){ unsigned long long now=st[--p]; if(isprime(now)){ ret.push_back(now); continue; } unsigned long long d=div(now); st[p++]=d; now/=d; if(now!=1)st[p++]=now; } return ret; } template<typename T> std::enable_if_t<std::is_integral_v<T>,T>carmichael(T n){ auto f=factorize(n); std::sort(f.begin(),f.end()); T res=1; for(int l=0,r=0;l<f.size();l=r){ while(r<f.size()&&f[l]==f[r])r++; if(f[l]==2){ if(r-l==2)res=2; else if(r-l>=3)res=T(1)<<(r-l-2); } else{ T prod=f[l]-1; for(int i=0;i<r-l-1;i++)prod*=f[l]; res=std::lcm(res,prod); } } return res; } constexpr int carmichael_constexpr(int n){ if(n==998244353)return 998244352; if(n==1000000007)return 1000000006; if(n<=1)return n; int res=1; int t=0; while(n%2==0){ n/=2; t++; } if(t==2)res=2; else if(t>=3)res=1<<(t-2); for(int i=3;i*i<=n;i++)if(n%i==0){ int c=0; while(n%i==0){ n/=i; c++; } int prod=i-1; for(int j=0;j<c-1;j++)prod*=i; res=std::lcm(res,prod); } if(n!=1)res=std::lcm(res,n-1); return res; } template<int m> struct mod_int{ private: static constexpr unsigned int umod=static_cast<unsigned int>(m); static constexpr unsigned int car=carmichael_constexpr(m); using uint=unsigned int; using mint=mod_int; uint v; static_assert(m<uint(1)<<31); mint sqrt_impl()const{ if(this->val()<=1)return *this; if constexpr(m%8==1){ mint b=2; while(b.pow((m-1)/2).val()==1)b++; int m2=m-1,e=0; while(m2%2==0)m2>>=1,e++; mint x=this->pow((m2-1)/2); mint y=(*this)*x*x; x*=*this; mint z=b.pow(m2); while(y.val()!=1){ int j=0; mint t=y; while(t.val()!=1)t*=t,j++; z=z.pow(1<<(e-j-1)); x*=z; z*=z; y*=z;e=j; } return x; } else if constexpr(m%8==5){ mint ret=this->pow((m+3)/8); if((ret*ret).val()==this->val())return ret; else return ret*mint::raw(2).pow((m-1)/4); } else{ return this->pow((m+1)/4); } } public: using value_type=uint; mod_int():v(0){} template<typename T,std::enable_if_t<std::is_signed_v<T>,std::nullptr_t> =nullptr> mod_int(T a){ a%=m; if(a<0)v=a+umod; else v=a; } template<typename T,std::enable_if_t<std::is_unsigned_v<T>,std::nullptr_t> =nullptr> mod_int(T a):v(a%umod){} static constexpr mint raw(int a){ mint ret; ret.v=a; return ret; } inline uint val()const{return this->v;} static constexpr int mod(){return m;} inline mint &operator+=(const mint &b){ this->v+=b.v; if(this->v>=umod)this->v-=umod; return *this; } inline mint &operator-=(const mint &b){ this->v-=b.v; if(this->v>=umod)this->v+=umod; return *this; } inline mint &operator*=(const mint &b){ this->v=((unsigned long long)this->v*b.v)%umod; return *this; } inline mint &operator/=(const mint &b){ *this*=b.inv(); return *this; } inline mint operator+()const{return *this;} inline mint operator-()const{return mint()-*this;} friend inline mint operator+(const mint &a,const mint &b){return mint(a)+=b;} friend inline mint operator-(const mint &a,const mint &b){return mint(a)-=b;} friend inline mint operator*(const mint &a,const mint &b){return mint(a)*=b;} friend inline mint operator/(const mint &a,const mint &b){return mint(a)/=b;} friend inline bool operator==(const mint &a,const mint &b){return a.val()==b.val();} friend inline bool operator!=(const mint &a,const mint &b){return !(a==b);} inline mint operator++(int){ mint ret=*this; *this+=1; return ret; } inline mint operator--(int){ mint ret=*this; *this-=1; return ret; } mint pow(long long n)const{ mint ret=mint::raw(1),a(*this); while(n){ if(n&1)ret*=a; a*=a; n>>=1; } return ret; } inline mint inv()const{ assert(this->v!=0); return pow(car-1); } std::optional<mint>sqrt()const{ if(this->val()<=1||this->pow((m-1)/2)==1)return std::make_optional(this->sqrt_impl()); else return std::nullopt; } static constexpr unsigned int order(){return car;} friend std::istream &operator>>(std::istream &is,mint &b){ long long a; is>>a; b=mint(a); return is; } friend std::ostream &operator<<(std::ostream &os,const mint &b){ os<<b.val(); return os; } }; template<int m> struct std::hash<mod_int<m>>{ std::size_t operator()(mod_int<m>x)const{ return std::hash<unsigned int>()(x.val()); } }; using mint998=mod_int<998244353>; using mint107=mod_int<1000000007>; using mint=mint998; void SOLVE(){ int n,m; string s; cin>>n>>m>>s; mint ans=0; vector<vector<int>>g(n); rep(i,m){ int u,v; cin>>u>>v; u--,v--; g[u].push_back(v); g[v].push_back(u); } string aoi="aoi?"; int q=count(all(s),'?'); rep(i,n){ if(s[i]!='?'&&s[i]!='o')continue; int cnt[4]={0,0,0,0}; for(int j:g[i]){ rep(k,4)if(s[j]==aoi[k])cnt[k]++; } int fr=q-(s[i]=='?'); ans+=mint(cnt[0])*cnt[2]*mint(26).pow(fr); if(cnt[3]){ ans+=mint(cnt[0])*cnt[3]*mint(26).pow(fr-1); ans+=mint(cnt[2])*cnt[3]*mint(26).pow(fr-1); if(cnt[3]>=2){ ans+=mint(cnt[3])*(cnt[3]-1)*mint(26).pow(fr-2); } } } cout<<ans<<endl; }