結果

問題 No.990 N×Mマス計算(Kの倍数)
コンテスト
ユーザー titia
提出日時 2020-02-14 23:44:19
言語 Python3
(3.14.3 + numpy 2.4.4 + scipy 1.17.1)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 1,346 ms / 2,000 ms
コード長 1,217 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 572 ms
コンパイル使用メモリ 20,828 KB
実行使用メモリ 41,024 KB
最終ジャッジ日時 2026-05-11 05:37:49
合計ジャッジ時間 7,437 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
from collections import Counter
from math import sqrt
from math import gcd
input = sys.stdin.readline

N,M,K=map(int,input().split())
B=list(input().split())
A=[int(input()) for i in range(N)]

ANS=0

if B[0]=="+":
    B=list(map(int,B[1:]))
    A=Counter([a%K for a in A])
    B=Counter([b%K for b in B])

    for c in A:
        ANS+=A[c]*B[K-c]
    ANS+=A[0]*B[0]

    print(ANS)
    sys.exit()
        
B=list(map(int,B[1:]))
A=[gcd(a,K) for a in A]
B=[gcd(b,K) for b in B]
x=K
L=int(sqrt(x))

FACT=Counter()

for i in range(2,L+2):
    while x%i==0:
        FACT[i]+=1
        x=x//i

if x!=1:
    FACT[x]=FACT.get(x,0)+1

fs=list(FACT.keys())
fv=list(FACT.values())
LEN=len(fs)
SCA=Counter()
SCB=Counter()

for b in B:
    sc=[0]*LEN
    for i in range(LEN):
        f=fs[i]
        while b%f==0:
            sc[i]+=1
            b//=f
    SCB[tuple(sc)]+=1

for a in A:
    sc=[0]*LEN
    for i in range(LEN):
        f=fs[i]
        while a%f==0:
            sc[i]+=1
            a//=f
    SCA[tuple(sc)]+=1

def geq(sa,sb):
    for i in range(LEN):
        if fv[i]-sa[i]>sb[i]:
            return 0
    return SCA[sa]*SCB[sb]

for sa in SCA:
    for sb in SCB:
        ANS+=geq(sa,sb)
print(ANS)
0