結果
問題 | No.220 世界のなんとか2 |
ユーザー |
![]() |
提出日時 | 2022-09-14 14:22:19 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 4 ms / 1,000 ms |
コード長 | 1,432 bytes |
コンパイル時間 | 3,365 ms |
コンパイル使用メモリ | 206,332 KB |
最終ジャッジ日時 | 2025-02-07 05:19:17 |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 19 |
ソースコード
#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;}