結果

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

テストケース

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

ソースコード

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) << endl;
}
0