結果

問題 No.87 Advent Calendar Problem
ユーザー xuzijian629xuzijian629
提出日時 2018-10-30 02:42:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,057 bytes
コンパイル時間 6,088 ms
コンパイル使用メモリ 320,056 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-30 00:31:50
合計ジャッジ時間 7,405 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = int64_t;
using vi = vector<i64>;
using vvi = vector<vi>;

class Calendar {
    regex yyyymmdd = regex("^\\d{4}/\\d{2}/\\d{2}$");
    regex mmdd = regex("^\\d{2}/\\d{2}$");
    const vvi days = {
        {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
        {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
    };

public:
    // [year1, year2)
    i64 leap_count(i64 year1, i64 year2) {
        i64 cnt = year2 / 4 - year1 / 4;
        cnt -= (year2 / 100 - year1 / 100);
        cnt += (year2 / 400 - year1 / 400);
        return cnt;
    }

    bool is_leap(i64 year) {
        return year % 400 == 0 || (year % 4 == 0 && year % 100);
    }

    i64 date_count(i64 year1, i64 year2) {
        int c = leap_count(year1, year2);
        return 365 * (year2 - year1 - c) + 366 * c; 
    }

    // 0-indexed
    i64 ith_date(string date, bool is_leap = false) {
        assert(regex_match(date.begin(), date.end(), mmdd));
        int m = stoi(date.substr(0, 2)) - 1;
        int d = stoi(date.substr(3, 2)) - 1;
        return accumulate(days[is_leap].begin(), days[is_leap].begin() + m, 0) + d;
    }

    // 0-indexed
    i64 ith_date_posix(string date) {
        assert(regex_match(date.begin(), date.end(), yyyymmdd));
        int y = stoi(date.substr(0, 4));
        int m = stoi(date.substr(5, 2)) - 1;
        int d = stoi(date.substr(8, 2)) - 1;
        return date_count(1970, y) + ith_date(date.substr(5, 5), is_leap(y));
    }   

    string to_string(int date, bool is_leap = false) {
        int m = 0;
        while (date - days[is_leap][m] >= 0) {
            date -= days[is_leap][m++];
        }
        stringstream ss;
        ss << setw(2) << setfill('0') << m + 1 << "/" << setw(2) << setfill('0') << date + 1;
        return ss.str();
    }

    string to_string_posix(int date) {
        int l = 1970, r = 10000;
        while (l < r - 1) {
            int m = (l + r) / 2;
            if (date_count(1970, m) <= date) {
                l = m;
            } else {
                r = m;
            }
        }
        stringstream ss;
        ss << setw(4) << setfill('0') << l << "/" << to_string(date - date_count(1970, l), is_leap(l));
        return ss.str();
    }

    int get_day(int date) {
        // 0: Sun, 1: Mon, ...
        // 1970/01/01 is Thu
        return (date % 7 + 4) % 7;
    }
};

int main() {
    Calendar cal;
    vi sum(2801);
    for (int i = 1; i <= 2800; i++) {
        stringstream ss;
        ss << setw(4) << setfill('0') << 2000 + i;
        int k = cal.ith_date_posix(ss.str() + "/07/23");
        if (cal.get_day(k) == 3) {
            sum[i] = sum[i - 1] + 1;
        } else {
            sum[i] = sum[i - 1];
        }
    }

    i64 n;
    cin >> n;
    n -= 2000;
    if (n <= 2800) {
        cout << sum[n] - sum[14] << endl;
    } else {
        i64 cnt = sum[2800] - sum[14];
        i64 range = (n + 2799) / 2800 - 2;
        cnt += sum[2800] * range;
        cnt += sum[(n - 1) % 2800 + 1];
        cout << cnt << endl;
    }
}
0