結果
| 問題 |
No.220 世界のなんとか2
|
| コンテスト | |
| ユーザー |
codershifth
|
| 提出日時 | 2016-01-26 23:19:41 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 1,000 ms |
| コード長 | 2,129 bytes |
| コンパイル時間 | 1,433 ms |
| コンパイル使用メモリ | 165,104 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-21 17:38:21 |
| 合計ジャッジ時間 | 2,354 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 19 |
ソースコード
#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
codershifth