結果

問題 No.2067 ±2^k operations
ユーザー chineristACchineristAC
提出日時 2022-09-02 23:17:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,795 ms / 2,000 ms
コード長 2,116 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 87,168 KB
実行使用メモリ 277,896 KB
最終ジャッジ日時 2023-08-10 05:41:11
合計ジャッジ時間 22,618 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 108 ms
74,360 KB
testcase_01 AC 460 ms
107,544 KB
testcase_02 AC 533 ms
109,176 KB
testcase_03 AC 537 ms
108,552 KB
testcase_04 AC 715 ms
112,252 KB
testcase_05 AC 543 ms
110,072 KB
testcase_06 AC 173 ms
83,616 KB
testcase_07 AC 1,795 ms
267,340 KB
testcase_08 AC 1,654 ms
271,700 KB
testcase_09 AC 1,782 ms
274,100 KB
testcase_10 AC 1,496 ms
263,944 KB
testcase_11 AC 1,777 ms
269,564 KB
testcase_12 AC 1,610 ms
277,896 KB
testcase_13 AC 1,767 ms
269,836 KB
testcase_14 AC 1,733 ms
268,644 KB
testcase_15 AC 1,509 ms
265,028 KB
testcase_16 AC 1,729 ms
270,484 KB
testcase_17 AC 250 ms
87,028 KB
testcase_18 AC 134 ms
81,356 KB
testcase_19 AC 131 ms
81,144 KB
testcase_20 AC 133 ms
81,464 KB
testcase_21 AC 130 ms
81,492 KB
testcase_22 AC 134 ms
81,372 KB
testcase_23 AC 139 ms
81,564 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random,bisect
from collections import deque,defaultdict,Counter
from heapq import heapify,heappop,heappush
from itertools import cycle, permutations
from math import log,gcd

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

memo = {}
def calc(n):
    if n <= 1:
        return n
    
    if n in memo:
        return memo[n]
    
    if n&1 == 1:
        memo[n] = 1 + min(calc(n//2),calc((n+1)//2))
    else:
        memo[n] = calc(n//2)
        
    return memo[n]

A = [calc(i) for i in range(101)]

def calc2(n):
    if n <= 1:
        return n
    
    if n&1:
        if (n//2) & 1:
            return 1 + calc2((n+1)//2)
        else:
            return 1 + calc2((n)//2)
    else:
        return calc2(n//2)

memo3 = {}
def calc3(n):
    if n in memo3:
        return memo3[n]
    if n <= 1:
        return n
    
    if n&1:
        if n%4 == 3:
            memo3[n] = 1 + calc3((n+1)//2)
            return 1 + calc3((n+1)//2)
        else:
            memo3[n] = 1 + calc3((n)//2)
            return 1 + calc3((n)//2)
    else:
        memo3[n] = calc3((n)//2)
        return calc3(n//2)
        
memo = {}
def f(n):
    if n in memo:
        return memo[n]
    """
    sum calc(i) for i in 0...n
    """
    q,r = (n+1)//4,(n+1)%4
    res = 0
    for i in range(r):
        res += calc3(4*q+i)
    
    """
    sum calc(i) for i in 0123,4567,...,(4*q-4),(4*q-3),(4*q-2),(4*q-1)
    
    r
    0: calc(0) + calc(4//2) + calc(8//2) + ... calc((4*q-4)//2) = calc(0) + calc(1) + calc(2) + ... calc(q-1)
    1: calc(0) + calc(4//2) + calc(8//2) + ... calc((4*q-4)//2) = calc(0) + calc(1) + calc(2) + ... calc(q-1) + q
    2: calc(2//2) + calc(6//2) + calc(10//2) + ... calc((4*q-2)//2) = calc(1) + calc(3) + calc(5) + ... calc(2*q-1)
    3: calc(4//2) + calc(8//2) + ... calc((4*q)//2) = calc(1) + calc(2) + ... + calc(q) + q

    sum = f(q-1) + f(2*q-1) + f(q)
    """

    if q!=0:
        res += 2 * f(q-1) + f(2*q-1) + calc3(q) + 2*q
    
    memo[n] = res
    
    return res

for _ in range(int(input())):
    print(f(int(input())))
0