結果

問題 No.1200 お菓子配り-3
ユーザー uni_pythonuni_python
提出日時 2020-08-28 22:22:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 383 ms / 4,000 ms
コード長 3,551 bytes
コンパイル時間 2,231 ms
コンパイル使用メモリ 86,560 KB
実行使用メモリ 82,884 KB
最終ジャッジ日時 2023-08-13 05:53:46
合計ジャッジ時間 8,649 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,300 KB
testcase_01 AC 74 ms
71,456 KB
testcase_02 AC 74 ms
71,452 KB
testcase_03 AC 74 ms
71,452 KB
testcase_04 AC 74 ms
71,168 KB
testcase_05 AC 75 ms
70,968 KB
testcase_06 AC 78 ms
75,256 KB
testcase_07 AC 91 ms
76,284 KB
testcase_08 AC 91 ms
76,156 KB
testcase_09 AC 89 ms
75,880 KB
testcase_10 AC 89 ms
76,232 KB
testcase_11 AC 87 ms
76,060 KB
testcase_12 AC 153 ms
77,812 KB
testcase_13 AC 145 ms
77,532 KB
testcase_14 AC 150 ms
78,232 KB
testcase_15 AC 145 ms
77,700 KB
testcase_16 AC 146 ms
77,516 KB
testcase_17 AC 259 ms
79,924 KB
testcase_18 AC 299 ms
80,544 KB
testcase_19 AC 152 ms
77,644 KB
testcase_20 AC 376 ms
81,984 KB
testcase_21 AC 373 ms
81,456 KB
testcase_22 AC 383 ms
82,884 KB
testcase_23 AC 381 ms
81,644 KB
testcase_24 AC 378 ms
82,040 KB
testcase_25 AC 370 ms
82,848 KB
testcase_26 AC 378 ms
80,748 KB
testcase_27 AC 75 ms
71,552 KB
testcase_28 AC 275 ms
79,452 KB
testcase_29 AC 153 ms
78,472 KB
testcase_30 AC 156 ms
78,520 KB
testcase_31 AC 72 ms
71,424 KB
testcase_32 AC 75 ms
71,216 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