結果

問題 No.278 連続する整数の和(2)
コンテスト
ユーザー rpy3cpp
提出日時 2015-09-05 11:25:59
言語 Python3
(3.15.0 + numpy 2.5.4 + scipy 1.18.1 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 94 ms / 2,000 ms
+ 974µs
コード長 1,026 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 59 ms
コンパイル使用メモリ 14,848 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2026-10-06 11:13:21
合計ジャッジ時間 2,231 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import math
import collections

def factorize(n):
    ''' returns a list of prime factors of n.
    ex. factorize(24) = [2, 2, 2, 3]
    source: Rossetta code: prime factorization (slightly modified)
    http://rosettacode.org/wiki/Prime_decomposition#Python:_Using_floating_point
    '''
    step = lambda x: 1 + (x<<2) - ((x>>1)<<1)
    maxq = int(math.floor(math.sqrt(n)))
    d = 1
    q = n % 2 == 0 and 2 or 3
    while q <= maxq and n % q != 0:
        q = step(d)
        d += 1
    return q <= maxq and [q] + factorize(n//q) or [n]

def sum_of_divisors(n):
    ''' returns the sum of divisors of integer n.
    n must be a positive integer.
    the sum includes n itself.
    ex. sum_of_divisors(6) = 6 + 3 + 2 + 1 = 12
    '''
    if n == 1:
        return 1
    factors = collections.Counter(factorize(n))
    result = 1
    for p, a in factors.items():
        result *= (p ** (a + 1) - 1)//(p - 1)
    return result

N = int(input())
if N & 1:
    print(sum_of_divisors(N))
else:
    print(sum_of_divisors(N//2))
0