結果

問題 No.220 世界のなんとか2
ユーザー zawakasuzawakasu
提出日時 2022-09-14 14:22:19
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,432 bytes
コンパイル時間 2,350 ms
コンパイル使用メモリ 211,392 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-21 13:49:25
合計ジャッジ時間 3,319 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

#define all(x) begin(x), end(x)

using namespace std;
using i32 = int;
using i64 = long long;
using ld = long double;

template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {return a < b and (a = b, true);}

template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {return a > b and (a = b, true);}

const i64 supl = LONG_LONG_MAX / 2 - 100;
const i32 supi = INT_MAX / 2 - 100;

i64 rec(vector<vector<vector<vector<i64>>>>& dp, string& s, vector<i32>& pows, 
        i32 dig, bool tight, bool three, i32 mod) {

    i64& res = dp[dig][tight][three][mod];
    if (~res) {
        return res;
    }
    res = 0;
    if (dig == (i32)s.size()) {
        return res = three or mod == 0;
    }
    i32 up = (tight ? s[dig] - '0' : 9);
    for (i32 i = 0 ; i <= up ; i++) {
        res += rec(dp, s, pows, 
                dig + 1, tight and i == up, three or i == 3, (mod + i * pows[dig]) % 3);
    }
    return res;
}

void main_() {
    i32 p; cin >> p;
    string s = "1";
    s += string(p, '0');
    vector pows(p + 1, 1);
    for (i32 i = 1 ; i <= p ; i++) {
        pows[i] = pows[i - 1] * 10;
        pows[i] %= 3;
    }
    reverse(all(pows));
    vector dp(s.size() + 1, vector(2, vector(2, vector(3, -1LL))));
    cout << rec(dp, s, pows, 0, true, false, 0) - 1 << endl;
}

int main() {
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    main_();

    return 0;
}
0