結果

問題 No.2060 AND Sequence
ユーザー taiga0629kyopro
提出日時 2022-08-06 01:45:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,104 bytes
コンパイル時間 287 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 78,208 KB
最終ジャッジ日時 2024-09-19 02:14:40
合計ジャッジ時間 5,790 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

def naive (n,m):
    mod=998244353
    dp=[[0]*(m+1) for i in range(n+1)]
    dp[0][0]=1
    for i in range(1,n+1):
        for x in range(m+1):
            for y in range(m+1):
                if x&y==y:
                    dp[i][x]+=dp[i-1][y]
                    dp[i][x]%=mod
    return sum(dp[n])%mod

def sol1(n,m):
    mod=998244353
    memo=dict()
    def dp(m,k,f):
        if m==0:
            if k==0 and f==1:return 1
            return 0
        if (m,k,f) in memo:return memo[m,k,f]
        ans=0
        for bx in range(2):
            nf=f
            if nf==0 and m%2==1 and bx==0:nf=1
            if nf==1 and m%2==0 and bx==1:nf=0
            ans+=dp(m//2,k-bx,nf)
            ans%=mod
        memo[m,k,f]=ans
        return ans
    ans=0
    for k in range(33):
        ans+=dp(m,k,1)*pow(n,k,mod)%mod
        ans%=mod
    return ans

from random import randrange as rd
cnt=0
while 0:
    cnt+=1
    print(cnt)
    n=rd(2,1000)
    m=rd(1,1000)
    ans1=naive(n,m)
    ans2=sol1(n,m)
    if ans1!=ans2:
        print(n,m)
        break

n,m=map(int,input().split())
print(sol1(n,m))
0