結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-09-20 00:56:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 255 ms / 2,000 ms
コード長 2,279 bytes
コンパイル時間 668 ms
コンパイル使用メモリ 81,156 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-26 13:57:01
合計ジャッジ時間 5,889 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 57 ms
4,376 KB
testcase_13 AC 56 ms
4,376 KB
testcase_14 AC 112 ms
4,376 KB
testcase_15 AC 114 ms
4,380 KB
testcase_16 AC 113 ms
4,380 KB
testcase_17 AC 113 ms
4,380 KB
testcase_18 AC 57 ms
4,380 KB
testcase_19 AC 58 ms
4,380 KB
testcase_20 AC 60 ms
4,376 KB
testcase_21 AC 61 ms
4,376 KB
testcase_22 AC 115 ms
4,376 KB
testcase_23 AC 115 ms
4,376 KB
testcase_24 AC 111 ms
4,380 KB
testcase_25 AC 115 ms
4,376 KB
testcase_26 AC 113 ms
4,376 KB
testcase_27 AC 113 ms
4,376 KB
testcase_28 AC 111 ms
4,376 KB
testcase_29 AC 205 ms
4,380 KB
testcase_30 AC 201 ms
4,380 KB
testcase_31 AC 199 ms
4,376 KB
testcase_32 AC 226 ms
4,376 KB
testcase_33 AC 116 ms
4,380 KB
testcase_34 AC 182 ms
4,380 KB
testcase_35 AC 255 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

#define MOD 1000000007

int P;

long long func(string& s){

	//dp[mod_P][lower | has3 | mod3]
	
	vector<vector<long long>> dp (P, vector<long long>(16, 0));
	vector<vector<long long>> dp_(P, vector<long long>(16, 0));

	int keta;
	int tmp = P;
	int two = 0;
	int five = 0;
	while(tmp%2==0){
		tmp/=2;
		two++;
	}
	while(tmp%5==0){
		tmp/=5;
		five++;
	}
	keta = max(two, five);

	dp[0][0b0000] = 1;

	for(int i=0; i<s.size(); i++){
		for(int j=0; j<( (s.size()-i< keta) ?P:1); j++){
			fill(dp_[j].begin(), dp_[j].end(), 0);
		}

		for(int lower=0; lower<2; lower++)
		for(int has3=0; has3<2; has3++)
		for(int mod3=0; mod3<3; mod3++)
		{
			int state = (lower<<3) | (has3<<2) | mod3;
			for(int d=0; d<10; d++){
				if(lower==0 && d>s[i]-'0') continue;
				int next_state = (lower || d<s[i]-'0')<<3;
				next_state |= (has3 || d==3)<<2;
				next_state |= (mod3*10+d)%3;

				if(s.size()-i > keta){
					long long& dp_now = dp[0][state];
					long long& dp_next = dp_[0][next_state];
					dp_next += dp_now;
					if(dp_next>=MOD) dp_next -= MOD;

				}else for(int mod_P=0; mod_P<P; mod_P++){
					long long& dp_now  = dp[mod_P][state];
					long long& dp_next = dp_[(mod_P*10+d)%P][next_state];
					dp_next += dp_now;
					if(dp_next>=MOD) dp_next -= MOD;
				}
			}
		}

		swap(dp, dp_);
	}

	long long ret = 0;
	for(int mod_P=1; mod_P<P; mod_P++){
		if(mod_P % P == 0) continue;

		for(int lower=0; lower<2; lower++)
		for(int has3=0; has3<2; has3++)
		for(int mod3=0; mod3<3; mod3++)
		{
			int state = (lower<<3) | (has3<<2) | mod3;
			if( has3==0 && mod3!=0 ) continue;
			ret += dp[mod_P][state];
			if(ret>=MOD) ret -= MOD;
		}
	}

	return ret;
}

int main(){
	string a,b;
	cin >> a >> b;
	cin >> P;
	

	long long ans = 0;
	ans += func(b);
	ans -= func(a);
	ans = (ans+MOD)%MOD;

	int mod3 = 0;
	int has3 = 0;
	int mod_P = 0;
	for(int i=0; i<a.size(); i++){
		if(a[i] == '3') has3 = 1;
		mod3 = (mod3*10 + a[i]-'0') % 3;
		mod_P = (mod_P*10 + a[i]-'0') % P;
	}
	if( (mod3==0 || has3==1) && mod_P!=0 ) ans += 1;

	ans = (ans+MOD)%MOD;

	cout << ans << endl;

	return 0;
}
0