結果

問題 No.260 世界のなんとか3
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-08-19 00:26:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 184 ms / 2,000 ms
コード長 2,233 bytes
コンパイル時間 705 ms
コンパイル使用メモリ 58,492 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-25 09:03:26
合計ジャッジ時間 3,298 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
10,880 KB
testcase_01 AC 7 ms
10,880 KB
testcase_02 AC 8 ms
11,008 KB
testcase_03 AC 143 ms
10,880 KB
testcase_04 AC 141 ms
11,008 KB
testcase_05 AC 32 ms
10,880 KB
testcase_06 AC 23 ms
11,008 KB
testcase_07 AC 96 ms
10,880 KB
testcase_08 AC 69 ms
10,880 KB
testcase_09 AC 40 ms
11,008 KB
testcase_10 AC 107 ms
10,880 KB
testcase_11 AC 106 ms
11,008 KB
testcase_12 AC 65 ms
11,008 KB
testcase_13 AC 24 ms
11,008 KB
testcase_14 AC 92 ms
11,008 KB
testcase_15 AC 28 ms
10,880 KB
testcase_16 AC 83 ms
11,008 KB
testcase_17 AC 73 ms
10,752 KB
testcase_18 AC 67 ms
11,008 KB
testcase_19 AC 81 ms
10,752 KB
testcase_20 AC 59 ms
11,008 KB
testcase_21 AC 52 ms
10,880 KB
testcase_22 AC 91 ms
11,008 KB
testcase_23 AC 16 ms
10,752 KB
testcase_24 AC 71 ms
10,880 KB
testcase_25 AC 91 ms
11,008 KB
testcase_26 AC 55 ms
10,880 KB
testcase_27 AC 7 ms
10,880 KB
testcase_28 AC 100 ms
11,008 KB
testcase_29 AC 184 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
static const int mod = 1e9 + 7;
//dp[i][j][k][l][m]
//整数pを考えた時に、i番目の桁まで考えて、j=1の時は考えている数がp未満であることが決定していて、j=0の時はp以下である。
//k=1の時はすでにi番目までに数字3を含んでいて、k=0の時は含まれていない。 lはi番目の桁まで考えた時のmod3の値
//mはi番目まで考えた時のmod8の値
long long dp[10010][2][2][3][8];
string a, b;

long long slv(string s){
	rep(i, 10010)rep(j, 2)rep(k, 2)rep(l, 3)rep(m, 8){
		dp[i][j][k][l][m] = 0;
	}

	dp[0][0][0][0][0] = 1;
	rep(i, s.size()){
		rep(j, 2){

			int lim;
			if(j == 1) lim = 9;
			else lim = s[i] - '0';
			
			rep(k, 2) rep(l, 3) rep(m, 8) rep(d, lim + 1){//dは末尾に追加する数字
				(dp[i + 1][j || d < lim][k || d == 3][(l + d) % 3][(10 * m + d) % 8] += dp[i][j][k][l][m]) %= mod;
			}
		}
	}

	long long ans = 0;
	rep(j, 2) rep(k, 2) rep(l, 3) rep(m, 8){
		if(k == 1|| l == 0){//3が含まれているまたは3の倍数
			if(m % 8 != 0)
			(ans += dp[s.size()][j][k][l][m]) %= mod;
		}
	}
	return ans;
}

bool hantei(void){
	//数字の3を含んでいるか判定
	bool flag = false;
	rep(i, a.size()){
		if(a[i] == '3'){
			flag = true; break;
		}
	}
	//3の倍数であるかをすべての桁の数字を足して3の倍数であるかで判定
	bool flag_3bai = false;
	long long sum = 0;
	rep(i, a.size()){	
		sum += (a[i] - '0');
	}
	if(sum % 3 == 0) flag_3bai = true;
	//8の倍数であるかを数字が大きい場合下三桁が8の倍数であるかで判定
	bool flag_8bai = false; int tm;
	if(a.size() > 6){
		string tmp = a.substr(a.size() - 3, 3);
		tm = stoi(tmp);
	}else{
		tm = stoi(a);
	}
	if(tm % 8 == 0) flag_8bai = true;

	if((flag || flag_3bai) && !flag_8bai) return true;
	else return false;
}

int main(void){
	cin >> a >> b;
	long long ans_a = slv(a);
	long long ans_b = slv(b);
	
	if(hantei()){//aが条件を満たす数字の時
		cout << (ans_b - ans_a + 1 + mod) % mod<< endl;
	}else{//満たさない時
		cout << (ans_b - ans_a + mod) % mod << endl;
	}
	return 0;
}
0