結果

問題 No.315 世界のなんとか3.5
ユーザー 👑 kmjpkmjp
提出日時 2015-12-08 00:55:18
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,373 bytes
コンパイル時間 1,485 ms
コンパイル使用メモリ 150,380 KB
実行使用メモリ 33,980 KB
最終ジャッジ日時 2023-10-12 20:27:22
合計ジャッジ時間 7,176 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,184 KB
testcase_01 AC 6 ms
6,192 KB
testcase_02 AC 12 ms
14,448 KB
testcase_03 AC 8 ms
6,220 KB
testcase_04 AC 7 ms
6,200 KB
testcase_05 AC 7 ms
6,272 KB
testcase_06 AC 8 ms
6,284 KB
testcase_07 AC 7 ms
6,180 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 133 ms
24,188 KB
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 385 ms
33,980 KB
testcase_35 AC 409 ms
33,804 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') {
			for(i=d+1;i<=S.size()-1;i++) ret=(ret*10+(S[i]-'0'))%mo;
			ret++;
		}
		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