結果

問題 No.474 色塗り2
ユーザー snukesnuke
提出日時 2016-12-22 17:23:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 550 ms / 2,000 ms
コード長 1,708 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 82,248 KB
実行使用メモリ 111,284 KB
最終ジャッジ日時 2024-05-08 11:06:07
合計ジャッジ時間 1,902 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
92,328 KB
testcase_01 AC 81 ms
93,056 KB
testcase_02 AC 87 ms
96,212 KB
testcase_03 AC 550 ms
111,284 KB
testcase_04 AC 79 ms
92,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def ext_gcd(a, b):
    if b == 0:
        return [1, 0]
    else:
        y, x = ext_gcd(b, a % b)
        return [x, y - (a // b) * x]

def minv(n, m):
    return ext_gcd(n, m)[0]

mod = 2 ** 21

fact_factor2 = [0] * mod
fact_mod = [0] * mod

fact_mod[0] = 1
for i in range(1, mod):
    j = i
    f2 = 0
    while j % 2 == 0:
        f2 += 1
        j = j // 2
    fact_mod[i] = fact_mod[i-1] * j % mod
    fact_factor2[i] = fact_factor2[i-1] + f2

# totient
# totient_table = [0] * mod
# for i in range(1, mod):
#     totient_table[i] = i

# for i in range(1, mod):
#     if totient_table[i] == i:
#         for j in range(i, mod, i):
#             totient_table[j] = totient_table[j] // i * (i - 1)

# def minv2(n, m):
#     return pow(n, totient_table[m] - 1, m)

def totient(n):
    res = n
    for i in range(2, n):
        if i*i > n:
            break
        if n % i == 0:
            res = res // i * (i - 1)
            while n % i == 0:
                n = n // i
    if n != 1:
        res = res // n * (n - 1)
    return res

totient_mod = totient(mod)
def minv2(n):
    return pow(n, totient_mod - 1, mod)

def solve(A, B, C):
    if C % 2 == 0:
        return 0
    else:
        f2 = fact_factor2[C+B-1] - fact_factor2[B] - fact_factor2[C-1]
        val = fact_mod[C+B-1] * minv(fact_mod[B], mod) % mod * minv(fact_mod[C-1], mod) % mod
        # val = fact_mod[C+B-1] * minv2(fact_mod[B]) % mod * minv2(fact_mod[C-1]) % mod
        val = ((C * val % mod) << min(32, f2)) % mod + A - 1
        if (val & A) == A:
            return 1
        else:
            return 0

testcase = int(input())
for _ in range(testcase):
    A, B, C = list(map(int, input().split()))
    print(solve(A, B, C))
0