結果

問題 No.372 It's automatic
ユーザー koba-e964
提出日時 2015-05-26 19:49:15
言語 C++11
(gcc 13.3.0)
結果
MLE  
実行時間 -
コード長 1,098 bytes
コンパイル時間 1,096 ms
コンパイル使用メモリ 62,772 KB
実行使用メモリ 781,952 KB
最終ジャッジ日時 2024-07-06 09:02:05
合計ジャッジ時間 25,271 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 MLE * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>

#define REP(i,s,n) for(int i=(int)(s);i<(int)(n);i++)

using namespace std;
typedef __int64_t ll;

int m;

/* Returns the next state from st. */
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;
}
const int M = 101000;
ll dp[2][M] = {};
const ll mod = 1e9 + 7;

int main(void){
  string s;
  cin >> s >> m;
  int len = s.length();
  vector<vector<int> > dp(len + 1, vector<int> (m + 3));
  dp[0][0] = 1;
  /* invariant condition : t == (i - 1) % 2*/
  REP (i, 1, len + 1) {
    REP (j, 0, m + 3) {
      dp[i][j] = dp[i - 1][j];
    }
    REP (j, 0, m + 3) {
      int &d2 = dp[i][next_state(j, s[i - 1])];
      d2 += dp[i - 1][j];
      d2 %= mod;
    }
  }
  // accepting states are {1, 3}
  cout << (dp[len][1] + dp[len][3]) % mod << endl;
}
0