結果

問題 No.220 世界のなんとか2
ユーザー codershifthcodershifth
提出日時 2016-01-26 23:19:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,129 bytes
コンパイル時間 2,002 ms
コンパイル使用メモリ 166,940 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 16:22:54
合計ジャッジ時間 2,843 ms
ジャッジサーバーID
(参考情報)
judge12 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

typedef long long ll;
typedef unsigned long long ull;

#define FOR(i,a,b) for(int (i)=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define RANGE(vec) (vec).begin(),(vec).end()

using namespace std;


class SomethingOfWorld2
{
public:
    void solve(void)
    {
        int p;
        cin>>p;

        // 10^p 以下の数字なので p-1 桁までみればよい
        --p;

        // 3 が含まれるかは桁 DP でやればよい
        // 3 の倍数がいくつあるかを桁 DP に取り入れたい
        //
        // 245 = 2*10^2 + 4*10 + 5
        //     =   2    +  1   + 2 (mod 3)
        //     =   2     (mod 3)
        //
        // 各桁の mod 3 での和がその数字の mod 3 での値となる。
        // そこでその桁までの mod 3 の値を状態として持てばよい
        // 同時に 3 を持つかどうかも状態として持つ
        //
        // dp[i][j][k] := k mod 3 と一致する i 桁目までの数字のうち 3
        //                を持つ/持たない (j==1/0) ものの数
        //
        vector<vector<vector<ll>>> dp(p+1,vector<vector<ll>>(2,vector<ll>(3,0)));

        REP(k,10)
        {
            if ( k == 3 )
                ++dp[0][1][k%3];
            else
                ++dp[0][0][k%3];
        }

        ll ten = 10;
        REP(i,p)
        {
            REP(j,3)
            REP(k,10)
            {
                int l = (ten*k + j)%3; // i 桁目を追加したときの mod 3 の値

                if ( k == 3 )
                    dp[i+1][1][l] += dp[i][0][j];
                else
                    dp[i+1][0][l] += dp[i][0][j];

                dp[i+1][1][l] += dp[i][1][j];
            }
            ten *= 10;
        }

        ll cnt = 0;
        REP(i,3)
            cnt += dp[p][1][i];
        cnt += dp[p][0][0];

        // 0000...00 のケースをのぞく
        cout<<(cnt-1)<<endl;
    }
};

#if 1
int main(int argc, char *argv[])
{
        ios::sync_with_stdio(false);
        auto obj = new SomethingOfWorld2();
        obj->solve();
        delete obj;
        return 0;
}
#endif
0