結果

問題 No.2514 Twelvefold Way Returns
ユーザー maksimmaksim
提出日時 2023-10-20 23:20:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 3,000 ms
コード長 1,712 bytes
コンパイル時間 2,078 ms
コンパイル使用メモリ 169,296 KB
実行使用メモリ 9,868 KB
最終ジャッジ日時 2023-10-20 23:21:49
合計ジャッジ時間 10,922 ms
ジャッジサーバーID
(参考情報)
judge9 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 149 ms
9,864 KB
testcase_01 AC 149 ms
9,816 KB
testcase_02 AC 185 ms
9,864 KB
testcase_03 AC 152 ms
9,864 KB
testcase_04 AC 228 ms
9,864 KB
testcase_05 AC 195 ms
9,864 KB
testcase_06 AC 188 ms
9,864 KB
testcase_07 AC 149 ms
9,868 KB
testcase_08 AC 175 ms
9,864 KB
testcase_09 AC 178 ms
9,864 KB
testcase_10 AC 213 ms
9,864 KB
testcase_11 AC 152 ms
9,864 KB
testcase_12 AC 163 ms
9,864 KB
testcase_13 AC 150 ms
9,864 KB
testcase_14 AC 164 ms
9,864 KB
testcase_15 AC 200 ms
9,864 KB
testcase_16 AC 215 ms
9,868 KB
testcase_17 AC 185 ms
9,864 KB
testcase_18 AC 211 ms
9,864 KB
testcase_19 AC 216 ms
9,864 KB
testcase_20 AC 223 ms
9,868 KB
testcase_21 AC 215 ms
9,864 KB
testcase_22 AC 215 ms
9,864 KB
testcase_23 AC 178 ms
9,816 KB
testcase_24 AC 222 ms
9,820 KB
testcase_25 AC 181 ms
9,816 KB
testcase_26 AC 149 ms
9,820 KB
testcase_27 AC 216 ms
9,816 KB
testcase_28 AC 161 ms
9,816 KB
testcase_29 AC 162 ms
9,816 KB
testcase_30 AC 157 ms
9,816 KB
testcase_31 AC 165 ms
9,816 KB
testcase_32 AC 157 ms
9,816 KB
testcase_33 AC 150 ms
9,816 KB
testcase_34 AC 184 ms
9,816 KB
testcase_35 AC 196 ms
9,864 KB
testcase_36 AC 150 ms
9,816 KB
testcase_37 AC 151 ms
9,816 KB
testcase_38 AC 212 ms
9,864 KB
testcase_39 AC 225 ms
9,868 KB
testcase_40 AC 212 ms
9,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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