結果

問題 No.719 Coprime
ユーザー vwxyzvwxyz
提出日時 2022-04-30 06:34:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 697 ms / 3,000 ms
コード長 3,132 bytes
コンパイル時間 294 ms
コンパイル使用メモリ 86,636 KB
実行使用メモリ 81,308 KB
最終ジャッジ日時 2023-09-11 23:07:35
合計ジャッジ時間 12,038 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
71,948 KB
testcase_01 AC 95 ms
71,824 KB
testcase_02 AC 97 ms
71,720 KB
testcase_03 AC 97 ms
71,840 KB
testcase_04 AC 98 ms
72,028 KB
testcase_05 AC 100 ms
71,976 KB
testcase_06 AC 97 ms
71,896 KB
testcase_07 AC 96 ms
71,932 KB
testcase_08 AC 95 ms
71,832 KB
testcase_09 AC 96 ms
71,816 KB
testcase_10 AC 96 ms
71,820 KB
testcase_11 AC 97 ms
72,028 KB
testcase_12 AC 96 ms
71,776 KB
testcase_13 AC 98 ms
72,196 KB
testcase_14 AC 97 ms
71,928 KB
testcase_15 AC 98 ms
71,816 KB
testcase_16 AC 98 ms
71,880 KB
testcase_17 AC 97 ms
71,768 KB
testcase_18 AC 97 ms
71,824 KB
testcase_19 AC 97 ms
71,712 KB
testcase_20 AC 97 ms
71,832 KB
testcase_21 AC 99 ms
71,980 KB
testcase_22 AC 99 ms
71,712 KB
testcase_23 AC 99 ms
71,924 KB
testcase_24 AC 97 ms
71,924 KB
testcase_25 AC 98 ms
71,704 KB
testcase_26 AC 99 ms
72,160 KB
testcase_27 AC 98 ms
71,828 KB
testcase_28 AC 99 ms
71,932 KB
testcase_29 AC 99 ms
71,928 KB
testcase_30 AC 100 ms
71,972 KB
testcase_31 AC 99 ms
72,024 KB
testcase_32 AC 99 ms
71,844 KB
testcase_33 AC 99 ms
71,880 KB
testcase_34 AC 99 ms
71,936 KB
testcase_35 AC 97 ms
71,724 KB
testcase_36 AC 99 ms
71,760 KB
testcase_37 AC 99 ms
71,828 KB
testcase_38 AC 99 ms
71,772 KB
testcase_39 AC 98 ms
71,932 KB
testcase_40 AC 98 ms
71,980 KB
testcase_41 AC 103 ms
72,396 KB
testcase_42 AC 104 ms
72,448 KB
testcase_43 AC 102 ms
72,576 KB
testcase_44 AC 105 ms
75,320 KB
testcase_45 AC 116 ms
77,916 KB
testcase_46 AC 120 ms
77,764 KB
testcase_47 AC 130 ms
77,708 KB
testcase_48 AC 128 ms
77,648 KB
testcase_49 AC 132 ms
77,660 KB
testcase_50 AC 136 ms
77,664 KB
testcase_51 AC 136 ms
77,932 KB
testcase_52 AC 156 ms
78,304 KB
testcase_53 AC 176 ms
78,472 KB
testcase_54 AC 249 ms
78,800 KB
testcase_55 AC 344 ms
79,400 KB
testcase_56 AC 607 ms
80,324 KB
testcase_57 AC 594 ms
80,232 KB
testcase_58 AC 679 ms
81,088 KB
testcase_59 AC 695 ms
81,308 KB
testcase_60 AC 697 ms
81,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from math import gcd
import sys
readline=sys.stdin.readline

class Prime:
    def __init__(self,N):
        assert N<=10**8
        self.smallest_prime_factor=[None]*(N+1)
        for i in range(2,N+1,2):
            self.smallest_prime_factor[i]=2
        n=int(N**.5)+1
        for p in range(3,n,2):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
                for i in range(p**2,N+1,2*p):
                    if self.smallest_prime_factor[i]==None:
                        self.smallest_prime_factor[i]=p
        for p in range(n,N+1):
            if self.smallest_prime_factor[p]==None:
                self.smallest_prime_factor[p]=p
        self.primes=[p for p in range(N+1) if p==self.smallest_prime_factor[p]]

    def Factorize(self,N):
        assert N>=1
        factors=defaultdict(int)
        if N<=len(self.smallest_prime_factor)-1:
            while N!=1:
                factors[self.smallest_prime_factor[N]]+=1
                N//=self.smallest_prime_factor[N]
        else:
            for p in self.primes:
                while N%p==0:
                    N//=p
                    factors[p]+=1
                if N<p*p:
                    if N!=1:
                        factors[N]+=1
                    break
                if N<=len(self.smallest_prime_factor)-1:
                    while N!=1:
                        factors[self.smallest_prime_factor[N]]+=1
                        N//=self.smallest_prime_factor[N]
                    break
            else:
                if N!=1:
                    factors[N]+=1
        return factors

    def Divisors(self,N):
        assert N>0
        divisors=[1]
        for p,e in self.Factorize(N).items():
            A=[1]
            for _ in range(e):
                A.append(A[-1]*p)
            divisors=[i*j for i in divisors for j in A]
        return divisors

    def Is_Prime(self,N):
        return N==self.smallest_prime_factor[N]

    def Totient(self,N):
        for p in self.Factorize(N).keys():
            N*=p-1
            N//=p
        return N

    def Mebius(self,N):
        fact=self.Factorize(N)
        for e in fact.values():
            if e>=2:
                return 0
        else:
            if len(fact)%2==0:
                return 1
            else:
                return -1

N=int(readline())
P=Prime(N)
dp=defaultdict(int)
dp[1]=0
dct=defaultdict(list)
for n in range(2,N+1):
    a,b=1,1
    for p,e in P.Factorize(n).items():
        if p**2<=N:
            a*=p
        else:
            b=p
    if b!=1:
        dct[b].append((a,n))
    else:
        prev=dp
        dp=defaultdict(int)
        for tpl,x in prev.items():
            g=tpl
            dp[g]=max(dp[g],x)
            if gcd(g,a)==1:
                dp[g*a]=max(dp[g*a],prev[g]+n)
for b,lst in dct.items():
    prev=dp
    dp=defaultdict(int)
    for tpl,x in prev.items():
        g=tpl
        dp[g]=max(dp[g],x)
        for a,n in lst:
            if gcd(g,a)==1:
                dp[g*a]=max(dp[g*a],prev[g]+n)
ans=max(dp.values())
print(ans)
0