Submission #39633419


Source Code Expand

def gcd(a, b):
    while b: a, b = b, a % b
    return a
def isPrimeMR(n):
    d = n - 1
    d = d // (d & -d)
    L = [2, 7, 61] if n < 1<<32 else [2, 3, 5, 7, 11, 13, 17] if n < 1<<48 else [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
    for a in L:
        t = d
        y = pow(a, t, n)
        if y == 1: continue
        while y != n - 1:
            y = y * y % n
            if y == 1 or t == n - 1: return 0
            t <<= 1
    return 1
def findFactorRho(n):
    m = 1 << n.bit_length() // 8
    for c in range(1, 99):
        f = lambda x: (x * x + c) % n
        y, r, q, g = 2, 1, 1, 1
        while g == 1:
            x = y
            for i in range(r):
                y = f(y)
            k = 0
            while k < r and g == 1:
                ys = y
                for i in range(min(m, r - k)):
                    y = f(y)
                    q = q * abs(x - y) % n
                g = gcd(q, n)
                k += m
            r <<= 1
        if g == n:
            g = 1
            while g == 1:
                ys = f(ys)
                g = gcd(abs(x - ys), n)
        if g < n:
            if isPrimeMR(g): return g
            elif isPrimeMR(n // g): return n // g
            return findFactorRho(g)
def primeFactor(n):
    i = 2
    ret = {}
    rhoFlg = 0
    while i * i <= n:
        k = 0
        while n % i == 0:
            n //= i
            k += 1
        if k: ret[i] = k
        i += i % 2 + (3 if i % 3 == 1 else 1)
        if i == 101 and n >= 2 ** 20:
            while n > 1:
                if isPrimeMR(n):
                    ret[n], n = 1, 1
                else:
                    rhoFlg = 1
                    j = findFactorRho(n)
                    k = 0
                    while n % j == 0:
                        n //= j
                        k += 1
                    ret[j] = k

    if n > 1: ret[n] = 1
    if rhoFlg: ret = {x: ret[x] for x in sorted(ret)}
    return ret
def divisors(N):
    pf = primeFactor(N)
    ret = [1]
    for p in pf:
        ret_prev = ret
        ret = []
        for i in range(pf[p]+1):
            for r in ret_prev:
                ret.append(r * (p ** i))
    return sorted(ret)
def divisors_pf(pf):
    ret = [1]
    for p in pf:
        ret_prev = ret
        ret = []
        for i in range(pf[p]+1):
            for r in ret_prev:
                ret.append(r * (p ** i))
    return sorted(ret)
def chk(n, k):
    if k <= 1:
        return 0
    while n:
        a = n % k
        if a > 1:
            return 0
        n //= k
    return 1

from collections import defaultdict
D = defaultdict(int)
T = int(input())
for _ in range(T):
    N = int(input())
    if N in D:
        print(D[N])
        continue
    ans = 0
    for d in divisors(N) + divisors(N - 1):
        ans += chk(N, d)
    print(ans)
    D[N] = ans

Submission Info

Submission Time
Task F - Zero or One
User Kiri8128
Language PyPy3 (7.3.0)
Score 500
Code Size 2943 Byte
Status AC
Exec Time 1085 ms
Memory 204932 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 1
AC × 35
Set Name Test Cases
Sample example0.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, example0.txt
Case Name Status Exec Time Memory
000.txt AC 135 ms 77020 KiB
001.txt AC 793 ms 80108 KiB
002.txt AC 84 ms 74044 KiB
003.txt AC 578 ms 79088 KiB
004.txt AC 546 ms 81460 KiB
005.txt AC 575 ms 82272 KiB
006.txt AC 552 ms 82960 KiB
007.txt AC 277 ms 78108 KiB
008.txt AC 347 ms 80800 KiB
009.txt AC 397 ms 79760 KiB
010.txt AC 329 ms 80884 KiB
011.txt AC 650 ms 78172 KiB
012.txt AC 707 ms 81108 KiB
013.txt AC 699 ms 80240 KiB
014.txt AC 689 ms 78228 KiB
015.txt AC 694 ms 78884 KiB
016.txt AC 722 ms 79384 KiB
017.txt AC 658 ms 78780 KiB
018.txt AC 692 ms 80276 KiB
019.txt AC 673 ms 79228 KiB
020.txt AC 719 ms 80120 KiB
021.txt AC 190 ms 77364 KiB
022.txt AC 198 ms 77532 KiB
023.txt AC 187 ms 77944 KiB
024.txt AC 192 ms 76724 KiB
025.txt AC 189 ms 76596 KiB
026.txt AC 196 ms 77872 KiB
027.txt AC 192 ms 77832 KiB
028.txt AC 200 ms 77764 KiB
029.txt AC 210 ms 76236 KiB
030.txt AC 193 ms 77676 KiB
031.txt AC 144 ms 90004 KiB
032.txt AC 735 ms 177488 KiB
033.txt AC 1085 ms 204932 KiB
example0.txt AC 67 ms 65468 KiB