結果

問題 No.260 世界のなんとか3
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2016-08-12 10:40:26
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 2,353 bytes
コンパイル時間 650 ms
コンパイル使用メモリ 58,336 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-25 07:52:44
合計ジャッジ時間 3,833 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 8 ms
11,008 KB
testcase_01 AC 8 ms
10,880 KB
testcase_02 AC 8 ms
11,008 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 36 ms
11,008 KB
testcase_06 AC 26 ms
11,008 KB
testcase_07 AC 107 ms
11,008 KB
testcase_08 AC 76 ms
10,880 KB
testcase_09 AC 46 ms
10,880 KB
testcase_10 AC 121 ms
10,880 KB
testcase_11 AC 114 ms
11,008 KB
testcase_12 AC 71 ms
11,008 KB
testcase_13 AC 25 ms
10,880 KB
testcase_14 AC 103 ms
10,880 KB
testcase_15 AC 33 ms
10,880 KB
testcase_16 AC 91 ms
10,880 KB
testcase_17 AC 73 ms
10,880 KB
testcase_18 AC 71 ms
11,008 KB
testcase_19 AC 90 ms
11,008 KB
testcase_20 AC 66 ms
10,880 KB
testcase_21 AC 59 ms
11,008 KB
testcase_22 AC 103 ms
10,880 KB
testcase_23 AC 16 ms
11,008 KB
testcase_24 AC 81 ms
10,880 KB
testcase_25 AC 102 ms
11,008 KB
testcase_26 AC 60 ms
11,008 KB
testcase_27 AC 8 ms
10,880 KB
testcase_28 AC 112 ms
11,008 KB
testcase_29 AC 196 ms
10,752 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;
		}
	}
	// printf("ans %lld\n", ans);
	return ans;
}

bool hantei(void){
	bool flag = false;
	rep(i, a.size()){//数字3を含んでいる
		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) return true;
	//8の倍数であるかを数字が大きい場合下三桁が8の倍数であるかで判定
	bool flag_8bai = false; int tm;
	// printf("ok\n");
	// cout << a << endl;
	if(a.size() > 6){
		string tmp = a.substr(a.size() - 3, 3);
		// cout << tmp << endl;
		tm = stoi(tmp);
	}else{
		tm = stoi(a);
		// cout << tm << endl;
	}
	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);
	// printf("ans_a %lld\n", ans_a);
	long long ans_b = slv(b);
	// printf("ans_b %lld\n", ans_b);
	
	if(hantei()){
		cout << (ans_b - ans_a + 1 + mod) % mod<< endl;
	}else{
		cout << (ans_b - ans_a + mod) % mod << endl;
	}
	return 0;
}

0