結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー 👑 rin204
提出日時 2022-07-09 15:17:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 965 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 82,428 KB
実行使用メモリ 92,668 KB
最終ジャッジ日時 2024-12-31 01:02:34
合計ジャッジ時間 3,295 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

n, m, k = map(int, input().split())
op, *B = input().split()
B = [int(b) for b in B]
A = [int(input()) for _ in range(n)]
if op == "+":
    cnt = {}
    for b in B:
        cnt[b % k] = cnt.get(b % k, 0) + 1
    ans = 0
    for a in A:
        ans += cnt.get(-a % k, 0)
    print(ans)
else:
    div = []
    for i in range(1, int(k ** 0.5 + 1)):
        if k % i == 0:
            div.append(i)
            if i * i != k:
                div.append(k // i)
            else:
                pass
        else:
            pass
    cnt = {}
    div.sort()
    for b in B:
        g = gcd(b, k)
        cnt[g] = cnt.get(g, 0) + 1
    for d in div:
        if d not in cnt:
            cnt[d] = 0
        for d2 in div[::-1]:
            if d2 == d:
                break
            if d2 % d == 0:
                cnt[d] += cnt.get(d2, 0)
    ans = 0
    for a in A:
        g = gcd(a, k)
        ans += cnt.get(k // g, 0)
    print(ans)

    
0