結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー neterukunneterukun
提出日時 2020-02-14 21:55:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 268 ms / 2,000 ms
コード長 1,557 bytes
コンパイル時間 361 ms
コンパイル使用メモリ 87,324 KB
実行使用メモリ 100,208 KB
最終ジャッジ日時 2023-08-10 01:53:17
合計ジャッジ時間 4,293 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
71,164 KB
testcase_01 AC 74 ms
71,588 KB
testcase_02 AC 73 ms
71,456 KB
testcase_03 AC 72 ms
71,332 KB
testcase_04 AC 77 ms
75,648 KB
testcase_05 AC 74 ms
71,232 KB
testcase_06 AC 75 ms
71,224 KB
testcase_07 AC 73 ms
71,544 KB
testcase_08 AC 74 ms
71,312 KB
testcase_09 AC 73 ms
70,804 KB
testcase_10 AC 134 ms
87,232 KB
testcase_11 AC 133 ms
89,100 KB
testcase_12 AC 268 ms
91,256 KB
testcase_13 AC 132 ms
85,364 KB
testcase_14 AC 147 ms
85,924 KB
testcase_15 AC 132 ms
80,816 KB
testcase_16 AC 142 ms
86,464 KB
testcase_17 AC 119 ms
80,360 KB
testcase_18 AC 267 ms
91,224 KB
testcase_19 AC 229 ms
84,756 KB
testcase_20 AC 176 ms
100,208 KB
権限があれば一括ダウンロードができます

ソースコード

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 a:
        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