結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-09-20 00:57:22
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,784 bytes
コンパイル時間 616 ms
コンパイル使用メモリ 80,340 KB
実行使用メモリ 8,760 KB
最終ジャッジ日時 2023-09-26 13:57:29
合計ジャッジ時間 11,229 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
8,144 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 14 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,380 KB
testcase_06 AC 14 ms
4,376 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 1,064 ms
4,376 KB
testcase_13 AC 1,059 ms
4,380 KB
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

//TLE
//桁DP

#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 3*P][lower | has3]

	long long my_mod = 3*P;

	vector<vector<long long>> dp (my_mod, vector<long long>(4, 0));

	dp[0][0] = 1;

	for(int i=0; i<s.size(); i++){
		vector<vector<long long>> dp_(my_mod, vector<long long>(4, 0));

		for(int mod3P=0; mod3P<my_mod; mod3P++)
		for(int lower=0; lower<2; lower++)
		for(int has3=0; has3<2; has3++)
		{
			int state = (lower<<1) | (has3);
			for(int d=0; d<10; d++){
				if(lower==0 && d>s[i]-'0') continue;

				int next_state = (lower || d<s[i]-'0')<<1;
				next_state |= (has3 || d==3);

				int next_mod = (mod3P*10 + d) % my_mod;
			
				long long& dp_now  = dp[mod3P][state];
				long long& dp_next = dp_[next_mod][next_state];
				dp_next += dp_now;
				if(dp_next>=MOD) dp_next -= MOD;
			}
		}

		swap(dp, dp_);
	}

	long long ret = 0;
	for(int mod3P=0; mod3P<my_mod; mod3P++)
	for(int lower=0; lower<2; lower++)
	for(int has3=0; has3<2; has3++)
	{
		int state = (lower<<1) | (has3);
		if( mod3P % P == 0 ) continue;
		if( has3==0 && mod3P%3!=0 ) continue;

		ret += dp[mod3P][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 modP = 0;
	for(int i=0; i<a.size(); i++){
		if(a[i] == '3') has3 = 1;
		mod3 = (mod3*10 + a[i]-'0') % 3;
		modP = (modP*10 + a[i]-'0') % P;
	}
	if( (mod3==0 || has3==1) && modP!=0 ) ans += 1;

	ans = (ans+MOD)%MOD;

	cout << ans << endl;

	return 0;
}
0