結果

問題 No.220 世界のなんとか2
ユーザー chocobochocobo
提出日時 2018-06-25 19:27:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,976 bytes
コンパイル時間 789 ms
コンパイル使用メモリ 94,668 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 13:53:49
合計ジャッジ時間 1,904 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#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;
}
0