結果

問題 No.1006 Share an Integer
ユーザー uni_pythonuni_python
提出日時 2020-07-16 23:33:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 2,915 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 87,120 KB
実行使用メモリ 91,752 KB
最終ジャッジ日時 2023-08-17 03:49:50
合計ジャッジ時間 3,575 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,316 KB
testcase_01 AC 73 ms
71,312 KB
testcase_02 AC 77 ms
75,780 KB
testcase_03 AC 78 ms
71,500 KB
testcase_04 AC 80 ms
75,676 KB
testcase_05 AC 82 ms
75,864 KB
testcase_06 AC 84 ms
75,908 KB
testcase_07 AC 90 ms
75,992 KB
testcase_08 AC 81 ms
75,988 KB
testcase_09 AC 81 ms
76,056 KB
testcase_10 AC 83 ms
75,732 KB
testcase_11 AC 93 ms
84,712 KB
testcase_12 AC 92 ms
90,656 KB
testcase_13 AC 96 ms
91,752 KB
testcase_14 AC 96 ms
91,600 KB
testcase_15 AC 93 ms
89,504 KB
testcase_16 AC 91 ms
82,680 KB
testcase_17 AC 90 ms
84,776 KB
testcase_18 AC 96 ms
89,380 KB
testcase_19 AC 95 ms
88,968 KB
testcase_20 AC 98 ms
89,744 KB
testcase_21 AC 93 ms
91,572 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():
    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)
    def primeFactor(n):
        if n==1:
            return 1
        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)}
        temp=1
        for k,v in ret.items():
            temp*=v+1
        return temp

    
    X=I()
    ff=[0]*(X+1)
    
    #約数は高々300個
    flo=max((X//2)-300,1)
    ceil=min((X//2)+300,X)
    
    for i in range(flo,ceil+1):
        ff[i]=i - primeFactor(i)
        
    ans=[]
    ansd=X
    for a in range(flo,ceil+1):
        b=X-a
        if a*b<=0:
            continue
        d=abs(ff[a]-ff[b])
        if d==ansd:
            ans.append([a,b])
        elif d<ansd:
            ansd=d
            ans=[[a,b]]
            
    for i in range(len(ans)):
        print(' '.join(map(str, ans[i])))
        


main()
0