結果

問題 No.220 世界のなんとか2
ユーザー ty70ty70
提出日時 2016-10-10 04:29:22
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,122 bytes
コンパイル時間 2,931 ms
コンパイル使用メモリ 146,276 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-14 06:02:26
合計ジャッジ時間 2,713 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

ll dp[22][2][2][3]; 	// dp[i桁目][N未満であることが確定しているか 0 -> していない 1 -> している][3 の付く数字か 0 -> そうでない 1 -> そう][mod 3 の値]

ll solve(int p){
	memset (dp, 0LL, sizeof(dp));
	string s = "";
	s += '1';
	rep (i, p) s += '0';
	int m = s.length();

	dp[0][0][0][0] = 1LL;
	for (int i = 0; i < m; ++i){
		int D = (int)(s[i] - '0');
		for (int j = 0; j < 2; ++j){
			for (int k = 0; k < 2; ++k){
				for (int l = 0; l < 3; ++l){
					for (int d = 0; d < (j == 1 ? 10 : D + 1); ++d){
						dp[i + 1][j || d < D][k || d == 3][(l + d) % 3] += dp[i][j][k][l];
					} // end for
				} // end for
			} // end for
		} // end for
	} // end for

	ll res = 0LL;
	rep (j, 2) rep (k, 2) rep (l, 3) if (k == 1 || l == 0) res += dp[m][j][k][l];
	
	return res - 1LL;
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int P; cin >> P;

	ll res = solve(P);

	cout << res << endl;	
	
	return 0;
}
0