結果

問題 No.206 数の積集合を求めるクエリ
ユーザー ygd.ygd.
提出日時 2022-03-05 15:58:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 532 ms / 7,000 ms
コード長 5,604 bytes
コンパイル時間 440 ms
コンパイル使用メモリ 86,724 KB
実行使用メモリ 113,108 KB
最終ジャッジ日時 2023-09-27 03:27:34
合計ジャッジ時間 19,014 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 490 ms
87,228 KB
testcase_01 AC 493 ms
87,456 KB
testcase_02 AC 489 ms
87,476 KB
testcase_03 AC 491 ms
87,608 KB
testcase_04 AC 487 ms
87,728 KB
testcase_05 AC 491 ms
87,484 KB
testcase_06 AC 492 ms
87,368 KB
testcase_07 AC 485 ms
87,692 KB
testcase_08 AC 512 ms
87,844 KB
testcase_09 AC 513 ms
87,544 KB
testcase_10 AC 494 ms
87,512 KB
testcase_11 AC 491 ms
87,476 KB
testcase_12 AC 506 ms
88,092 KB
testcase_13 AC 495 ms
88,472 KB
testcase_14 AC 496 ms
88,256 KB
testcase_15 AC 491 ms
88,296 KB
testcase_16 AC 507 ms
88,300 KB
testcase_17 AC 518 ms
113,108 KB
testcase_18 AC 495 ms
98,184 KB
testcase_19 AC 506 ms
109,880 KB
testcase_20 AC 509 ms
100,268 KB
testcase_21 AC 503 ms
102,448 KB
testcase_22 AC 509 ms
102,504 KB
testcase_23 AC 521 ms
111,372 KB
testcase_24 AC 531 ms
112,808 KB
testcase_25 AC 532 ms
109,796 KB
testcase_26 AC 515 ms
98,016 KB
testcase_27 AC 509 ms
95,156 KB
testcase_28 AC 525 ms
102,360 KB
testcase_29 AC 522 ms
102,388 KB
testcase_30 AC 512 ms
96,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline
input = sys.stdin.buffer.readline #文字列はダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#@lru_cache(maxsize=None)
#MOD = pow(10,9) + 7
MOD = 998244353
#dx = [1,0,-1,0]
#dy = [0,1,0,-1]

#ACL for pythonより

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]*30
    sum_ie=[0]*30
    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]*30
            ies=[0]*30
            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]*30
            ies=[0]*30
            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]


def main():
    L,M,N = map(int,input().split())
    A = list(map(int,input().split()))
    B = list(map(int,input().split()))
    Q = int(input())

    MAX = pow(10,5) + 10
    #AX[i]:0/1 iとなる値がAに入っているか否か
    AX = [0]*MAX
    BX = [0]*MAX
    for a in A:
        AX[a] = 1
    for b in B:
        BX[b] = 1
    BXR = list(reversed(BX))
    #print(AX)
    #print(BXR)

    #AX[i] * BX[i+q]の係数が欲しいもの。
    #Bを反転させるとBXR[N-1-i-q]となる。
    #iとi+qの関係が、iとN-1-q-iとなるので、和がN-1-qのところを見ればよい。
    #と思うんだが、なぜかN-1+qを見るとあう。なんで?
    conv = FFT(MOD)
    D = conv.convolution(AX,BXR)
    #print(D)

    for v in range(Q):
        ans = D[MAX-1+v]
        print(ans)

    



if __name__ == '__main__':
    main()
0