結果

問題 No.474 色塗り2
ユーザー snukesnuke
提出日時 2016-12-22 18:10:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 420 ms / 2,000 ms
コード長 1,894 bytes
コンパイル時間 335 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 109,952 KB
最終ジャッジ日時 2024-05-08 11:23:02
合計ジャッジ時間 1,610 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
92,288 KB
testcase_01 AC 79 ms
92,416 KB
testcase_02 AC 84 ms
93,696 KB
testcase_03 AC 420 ms
109,952 KB
testcase_04 AC 77 ms
92,416 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 ext_gcd(a, b):
    u = y = 1
    v = x = 0
    while a != 0:
        q = b // a
        u, x = x-q*u, u
        v, y = y-q*v, v
        a, b = b-q*a, a
    return [x,y]

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