結果

問題 No.372 It's automatic
ユーザー koba-e964koba-e964
提出日時 2015-05-26 22:30:06
言語 Java19
(openjdk 21)
結果
AC  
実行時間 2,194 ms / 6,000 ms
コード長 1,112 bytes
コンパイル時間 2,057 ms
コンパイル使用メモリ 74,184 KB
実行使用メモリ 58,140 KB
最終ジャッジ日時 2023-09-20 14:25:42
合計ジャッジ時間 32,386 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 130 ms
55,924 KB
testcase_01 AC 128 ms
55,436 KB
testcase_02 AC 124 ms
55,840 KB
testcase_03 AC 125 ms
55,476 KB
testcase_04 AC 2,160 ms
56,144 KB
testcase_05 AC 2,167 ms
56,076 KB
testcase_06 AC 2,166 ms
56,144 KB
testcase_07 AC 2,173 ms
55,956 KB
testcase_08 AC 2,194 ms
56,284 KB
testcase_09 AC 2,168 ms
56,168 KB
testcase_10 AC 2,169 ms
56,268 KB
testcase_11 AC 2,147 ms
56,044 KB
testcase_12 AC 2,187 ms
56,308 KB
testcase_13 AC 2,145 ms
56,036 KB
testcase_14 AC 2,184 ms
58,128 KB
testcase_15 AC 2,175 ms
55,740 KB
testcase_16 AC 130 ms
55,480 KB
testcase_17 AC 131 ms
55,436 KB
testcase_18 AC 127 ms
55,324 KB
testcase_19 AC 126 ms
53,932 KB
testcase_20 AC 129 ms
56,224 KB
testcase_21 AC 287 ms
58,140 KB
testcase_22 AC 286 ms
56,212 KB
testcase_23 AC 284 ms
55,736 KB
testcase_24 AC 285 ms
56,548 KB
testcase_25 AC 265 ms
56,664 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

class Main {
    public static void main(String[] args) {
	String s;
	int m;
	Scanner sc = new Scanner(System.in);
	s = sc.next();
	m = sc.nextInt();
	new Main(s,m);
    }
    int m;
    final int mod = (int)(1e9 + 7);
    Main(String s, int m) {
	int len = s.length();
	this.m = m;
	int[][] dp = new int[2][m + 3];
	dp[0][0] = 1;
	int t = 0;
	for (int i = 1; i <= len; ++i) {
	    t = 1 - t; // alternating, t == i % 2
	    for (int j = 0; j < m + 3; ++j) {
		dp[t][j] = dp[1 - t][j];
	    }
	    for (int j = 0; j < m + 3; ++j) {
		int pos = next_state(j, s.charAt(i-1));
		dp[t][pos] += dp[1 - t][j];
		dp[t][pos] %= mod;
	    }
	}
	// accepting states are {1, 3}
	System.out.println((dp[len % 2][1] + dp[len % 2][3]) % mod);
    }
    final int next_state(int st, char ch) {
	if (st == 2) { // fail
	    return 2;
	}
	if (st == 1) { // "0"
	    return 2; // This automaton never accepts string of form /0..*/
	}
	if (st == 0) { // init, ""
	    if (ch == '0') {
		return 1; // "0"
	    }
	    return (ch - '0') % m + 3;
	}
	int q = st - 3;
	return (q * 10 + (ch - '0')) % m + 3;
    }
}
0