結果

問題 No.220 世界のなんとか2
ユーザー guestguest
提出日時 2016-09-01 23:39:24
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 748 bytes
コンパイル時間 696 ms
コンパイル使用メモリ 67,840 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-09 19:56:43
合計ジャッジ時間 1,835 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>

#define rep(x,to) for(int (x)=0;(x)<(to);(x)++)

using namespace std;
typedef long long ll;


ll cnt(int n){
	string s = "1" + string(n, '0'); n++;
	ll dp[22][2][3][2]={};
	dp[0][0][0][0] = 1; //dp[桁数][3有り][%3][未/確] = 数
	rep(i,n)  rep(j, 2) rep(k, 3) rep(d, 10){
		int mj = j | (d==3);
		int mk = (k + d)%3;
		if(d< s[i]-'0')  dp[i+1][mj][mk][1] += dp[i][j][k][0];
		if(d==s[i]-'0')  dp[i+1][mj][mk][0] += dp[i][j][k][0];
		dp[i+1][mj][mk][1] += dp[i][j][k][1];
	}
	
	ll ans = 0;
	rep(i,2) rep(j,3) if(i==1 || j==0) ans += dp[n][i][j][0] + dp[n][i][j][1];
	return ans - 1; // -1 zero?
}

int main()
{
	int p; cin >>p;
	cout << cnt(p) << endl;
	return 0;
}
0