結果

問題 No.990 N×Mマス計算(Kの倍数)
ユーザー convexineqconvexineq
提出日時 2020-02-14 22:08:19
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 1,606 bytes
コンパイル時間 628 ms
コンパイル使用メモリ 10,876 KB
実行使用メモリ 40,752 KB
最終ジャッジ日時 2023-08-10 01:59:31
合計ジャッジ時間 2,758 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,616 KB
testcase_01 AC 14 ms
8,396 KB
testcase_02 AC 15 ms
8,276 KB
testcase_03 AC 17 ms
8,600 KB
testcase_04 AC 14 ms
8,336 KB
testcase_05 AC 16 ms
8,772 KB
testcase_06 AC 14 ms
8,352 KB
testcase_07 AC 14 ms
8,300 KB
testcase_08 AC 16 ms
8,568 KB
testcase_09 AC 16 ms
8,564 KB
testcase_10 AC 89 ms
19,848 KB
testcase_11 AC 65 ms
18,704 KB
testcase_12 AC 211 ms
31,056 KB
testcase_13 AC 66 ms
14,712 KB
testcase_14 AC 119 ms
23,396 KB
testcase_15 AC 73 ms
15,948 KB
testcase_16 AC 106 ms
22,264 KB
testcase_17 AC 61 ms
14,820 KB
testcase_18 AC 209 ms
31,176 KB
testcase_19 AC 113 ms
18,712 KB
testcase_20 AC 181 ms
40,752 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# coding: utf-8
# Your code here!

def prime_factorize(N): #素因数分解
    exponent = 0
    while N%2 == 0:
        exponent += 1
        N //= 2
    if exponent: factorization = [[2,exponent]]
    else: factorization = []
    i=1
    while i*i <=N:
        i += 2
        if N%i: continue
        exponent = 0
        while N%i == 0:
            exponent += 1
            N //= i
        factorization.append([i,exponent])
    if N!= 1: factorization.append([N,1])
    assert N != 0, "zero"
    return factorization


import sys
readline = sys.stdin.readline
read = sys.stdin.read

n,m,k = [int(i) for i in readline().split()]

x = read().split()
op = x[0]

b = list(map(lambda x: int(x)%k, x[1:m+1]))
a = list(map(lambda x: int(x)%k, x[m+1:]))

#a.sort()
b.sort()




if op == "+":
    from collections import Counter
    C = Counter(b)
    ans = 0
    for ai in a:
        if ai == 0:
            ans += C[0]
        else:
            ans += C[k-ai]
    print(ans)

else:
    if k == 1:
        print(n*m)
        exit()

    fac = prime_factorize(k)
    plist = [f[0] for f in fac]
    
    div = [1]
    for p,e in fac:
        div = [i*p**j for j in range(e+1) for i in div]
    
    #print(fac)
    #print(div)
    
    from math import gcd
    
    memo = {i:0 for i in div}
    for bi in b:
        memo[gcd(bi,k)] += 1

    #print(memo)
    # zeta
    for p in plist:
        for x in div[::-1]:
            if x%p == 0:
                memo[x//p] += memo[x]
        
    #print(memo)
    
    ans = 0
    for ai in a:
        ai = gcd(ai,k)

        ans += memo[k//ai]


    print(ans)




0