結果

問題 No.719 Coprime
ユーザー vwxyzvwxyz
提出日時 2022-04-30 06:33:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,275 ms / 3,000 ms
コード長 3,132 bytes
コンパイル時間 543 ms
コンパイル使用メモリ 11,252 KB
実行使用メモリ 9,744 KB
最終ジャッジ日時 2023-09-11 23:07:05
合計ジャッジ時間 15,789 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
9,164 KB
testcase_01 AC 21 ms
9,148 KB
testcase_02 AC 21 ms
9,308 KB
testcase_03 AC 20 ms
9,168 KB
testcase_04 AC 20 ms
9,304 KB
testcase_05 AC 20 ms
9,208 KB
testcase_06 AC 21 ms
9,312 KB
testcase_07 AC 20 ms
9,168 KB
testcase_08 AC 20 ms
9,116 KB
testcase_09 AC 21 ms
9,164 KB
testcase_10 AC 20 ms
9,256 KB
testcase_11 AC 20 ms
9,216 KB
testcase_12 AC 20 ms
9,116 KB
testcase_13 AC 20 ms
9,236 KB
testcase_14 AC 21 ms
9,180 KB
testcase_15 AC 20 ms
9,216 KB
testcase_16 AC 20 ms
9,264 KB
testcase_17 AC 21 ms
9,172 KB
testcase_18 AC 21 ms
9,100 KB
testcase_19 AC 20 ms
9,148 KB
testcase_20 AC 20 ms
9,196 KB
testcase_21 AC 21 ms
9,100 KB
testcase_22 AC 21 ms
9,124 KB
testcase_23 AC 21 ms
9,164 KB
testcase_24 AC 20 ms
9,212 KB
testcase_25 AC 20 ms
9,160 KB
testcase_26 AC 21 ms
9,212 KB
testcase_27 AC 20 ms
9,240 KB
testcase_28 AC 20 ms
9,120 KB
testcase_29 AC 20 ms
9,216 KB
testcase_30 AC 20 ms
9,208 KB
testcase_31 AC 20 ms
9,168 KB
testcase_32 AC 20 ms
9,216 KB
testcase_33 AC 20 ms
9,256 KB
testcase_34 AC 20 ms
9,232 KB
testcase_35 AC 20 ms
9,212 KB
testcase_36 AC 20 ms
9,164 KB
testcase_37 AC 20 ms
9,160 KB
testcase_38 AC 20 ms
9,128 KB
testcase_39 AC 21 ms
9,308 KB
testcase_40 AC 20 ms
9,120 KB
testcase_41 AC 21 ms
9,124 KB
testcase_42 AC 21 ms
9,216 KB
testcase_43 AC 22 ms
9,100 KB
testcase_44 AC 22 ms
9,164 KB
testcase_45 AC 24 ms
9,212 KB
testcase_46 AC 25 ms
9,216 KB
testcase_47 AC 31 ms
9,208 KB
testcase_48 AC 32 ms
9,252 KB
testcase_49 AC 32 ms
9,268 KB
testcase_50 AC 32 ms
9,260 KB
testcase_51 AC 33 ms
9,124 KB
testcase_52 AC 59 ms
9,120 KB
testcase_53 AC 140 ms
9,264 KB
testcase_54 AC 375 ms
9,272 KB
testcase_55 AC 767 ms
9,420 KB
testcase_56 AC 1,826 ms
9,604 KB
testcase_57 AC 1,823 ms
9,556 KB
testcase_58 AC 2,168 ms
9,744 KB
testcase_59 AC 2,200 ms
9,612 KB
testcase_60 AC 2,275 ms
9,572 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