結果
問題 |
No.2514 Twelvefold Way Returns
|
ユーザー |
|
提出日時 | 2023-10-20 23:20:52 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 285 ms / 3,000 ms |
コード長 | 1,712 bytes |
コンパイル時間 | 1,565 ms |
コンパイル使用メモリ | 170,092 KB |
実行使用メモリ | 9,796 KB |
最終ジャッジ日時 | 2024-09-20 22:59:18 |
合計ジャッジ時間 | 12,786 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
#include <bits/stdc++.h> using namespace std; #define int long long const int p=998244353; int po(int a,int b) {if(b==0) return 1; if(b==1) return a; if(b%2==0) {int u=po(a,b/2);return (u*u)%p;} else {int u=po(a,b-1);return (a*u)%p;}} int inv(int x) {return po(x,p-2);} const int maxn=4e5+5; int fact[maxn];int invf[maxn]; int c1(int n1,int n2,int n3) { int ans=fact[n1+n2+n3];ans*=invf[n1];ans%=p;ans*=invf[n2];ans%=p;ans*=invf[n3];ans%=p;return ans; } array<int,3> po1(array<int,3> v,int n) { if(n==1) return v; if(n%2==0) { array<int,3> u=po1(v,n/2); array<int,3> res={0,0,0}; for(int i=0;i<3;++i) {for(int j=0;j<3;++j) { res[(i+j)%3]+=u[i]*u[j];res[(i+j)%3]%=p; }} return res; } else { array<int,3> u=po1(v,n-1); array<int,3> res={0,0,0}; for(int i=0;i<3;++i) {for(int j=0;j<3;++j) { res[(i+j)%3]+=u[i]*v[j];res[(i+j)%3]%=p; }} return res; } } int32_t main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); fact[0]=1;for(int i=1;i<maxn;++i) {fact[i]=(fact[i-1]*i)%p;} for(int i=0;i<maxn;++i) {invf[i]=inv(fact[i]);} assert((p+1)%3==0);int inv3=(p+1)/3; int n,m;cin>>n>>m; int res=0; for(int a=0;a<=m;++a) { for(int b=0;a+b<=m;++b) { int c=m-a-b; int h=c1(a,b,c); array<int,3> v={a,b,c}; array<int,3> u=po1(v,n); res+=h*(2*u[(333333-2*b-c)%3]-u[(333333-2*b-c+1)%3]-u[(333333-2*b-c+2)%3]);res%=p; } } for(int i=0;i<m;++i) {res*=inv3;res%=p;} res*=(p+1)/2;res%=p; cout<<(res%p+p)%p; return 0; }