結果
| 問題 |
No.220 世界のなんとか2
|
| コンテスト | |
| ユーザー |
chocobo
|
| 提出日時 | 2018-06-25 19:27:12 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 1,000 ms |
| コード長 | 1,976 bytes |
| コンパイル時間 | 847 ms |
| コンパイル使用メモリ | 97,268 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-06-30 22:40:47 |
| 合計ジャッジ時間 | 1,659 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <typeinfo>
#include <numeric>
#include <functional>
#include <unordered_map>
#include <bitset>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll INF = 1e16;
const ll MOD = 1e9 + 7;
#define REP(i, n) for(ll i = 0; i < n; i++)
string n = "1";
ll dp1[22][2][4], dp2[22][2][2], dp3[22][2][4][2];
ll rec1(ll k, bool tight, ll sum){
// cout << k << ' ' << tight << ' ' << sum << endl;
if(k == n.size())return (dp1[k][tight][sum] = (sum == 0));
if(~dp1[k][tight][sum])return dp1[k][tight][sum];
ll res = 0;
for(ll i = 0; i <= ((tight)? n[k] - '0' : 9); i++){
res += rec1(k + 1, tight && (i == n[k] - '0'), (sum + i) % 3);
}
return (dp1[k][tight][sum] = res);
}
ll rec2(ll k, bool tight, bool f){
if(~dp2[k][tight][f])return dp2[k][tight][f];
if(k == n.size())return dp2[k][tight][f] = f;
ll res = 0;
for(ll i = 0; i <= ((tight)? n[k] - '0' : 9); i++){
res += rec2(k + 1, tight && (i == n[k] - '0'), f || (i == 3));
}
return dp2[k][tight][f] = res;
}
ll rec3(ll k, bool tight, ll sum, bool f){
if(~dp3[k][tight][sum][f])return dp3[k][tight][sum][f];
if(k == n.size())return dp3[k][tight][sum][f] = ((sum == 0) && f);
ll res = 0;
for(ll i = 0; i <= ((tight)? n[k] - '0' : 9); i++){
res += rec3(k + 1, tight && (i == n[k] - '0'), (sum + i) % 3, f || (i == 3));
}
return dp3[k][tight][sum][f] = res;
}
int main() {
ll p;
cin >> p;
REP(i, p){
n += '0';
}
memset(dp1, -1, sizeof(dp1));
memset(dp2, -1, sizeof(dp2));
memset(dp3, -1, sizeof(dp3));
cout << rec1(0, true, 0) + rec2(0, true, false) - rec3(0, true, 0, false) - 1 << endl;
}
chocobo