結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-12-01 05:34:35
言語 Java21
(openjdk 21)
結果
AC  
実行時間 723 ms / 2,000 ms
コード長 1,640 bytes
コンパイル時間 5,302 ms
コンパイル使用メモリ 73,900 KB
実行使用メモリ 60,692 KB
最終ジャッジ日時 2023-10-12 07:23:52
合計ジャッジ時間 17,506 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,788 KB
testcase_01 AC 124 ms
55,736 KB
testcase_02 AC 189 ms
56,404 KB
testcase_03 AC 116 ms
55,704 KB
testcase_04 AC 118 ms
55,616 KB
testcase_05 AC 143 ms
56,552 KB
testcase_06 AC 207 ms
56,736 KB
testcase_07 AC 143 ms
56,376 KB
testcase_08 AC 140 ms
56,064 KB
testcase_09 AC 115 ms
55,336 KB
testcase_10 AC 119 ms
55,612 KB
testcase_11 AC 117 ms
55,832 KB
testcase_12 AC 303 ms
56,168 KB
testcase_13 AC 302 ms
56,584 KB
testcase_14 AC 451 ms
60,692 KB
testcase_15 AC 449 ms
58,204 KB
testcase_16 AC 465 ms
58,768 KB
testcase_17 AC 492 ms
58,620 KB
testcase_18 AC 337 ms
57,300 KB
testcase_19 AC 333 ms
56,980 KB
testcase_20 AC 366 ms
58,972 KB
testcase_21 AC 357 ms
59,048 KB
testcase_22 AC 490 ms
58,896 KB
testcase_23 AC 492 ms
58,904 KB
testcase_24 AC 466 ms
57,268 KB
testcase_25 AC 494 ms
59,040 KB
testcase_26 AC 464 ms
57,028 KB
testcase_27 AC 451 ms
58,880 KB
testcase_28 AC 506 ms
58,632 KB
testcase_29 AC 644 ms
58,856 KB
testcase_30 AC 650 ms
58,692 KB
testcase_31 AC 633 ms
59,156 KB
testcase_32 AC 619 ms
59,252 KB
testcase_33 AC 460 ms
58,872 KB
testcase_34 AC 562 ms
58,600 KB
testcase_35 AC 723 ms
58,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class Main{
	private long MOD = 1000000007;
	private long calc(char[] n, int p){
		long dp[][][];
		dp = new long[2][p][16];
		dp[0][0][0] = 1;
		int u = 0;
		int v = 1;
		int sz = n.length;
		for(int i=0; i<sz; i++){
			int k = n[i] - '0';
			for(int j=0; j<(i<sz-5?1:p); j++){
				for(int a=0;a<2;a++){
				for(int b=0;b<3;b++){
				for(int c=0;c<2;c++){
					int s = (a<<3)|(b<<1)|c;
					long val = dp[u][j][s];
					dp[u][j][s] = 0;
					for(int d=0;d<10;d++){
						if(a==0 && d>k) continue;
						int a_ = a | (d<k?1:0);
						int b_ = (b+d)%3;
						int c_ = c | (d==3?1:0);
						int s_ = (a_<<3)|(b_<<1)|c_;
						int j_ = (i<sz-5?0:(j*10+d)%p);
						dp[v][j_][s_] += val;
						if(dp[v][j_][s_]>=MOD) dp[v][j_][s_] -= MOD;
					}
				}}}
			}
			u ^= 1;
			v ^= 1;
		}
		long ret = 0;

		for(int j=1; j<p; j++){
		for(int a=0;a<2;a++){
		for(int b=0;b<3;b++){
		for(int c=0;c<2;c++){
			if(b!=0 && c==0) continue;
			int s = (a<<3)|(b<<1)|c;
			ret += dp[u][j][s];
			if(ret >= MOD) ret-=MOD;
		}}}}

		return ret;
	}

	private void solver(){

		Scanner s = new Scanner(System.in);
		char[] a = s.next().toCharArray();
		char[] b = s.next().toCharArray();
		int p = Integer.parseInt(s.next());

		long ans = calc(b, p);
		ans = (ans - calc(a,p) + MOD) % MOD;
		int x = 0;
		int y = 0;
		int z = 0;
		for(int i=0; i<a.length; i++){
			int k = a[i] - '0';
			x = (x+k)%3;
			y |= k==3?1:0;
			z = (z*10+k)%p;
		}
		if((x==0 || y==1) && z!=0){
			ans += 1;
			ans %= MOD;
		}

		System.out.println(ans);
	}


	public static void main(String[] args){
		Main hoge = new Main();
		hoge.solver();
	}
}
0