結果

問題 No.1200 お菓子配り-3
ユーザー uni_pythonuni_python
提出日時 2020-08-28 22:22:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 351 ms / 4,000 ms
コード長 3,551 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 80,344 KB
最終ジャッジ日時 2024-04-30 23:52:36
合計ジャッジ時間 6,405 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,136 KB
testcase_01 AC 39 ms
53,744 KB
testcase_02 AC 39 ms
54,324 KB
testcase_03 AC 39 ms
54,348 KB
testcase_04 AC 40 ms
54,176 KB
testcase_05 AC 39 ms
53,752 KB
testcase_06 AC 42 ms
58,688 KB
testcase_07 AC 56 ms
66,204 KB
testcase_08 AC 54 ms
64,700 KB
testcase_09 AC 52 ms
64,336 KB
testcase_10 AC 55 ms
64,892 KB
testcase_11 AC 51 ms
63,428 KB
testcase_12 AC 119 ms
76,644 KB
testcase_13 AC 118 ms
76,668 KB
testcase_14 AC 126 ms
77,632 KB
testcase_15 AC 117 ms
76,748 KB
testcase_16 AC 119 ms
76,712 KB
testcase_17 AC 239 ms
79,552 KB
testcase_18 AC 264 ms
79,356 KB
testcase_19 AC 122 ms
76,724 KB
testcase_20 AC 341 ms
80,076 KB
testcase_21 AC 341 ms
79,632 KB
testcase_22 AC 343 ms
79,892 KB
testcase_23 AC 351 ms
80,344 KB
testcase_24 AC 345 ms
79,808 KB
testcase_25 AC 325 ms
80,208 KB
testcase_26 AC 346 ms
80,336 KB
testcase_27 AC 39 ms
53,380 KB
testcase_28 AC 244 ms
79,212 KB
testcase_29 AC 125 ms
77,632 KB
testcase_30 AC 123 ms
77,476 KB
testcase_31 AC 42 ms
53,984 KB
testcase_32 AC 38 ms
53,016 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    S=I()
    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]
        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)
    #[(p1,n1),(p2,n2),...]の形で返す
    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 += 1 + i % 2
            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


    #約数列挙
    #10^6以下で約数の数が最大なのは720720の時(240個しかない!)
    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 calc(X,Y):
        diff=X-Y
        if diff==0:
            ans=0
            #A=1の場合,B+C=X=Y
            ans+=X-1
            
            #それ以外ならB*(A+1)=X=Y
            d=divisors(X)
            ans+=len(d)-1#(A+1)の部分がdの要素になれば良い,(A+1)は1になり得ない
            if X%2==0:# (A+1)=2 => A=1 をダブルカウントしている
                ans-=1
            return ans
        
        d=divisors(diff)
        ans=0
        for i in range(len(d)):
            A=d[i]+1
            Z=d[-1-i]
            if Y-Z>0:
                if (Y-Z)%(A+1)==0:
                    ans+=1
        return ans
        
    
    # X-Y=(B-C)*(A-1)
    for _ in range(S):
        x,y=MI()
        if y>x:
            x,y=y,x
        print(calc(x,y))
            
        


main()
0