結果

問題 No.372 It's automatic
ユーザー koba-e964koba-e964
提出日時 2015-05-26 22:25:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 2,195 ms / 6,000 ms
コード長 1,106 bytes
コンパイル時間 2,260 ms
コンパイル使用メモリ 74,920 KB
実行使用メモリ 58,196 KB
最終ジャッジ日時 2023-09-20 14:18:31
合計ジャッジ時間 32,397 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
55,880 KB
testcase_01 AC 126 ms
55,784 KB
testcase_02 AC 129 ms
55,720 KB
testcase_03 AC 127 ms
55,664 KB
testcase_04 AC 2,181 ms
56,044 KB
testcase_05 AC 2,191 ms
56,184 KB
testcase_06 AC 2,182 ms
56,344 KB
testcase_07 AC 2,180 ms
56,096 KB
testcase_08 AC 2,195 ms
56,484 KB
testcase_09 AC 2,186 ms
56,348 KB
testcase_10 AC 2,184 ms
56,316 KB
testcase_11 AC 2,174 ms
56,084 KB
testcase_12 AC 2,190 ms
56,476 KB
testcase_13 AC 2,171 ms
58,196 KB
testcase_14 AC 2,169 ms
56,184 KB
testcase_15 AC 2,186 ms
56,392 KB
testcase_16 AC 130 ms
55,748 KB
testcase_17 AC 131 ms
55,648 KB
testcase_18 AC 128 ms
55,728 KB
testcase_19 AC 129 ms
55,784 KB
testcase_20 AC 129 ms
53,780 KB
testcase_21 AC 291 ms
56,592 KB
testcase_22 AC 283 ms
56,072 KB
testcase_23 AC 264 ms
55,728 KB
testcase_24 AC 289 ms
55,912 KB
testcase_25 AC 265 ms
56,232 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);
    }
    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