結果

問題 No.87 Advent Calendar Problem
ユーザー krotonkroton
提出日時 2014-12-03 22:39:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,479 bytes
コンパイル時間 445 ms
コンパイル使用メモリ 53,356 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-02 07:45:07
合計ジャッジ時間 1,648 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;

/*
 現実の 2015/07/23 は木曜日
 日月火水木金土の順に0~6まで数字を振る
 */

bool is_leap(int y){
    if(y % 400 == 0)return true;
    if(y % 100 == 0)return false;
    if(y % 4 == 0)return true;
    return false;
}

int pr[400][7];
int store[3000];

ll solve(ll N){
    memset(pr, -1, sizeof(pr));
    
    int sz = 0;
    int st = -1;
    int w = 4;
    
    for(int y=2015;y<=N;y++){
        int m = y % 400;
        
        if(pr[m][w] != -1){
            st = pr[m][w];
            break;
        } else {
            pr[m][w] = sz;
            store[sz] = (w == 3);
            ++sz;
        }
        
        if(is_leap(y + 1)){
            w = (w + 366) % 7;
        } else {
            w = (w + 365) % 7;
        }
    }
    
    ll res = 0;
    if(st == -1){
        for(int i=0;i<sz;i++)res += store[i];
    } else {
        for(int i=0;i<st;i++)res += store[i];
        
        ll acc = 0;
        for(int i=st;i<sz;i++)acc += store[i];
        
        N -= 2014;
        N -= st;
        
        ll loop_size = sz - st;
        ll k = N / loop_size;
        
        res += acc * k;
        N -= k * loop_size;
        
        for(int i=0;i<N;i++)res += store[i];
    }
    
    return res;
}

int main(){
    ll N;
    cin >> N;

    cout << solve(N) << endl;
    return 0;
}
0