結果
問題 |
No.2514 Twelvefold Way Returns
|
ユーザー |
|
提出日時 | 2023-10-20 23:19:53 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,639 bytes |
コンパイル時間 | 1,782 ms |
コンパイル使用メモリ | 168,676 KB |
実行使用メモリ | 9,924 KB |
最終ジャッジ日時 | 2024-09-20 22:56:02 |
合計ジャッジ時間 | 10,396 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 3 |
other | AC * 1 WA * 37 |
ソースコード
#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*u[(333333-2*b-c)%3];res%=p; } } for(int i=0;i<m;++i) {res*=inv3;res%=p;} cout<<(res%p+p)%p; return 0; }