結果

問題 No.955 ax^2+bx+c=0
ユーザー nehan_der_thalnehan_der_thal
提出日時 2021-01-03 16:38:49
言語 PyPy3
(7.3.15)
結果
MLE  
実行時間 -
コード長 3,141 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 1,365,248 KB
最終ジャッジ日時 2024-04-21 11:30:54
合計ジャッジ時間 4,782 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 MLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
testcase_46 -- -
testcase_47 -- -
testcase_48 -- -
testcase_49 -- -
testcase_50 -- -
testcase_51 -- -
testcase_52 -- -
testcase_53 -- -
testcase_54 -- -
testcase_55 -- -
testcase_56 -- -
testcase_57 -- -
testcase_58 -- -
testcase_59 -- -
testcase_60 -- -
testcase_61 -- -
testcase_62 -- -
testcase_63 -- -
testcase_64 -- -
testcase_65 -- -
testcase_66 -- -
testcase_67 -- -
testcase_68 -- -
testcase_69 -- -
testcase_70 -- -
testcase_71 -- -
testcase_72 -- -
testcase_73 -- -
testcase_74 -- -
testcase_75 -- -
testcase_76 -- -
testcase_77 -- -
testcase_78 -- -
testcase_79 -- -
testcase_80 -- -
testcase_81 -- -
testcase_82 -- -
testcase_83 -- -
testcase_84 -- -
testcase_85 -- -
testcase_86 -- -
testcase_87 -- -
testcase_88 -- -
testcase_89 -- -
testcase_90 -- -
testcase_91 -- -
testcase_92 -- -
testcase_93 -- -
testcase_94 -- -
testcase_95 -- -
testcase_96 -- -
testcase_97 -- -
testcase_98 -- -
testcase_99 -- -
testcase_100 -- -
testcase_101 -- -
testcase_102 -- -
testcase_103 -- -
testcase_104 -- -
testcase_105 -- -
testcase_106 -- -
testcase_107 -- -
testcase_108 -- -
testcase_109 -- -
testcase_110 -- -
testcase_111 -- -
testcase_112 -- -
testcase_113 -- -
testcase_114 -- -
testcase_115 -- -
testcase_116 -- -
testcase_117 -- -
testcase_118 -- -
testcase_119 -- -
testcase_120 -- -
testcase_121 -- -
testcase_122 -- -
testcase_123 -- -
testcase_124 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

# f(n) : 2n^2の約数のうち,n以下のものの個数
# F(n) : sum_(n=1..n)f(n)
# f(15) = 8
# F(15) = 63
# F(1000) = 15066
# F(10^12)を求めよ

from collections import defaultdict
def guchok_f(n):
    m = 2*n**2
    i = 2
    count = 1
    while i<=n:
        if m%i==0:
            count += 1
        i += 1
    return count

def guchok_F(n):
    count = 0
    for i in range(1, n+1):
        count += guchok_f(i)
    return count

print(guchok_f(15))
print(guchok_F(15))
print(guchok_F(1000))

# 以上の計算量はf(n)でO(n)だから合計でO(n^2)
# print(guchok_F(10**5))
# 上が大体52秒くらいかかる

# k が2n^2の約数となるk<=n<=Nの個数g(k)を考える
# sum(k=1..N)g(k) = F(N)となる
# k を素因数分解してa1^b1*a2^b2...ai^biとなるとする。

# k' = a1^(-(-b1//2)-1)a2^(-(-b2//2))...として、N以下k以上のk'の倍数の個数を数えればいい。
# a1=2, a2=3...となってるとしている。a1だけ先に-1して、a1..iを全て2で割って繰り上げ

import itertools
import collections
from math import gcd
def prime_factor_table(n):
    table = [0] * (n + 1)
    for i in range(2, n + 1):
        if table[i] == 0:
            for j in range(i + i, n + 1, i):
                table[j] = i
    return table

def prime_factor(n, prime_factor_table):
    prime_count = dict()
    while prime_factor_table[n] != 0:
        if prime_factor_table[n] not in prime_count:
            prime_count[prime_factor_table[n]] = 0
        prime_count[prime_factor_table[n]] += 1
        n //= prime_factor_table[n]
    if n not in prime_count:
        prime_count[n] = 0
    prime_count[n] += 1
    return prime_count

def yakusu_base_F(n):
    pp = prime_factor_table(n)
    R = 0
    R2 = 0
    for i in range(1, n+1):
        tt = prime_factor(i, pp)
        x = 1
        for key in tt:
            v = tt[key]
            if key == 2:
                v-=1
            x *= key**-((-v)//2)
        diff = (n-i)//x + 1
        diff2 = n//x - i//x+1
        R += diff
        R2 += diff2
#        if diff>-1:
#            print(n, i,x, diff)
#            print(n, i,x, diff2)
#            print(tt)
    return R, R2

# 以上の計算だと以下が0.49sかかる。O(NlogN)
# print(yakusu_base_F(10**6))
# k'ごとにカウントすればさらに高速化できる。
# k'=p1^q1 * p2^q2...pt^qt
# になるようなk'が奇数ならkは2^(t+1)個、偶数なら2^(t)個ある

print(yakusu_base_F(10**8))

def yakusu_base_F_test(n):
    pp = prime_factor_table(n)
    R = 0
    d = defaultdict(set)
    count = 0
    for i in range(1, n+1):
        tt = prime_factor(i, pp)
        x = 1
        for key in tt:
            v = tt[key]
            if key == 2:
                v-=1
            x *= key**-((-v)//2)
        diff = (n-i)//x+1
        d[x].add((i, diff))
        if diff <= 1000:
#            print(i, x, diff)
            count += 1
#        if diff>30:
#            print(i, diff)
        R += diff
    print(len(d))
#    keys = sorted(list(d.keys()))
#    for key in keys:
#        print(key, d[key])
#    print(count)
    return R
#print(yakusu_base_F_test(10**3))
0