結果

問題 No.319 happy b1rthday 2 me
ユーザー pekempeypekempey
提出日時 2015-12-12 01:25:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,238 bytes
コンパイル時間 1,170 ms
コンパイル使用メモリ 148,108 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-13 11:34:54
合計ジャッジ時間 2,464 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define GET_MACRO(a, b, c, NAME, ...) NAME
#define rep(...) GET_MACRO(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define rep2(i, a) rep3 (i, 0, a)
#define rep3(i, a, b) for (int i = (a); i < (b); i++)
#define repr(...) GET_MACRO(__VA_ARGS__, repr3, repr2)(__VA_ARGS__)
#define repr2(i, a) repr3 (i, 0, a)
#define repr3(i, a, b) for (int i = (b) - 1; i >= (a); i--)
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { return b < a && (a = b, true); }
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
using namespace std;
typedef long long ll;

ll dp[20][2][2][20][2]; // less, 0-1, num 12, leading zero

pair<ll, ll> f(string S, bool less, bool onetwo) {
    if (S.length() <= 1) {
        int n = S[0] - '0';
        if (n <= 1) {
            return {0, 0};
        } else {
            return {1, 0};
        }
    }
    memset(dp, 0, sizeof(dp));
    int n = S.length();
    dp[0][0][0][0][0] = 1;
    rep (i, n) rep (j, 2) {
        rep (k, 2) rep (l, 19) rep (m, 2) {
            int lim = j ? 9 : S[i] - '0';
            rep (x, lim + 1) {
                if (onetwo && !m && x > 0 && x != 2) continue;
                if (onetwo && i == n - 1 && x != 1) continue;
                if (k == 0 && x == 1) {
                    dp[i + 1][j || x < lim][1][l][m || x > 0] += dp[i][j][k][l][m];
                } else if (k == 1 && x == 1) {
                    dp[i + 1][j || x < lim][1][l][m || x > 0] += dp[i][j][k][l][m];
                } else if (k == 1 && x == 2) {
                    dp[i + 1][j || x < lim][0][l + 1][m || x > 0] += dp[i][j][k][l][m];
                } else {
                    dp[i + 1][j || x < lim][0][l][m || x > 0] += dp[i][j][k][l][m];
                }
            }
        }
    }
    ll res1 = 0, res2 = 0;
    rep (j, 2) rep (k, 2) rep (l, 20) rep (m, 2) {
        if (j == 0 && less) continue;
        res1 += dp[n][j][k][l][m] * l;
        res2 += dp[n][j][k][l][m];
    }
    return {res1, res2 + 1};
}

int main() {
    string A, B;
    cin >> A >> B;
    ll ans = f(B, false, false).first - f(A, true, false).first;
    ans += f(B, true, true).second - f(A, true, true).second;
    cout << ans << endl;
    return 0;
}
0