結果
| 問題 | No.87 Advent Calendar Problem | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2014-12-03 23:08:52 | 
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 5,000 ms | 
| コード長 | 1,482 bytes | 
| コンパイル時間 | 555 ms | 
| コンパイル使用メモリ | 56,788 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-06-11 14:42:38 | 
| 合計ジャッジ時間 | 1,430 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 24 | 
ソースコード
#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[st+i];
    }
    
    return res;
}
int main(){
    ll N;
    cin >> N;
    cout << solve(N) << endl;
    return 0;
}
            
            
            
        