結果

問題 No.315 世界のなんとか3.5
ユーザー NekosyndromeNekosyndrome
提出日時 2015-12-11 21:40:39
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,083 bytes
コンパイル時間 1,160 ms
コンパイル使用メモリ 147,976 KB
実行使用メモリ 22,944 KB
最終ジャッジ日時 2023-10-13 11:25:05
合計ジャッジ時間 5,313 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 6 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 3 ms
4,352 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 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 WA -
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 122 ms
22,608 KB
testcase_35 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define REP(x,y,z) for(int x=y;x<=z;x++)
#define FORD(x,y,z) for(int x=y;x>=z;x--)
#define MSET(x,y) memset(x,y,sizeof(x))
#define FOR(x,y) for(__typeof(y.begin()) x=y.begin();x!=y.end();x++)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 200005
#define MOD 1000000007
void RI(){}
template<typename... T>
void RI( int& head, T&... tail ) {
    scanf("%d",&head);
    RI(tail...);
}
using namespace std;
typedef long long LL;
int p;
char a[M],b[M];
LL dp[M][2][2][3];//pos, same, has3, %3

void add(LL &x,LL y)
{
	x += y;
	if(x>=MOD) x%=MOD;
	if(x<0) x = (x%MOD)+MOD;
}
bool has3(int x)
{
	while(x)
	{
		if(x%10==3) return 1;
		x /= 10;
	}
	return false;
}
int solve2(char in[],int same)
{
	int n,re=0;
	sscanf(in, "%d", &n);
	if(!same) n--;
	REP(i,0,n) if(i%3==0||has3(i)) if(i%p!=0) re++;
	return re;
}
LL solve(char in[],int same)
{
	int n=strlen(in);
	if(n<=6)
	{
		return solve2(in,same);
	}

	int ni,nj,nk,nl;
	LL re=0;
	MSET(dp,0);
	REP(i,0,n-1) in[i]-='0';

	REP(i,0,in[0]-1) dp[0][0][i==3][i%3]++;
	dp[0][1][in[0]==3][in[0]%3]++;

	REP(i,0,n-2)REP(j,0,1)REP(k,0,1)REP(l,0,2) if(dp[i][j][k][l])
	{
		REP(num,0,9)
		{
			if(j==1 && num>in[i+1]) continue;
			ni = i+1;
			nj = (j==1 && num==in[i+1]);
			nk = k; if(num==3) nk=1;
			nl = (l+num)%3;
			add(dp[ni][nj][nk][nl], dp[i][j][k][l]);
		}
	}

	REP(j,0,same) re += dp[n-1][j][0][0];
	REP(j,0,same)REP(l,0,2) re += dp[n-1][j][1][l];

	/////
	int end=n-4; if(p==80) end=n-5; if(p==800)end=n-6;
	int mult=1; if(p==80) mult=10; if(p==800)mult=100;
	int tail, tail2;
	sscanf(in+end+1, "%d", &tail);
	
	REP(j,0,1)REP(k,0,1)REP(l,0,2)for(int num=0; num<1000; num+=8)
	{
		//bigger
		tail2 = num*mult;
		if(same && j && tail2>tail) continue;
		if(!same && j && tail2>=tail) continue;

		//no 3
		if(k==0 && (l+num)%3!=0 && !has3(num)) continue;

		add(re, -dp[end][j][k][l]);
	}
	return re;
}
int main()
{
	LL ans;
	while(~scanf("%s%s%d",a,b,&p))
	{
		ans = solve(b,1)-solve(a,0);
		ans = ((ans%MOD)+MOD)%MOD;
		printf("%lld\n",ans);
	}
	return 0;
}
0