結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー neterukunneterukun
提出日時 2020-02-14 21:54:03
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,564 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 82,300 KB
実行使用メモリ 99,028 KB
最終ジャッジ日時 2024-04-27 19:35:17
合計ジャッジ時間 3,023 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
53,944 KB
testcase_01 AC 33 ms
53,736 KB
testcase_02 AC 33 ms
53,420 KB
testcase_03 RE -
testcase_04 AC 36 ms
59,648 KB
testcase_05 RE -
testcase_06 AC 33 ms
52,464 KB
testcase_07 AC 33 ms
54,436 KB
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 AC 102 ms
88,316 KB
testcase_12 AC 209 ms
90,276 KB
testcase_13 AC 88 ms
84,676 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 RE -
testcase_17 WA -
testcase_18 AC 199 ms
90,280 KB
testcase_19 AC 164 ms
83,828 KB
testcase_20 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def gcd(a: int, b: int) -> int:
    """a, bの最大公約数(greatest common divisor: GCD)を求める
    計算量: O(log(min(a, b)))
    """
    if b == 0:
        return a
    return gcd(b, a%b)


def make_divisors(n):
    """自然数nの約数を列挙したリストを出力する
    計算量: O(sqrt(N))
    入出力例: 12 -> [1, 2, 3, 4, 6, 12]
    """
    divisors = []
    for k in range(1, int(n**0.5) + 1):
        if n % k == 0:
            divisors.append(k)
            if k != n // k:
                divisors.append(n // k)
    divisors = sorted(divisors)
    return divisors

n, m, k = list(map(int, input().split()))
op = list(input().split())
b = [int(op[i]) for i in range(1, m + 1)]
a = [int(input()) for i in range(n)]

ans = 0
if op[0] == "+":
    a = [a[i] % k for i in range(n)]
    b = [b[i] % k for i in range(m)]
    cnt_b = {}
    for num in b:
        if num not in cnt_b:
            cnt_b[num] = 1
        else:
            cnt_b[num] += 1
    for num in range(n):
        if num == 0:
            ans += cnt_b[num]
        if k - num in cnt_b:
            ans += cnt_b[k - num]
else:
    li = make_divisors(k)
    to_ind = {v: i for i, v in enumerate(li)}
    a_li = [0] * len(li)
    b_li = [0] * len(li)
    for num in a:
        tmp = gcd(num, k)
        a_li[to_ind[tmp]] += 1  
    for num in b:
        tmp = gcd(num, k)
        b_li[to_ind[tmp]] += 1 

    for i, num1 in enumerate(li):
        for j, num2 in enumerate(li):
            if (num1 * num2) % k == 0:
                ans += a_li[i] * b_li[j]

print(ans)
0