結果

問題 No.315 世界のなんとか3.5
ユーザー 👑 kmjpkmjp
提出日時 2015-12-08 01:19:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 381 ms / 2,000 ms
コード長 2,397 bytes
コンパイル時間 1,309 ms
コンパイル使用メモリ 150,732 KB
実行使用メモリ 34,092 KB
最終ジャッジ日時 2023-10-12 20:33:15
合計ジャッジ時間 6,520 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,264 KB
testcase_01 AC 6 ms
6,320 KB
testcase_02 AC 11 ms
14,432 KB
testcase_03 AC 7 ms
6,264 KB
testcase_04 AC 8 ms
6,376 KB
testcase_05 AC 7 ms
6,248 KB
testcase_06 AC 7 ms
6,288 KB
testcase_07 AC 6 ms
6,276 KB
testcase_08 AC 14 ms
14,452 KB
testcase_09 AC 14 ms
14,416 KB
testcase_10 AC 14 ms
14,420 KB
testcase_11 AC 14 ms
14,444 KB
testcase_12 AC 74 ms
24,092 KB
testcase_13 AC 74 ms
23,956 KB
testcase_14 AC 125 ms
24,120 KB
testcase_15 AC 124 ms
24,244 KB
testcase_16 AC 125 ms
24,212 KB
testcase_17 AC 127 ms
24,280 KB
testcase_18 AC 74 ms
24,012 KB
testcase_19 AC 74 ms
24,180 KB
testcase_20 AC 75 ms
24,108 KB
testcase_21 AC 74 ms
24,112 KB
testcase_22 AC 125 ms
24,148 KB
testcase_23 AC 125 ms
24,120 KB
testcase_24 AC 124 ms
24,120 KB
testcase_25 AC 126 ms
24,280 KB
testcase_26 AC 125 ms
24,132 KB
testcase_27 AC 125 ms
24,216 KB
testcase_28 AC 177 ms
24,124 KB
testcase_29 AC 283 ms
33,924 KB
testcase_30 AC 284 ms
34,092 KB
testcase_31 AC 278 ms
33,860 KB
testcase_32 AC 230 ms
34,040 KB
testcase_33 AC 132 ms
33,680 KB
testcase_34 AC 357 ms
33,824 KB
testcase_35 AC 381 ms
33,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

string A,B;
ll P;
ll mo=1000000007;
ll p10[202020];
ll memo[202020][3][2];

ll dfs(string& S,int d,int m,int lead) {
	if(d>=S.size()) return (m==0);
	if(memo[d][m][lead]>=0) return memo[d][m][lead];
	ll ret=0;
	int i;
	
	if(lead==1) {
		FOR(i,S[d]-'0') {
			if(i==3) ret+=p10[S.size()-1-d];
			else ret += dfs(S,d+1,(m+i)%3,0);
		}
		if(S[d]=='3') {
			ll pat=0;
			for(i=d+1;i<=S.size()-1;i++) pat=(pat*10+(S[i]-'0'))%mo;
			ret += pat+1;
			
		}
		else {
			ret += dfs(S,d+1,(m+S[d]-'0')%3,1);
		}
	}
	else {
		FOR(i,10) {
			if(i==3) ret += p10[S.size()-1-d];
			else ret += dfs(S,d+1,(m+i)%3,0);
		}
	}
	return memo[d][m][lead]=ret%mo;
}

string decdec(string A) {
	reverse(A.begin(),A.end());
	FORR(r,A) {
		if(r--!='0') break;
		r='9';
	}
	if(A.back()=='0') A.resize(A.size()-1);
	reverse(A.begin(),A.end());
	return A;
}


int greed(int v,int p) {
	int ret=0;
	for(int i=1;i<=v;i++) {
		if(i%p==0) continue;
		int ok=(i%3==0);
		int j=i;
		while(j) {
			if(j%10==3) ok=1;
			j/=10;
		}
		ret+=ok;
	}
	return ret;
}

ll hoge(string S,ll P) {
	ll dp[2][3],t[2]={};
	if(S.size()<=5) return greed(atol(S.c_str()),P);
	ll ret=0;
	int low=atol(S.substr(S.size()-5).c_str());
	int i;
	S=S.substr(0,S.size()-5);
	MINUS(memo);
	FOR(i,3) dp[0][i]=dfs(S,0,i,1);
	FORR(r,S) t[0]=(t[0]*10+r-'0')%mo;
	
	S=decdec(S);
	MINUS(memo);
	FOR(i,3) dp[1][i]=dfs(S,0,i,1);
	FORR(r,S) t[1]=(t[1]*10+r-'0')%mo;
	
	FOR(i,100000) {
		int v=i;
		if(i%P==0) continue;
		while(v) {
			if(v%10==3) break;
			v/=10;
		}
		if(v) ret+=t[(i>low)]+1;
		else ret += dp[(i>low)][i%3];
	}
	
	return ret%mo;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	p10[0]=1;
	FOR(i,202000) p10[i+1]=p10[i]*10%mo;
	
	cin>>A>>B>>P;
	A=decdec(A);
	cout<<((hoge(B,P)+mo-hoge(A,P))%mo)<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0