結果

問題 No.372 It's automatic
ユーザー koba-e964
提出日時 2015-05-26 22:25:33
言語 Java
(openjdk 23)
結果
AC  
実行時間 2,233 ms / 6,000 ms
コード長 1,106 bytes
コンパイル時間 2,170 ms
コンパイル使用メモリ 77,056 KB
実行使用メモリ 54,608 KB
最終ジャッジ日時 2024-07-06 09:29:51
合計ジャッジ時間 32,488 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 23
権限があれば一括ダウンロードができます

ソースコード

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