結果

問題 No.1307 Rotate and Accumulate
ユーザー roarisroaris
提出日時 2020-12-05 00:55:29
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 976 ms / 5,000 ms
コード長 4,562 bytes
コンパイル時間 341 ms
コンパイル使用メモリ 87,024 KB
実行使用メモリ 131,292 KB
最終ジャッジ日時 2023-10-13 12:44:16
合計ジャッジ時間 11,976 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,132 KB
testcase_01 AC 72 ms
71,260 KB
testcase_02 AC 72 ms
71,368 KB
testcase_03 AC 79 ms
75,840 KB
testcase_04 AC 91 ms
76,368 KB
testcase_05 AC 81 ms
75,800 KB
testcase_06 AC 80 ms
75,888 KB
testcase_07 AC 71 ms
71,336 KB
testcase_08 AC 511 ms
101,356 KB
testcase_09 AC 518 ms
103,468 KB
testcase_10 AC 523 ms
104,712 KB
testcase_11 AC 501 ms
96,120 KB
testcase_12 AC 522 ms
103,416 KB
testcase_13 AC 160 ms
89,320 KB
testcase_14 AC 302 ms
92,832 KB
testcase_15 AC 976 ms
129,188 KB
testcase_16 AC 960 ms
129,332 KB
testcase_17 AC 966 ms
129,076 KB
testcase_18 AC 963 ms
123,364 KB
testcase_19 AC 956 ms
129,248 KB
testcase_20 AC 969 ms
131,292 KB
testcase_21 AC 74 ms
71,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class FFT():
    def primitive_root_constexpr(self,m):
        if m==2:return 1
        if m==167772161:return 3
        if m==469762049:return 3
        if m==754974721:return 11
        if m==998244353:return 3
        divs=[0]*20
        divs[0]=2
        cnt=1
        x=(m-1)//2
        while(x%2==0):x//=2
        i=3
        while(i*i<=x):
            if (x%i==0):
                divs[cnt]=i
                cnt+=1
                while(x%i==0):
                    x//=i
            i+=2
        if x>1:
            divs[cnt]=x
            cnt+=1
        g=2
        while(1):
            ok=True
            for i in range(cnt):
                if pow(g,(m-1)//divs[i],m)==1:
                    ok=False
                    break
            if ok:
                return g
            g+=1
    def bsf(self,x):
        res=0
        while(x%2==0):
            res+=1
            x//=2
        return res
    butterfly_first=True
    butterfly_inv_first=True
    sum_e=[0]*24
    sum_ie=[0]*24
    def __init__(self,MOD):
        self.mod=MOD
        self.g=self.primitive_root_constexpr(self.mod)
    def butterfly(self,a):
        n=len(a)
        h=(n-1).bit_length()
        if self.butterfly_first:
            self.butterfly_first=False
            es=[0]*24
            ies=[0]*24
            cnt2=self.bsf(self.mod-1)
            e=pow(self.g,(self.mod-1)>>cnt2,self.mod)
            ie=pow(e,self.mod-2,self.mod)
            for i in range(cnt2,1,-1):
                es[i-2]=e
                ies[i-2]=ie
                e=(e*e)%self.mod
                ie=(ie*ie)%self.mod
            now=1
            for i in range(cnt2-2):
                self.sum_e[i]=((es[i]*now)%self.mod)
                now*=ies[i]
                now%=self.mod
        for ph in range(1,h+1):
            w=1<<(ph-1)
            p=1<<(h-ph)
            now=1
            for s in range(w):
                offset=s<<(h-ph+1)
                for i in range(p):
                    l=a[i+offset]
                    r=a[i+offset+p]*now
                    r%=self.mod
                    a[i+offset]=l+r
                    a[i+offset]%=self.mod
                    a[i+offset+p]=l-r
                    a[i+offset+p]%=self.mod
                now*=self.sum_e[(~s & -~s).bit_length()-1]
                now%=self.mod
    def butterfly_inv(self,a):
        n=len(a)
        h=(n-1).bit_length()
        if self.butterfly_inv_first:
            self.butterfly_inv_first=False
            es=[0]*24
            ies=[0]*24
            cnt2=self.bsf(self.mod-1)
            e=pow(self.g,(self.mod-1)>>cnt2,self.mod)
            ie=pow(e,self.mod-2,self.mod)
            for i in range(cnt2,1,-1):
                es[i-2]=e
                ies[i-2]=ie
                e=(e*e)%self.mod
                ie=(ie*ie)%self.mod
            now=1
            for i in range(cnt2-2):
                self.sum_ie[i]=((ies[i]*now)%self.mod)
                now*=es[i]
                now%=self.mod
        for ph in range(h,0,-1):
            w=1<<(ph-1)
            p=1<<(h-ph)
            inow=1
            for s in range(w):
                offset=s<<(h-ph+1)
                for i in range(p):
                    l=a[i+offset]
                    r=a[i+offset+p]
                    a[i+offset]=l+r
                    a[i+offset]%=self.mod
                    a[i+offset+p]=(l-r)*inow
                    a[i+offset+p]%=self.mod
                inow*=self.sum_ie[(~s & -~s).bit_length()-1]
                inow%=self.mod
    def convolution(self,a,b):
        n=len(a);m=len(b)
        if not(a) or not(b):
            return []
        if min(n,m)<=40:
            if n<m:
                n,m=m,n
                a,b=b,a
            res=[0]*(n+m-1)
            for i in range(n):
                for j in range(m):
                    res[i+j]+=a[i]*b[j]
                    res[i+j]%=self.mod
            return res
        z=1<<((n+m-2).bit_length())
        a=a+[0]*(z-n)
        b=b+[0]*(z-m)
        self.butterfly(a)
        self.butterfly(b)
        c=[0]*z
        for i in range(z):
            c[i]=(a[i]*b[i])%self.mod
        self.butterfly_inv(c)
        iz=pow(z,self.mod-2,self.mod)
        for i in range(n+m-1):
            c[i]=(c[i]*iz)%self.mod
        return c[:n+m-1]

N, Q = map(int, input().split())
a = list(map(int, input().split()))
r = list(map(int, input().split()))
f = a*2
f.reverse()
g = [0]*N

for ri in r:
    g[ri] += 1

fft = FFT(998244353)
h = fft.convolution(f, g)
print(*list(reversed(h[N:2*N])))
0