結果

問題 No.87 Advent Calendar Problem
ユーザー yuki2006yuki2006
提出日時 2014-12-03 21:14:41
言語 Python2
(2.7.18)
結果
AC  
実行時間 15 ms / 5,000 ms
コード長 2,310 bytes
コンパイル時間 675 ms
コンパイル使用メモリ 6,700 KB
実行使用メモリ 6,472 KB
最終ジャッジ日時 2023-09-02 07:45:05
合計ジャッジ時間 2,671 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
6,208 KB
testcase_01 AC 14 ms
6,332 KB
testcase_02 AC 15 ms
6,208 KB
testcase_03 AC 14 ms
6,292 KB
testcase_04 AC 14 ms
6,212 KB
testcase_05 AC 15 ms
6,472 KB
testcase_06 AC 14 ms
6,432 KB
testcase_07 AC 13 ms
6,288 KB
testcase_08 AC 14 ms
6,208 KB
testcase_09 AC 14 ms
6,288 KB
testcase_10 AC 14 ms
6,336 KB
testcase_11 AC 15 ms
6,332 KB
testcase_12 AC 15 ms
6,216 KB
testcase_13 AC 14 ms
6,320 KB
testcase_14 AC 14 ms
6,244 KB
testcase_15 AC 14 ms
6,428 KB
testcase_16 AC 14 ms
6,320 KB
testcase_17 AC 14 ms
6,288 KB
testcase_18 AC 14 ms
6,216 KB
testcase_19 AC 14 ms
6,240 KB
testcase_20 AC 14 ms
6,220 KB
testcase_21 AC 15 ms
6,332 KB
testcase_22 AC 14 ms
6,208 KB
testcase_23 AC 14 ms
6,212 KB
testcase_24 AC 15 ms
6,340 KB
testcase_25 AC 14 ms
6,452 KB
testcase_26 AC 14 ms
6,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-
# メモ
#
# 100年ごと5ずれるが 最後の100年だけ6ずれる(400年ごとにうるう年)
# 400年ごとに 21ずれる (周期性
# 7月23日は12月31日と同じ曜日であるという事実に基づいている。
# 日月火・・・ が0,1,2,3,4,5,6,7となる。
from collections import Counter


def solve(N, start):
    diff = 0
    countY100 = Counter()
    for i in xrange(1, 99 + 1):
        if i % 4 == 0:
            diff += 2
        else:
            diff += 1
        diff %= 7
        countY100[diff] += 1

    # 100年目のうるう年じゃない処理
    diff += 1
    countY100[diff] += 1

    nextDiff = 0
    countY400 = Counter()

    # 400年分計算する
    for _ in xrange(4):

        # 100年ごとセットで繰り返す
        for j in xrange(7):
            countY400[(j + nextDiff) % 7] += countY100[j]

        nextDiff += diff
        nextDiff %= 7


    # 400年ごとのうるう年の処理
    countY400[(nextDiff + 1) % 7] += 1
    countY400[nextDiff] -= 1

    M = N
    repY400 = (M // 400)
    M %= 400

    repY100 = M // 100
    M %= 100

    repY1 = M

    resultCount = Counter()

    # 400年ごとに曜日は繰り返される。
    if repY400 > 0:
        for i in xrange(7):
            resultCount[i] = countY400[i] * repY400

    # 100年ごとに曜日を5つずらす
    diff = 0
    for _ in xrange(repY100):
        for j in xrange(7):
            resultCount[(j + diff) % 7] += countY100[j]

        diff += 5

    # 1~99年はシミュレーション
    for i in xrange(1, repY1 + 1):
        if i % 4 == 0:
            diff += 2
        else:
            diff += 1

        diff %= 7
        resultCount[diff] += 1


    # ちょい面倒なので
    # 2014 ~ 1 のぶんをシミュレーションで引く
    day = 0
    for year in xrange(1, start + 1):

        if year % 400 == 0:
            day += 366
        elif year % 100 == 0:
            day += 365
        elif year % 4 == 0:
            day += 366
        else:
            day += 365

        resultCount[day % 7] -= 1

    return resultCount

N = int(raw_input())
start = 2014
resultCount = solve(N, start)
print resultCount[3]
0