結果

問題 No.372 It's automatic
ユーザー koba-e964koba-e964
提出日時 2015-05-26 17:22:29
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,703 ms / 6,000 ms
コード長 1,308 bytes
コンパイル時間 501 ms
コンパイル使用メモリ 65,156 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-20 13:30:57
合計ジャッジ時間 22,357 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1,692 ms
4,376 KB
testcase_05 AC 1,696 ms
4,380 KB
testcase_06 AC 1,690 ms
4,376 KB
testcase_07 AC 1,695 ms
4,376 KB
testcase_08 AC 1,703 ms
4,376 KB
testcase_09 AC 1,695 ms
4,380 KB
testcase_10 AC 1,691 ms
4,376 KB
testcase_11 AC 1,679 ms
4,380 KB
testcase_12 AC 1,699 ms
4,380 KB
testcase_13 AC 1,681 ms
4,376 KB
testcase_14 AC 1,696 ms
4,380 KB
testcase_15 AC 1,701 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 86 ms
4,376 KB
testcase_22 AC 86 ms
4,380 KB
testcase_23 AC 85 ms
4,380 KB
testcase_24 AC 85 ms
4,380 KB
testcase_25 AC 85 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <string>

#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();
  dp[0][0] = 1;
  int t = 0;
  /* invariant condition : t == (i - 1) % 2*/
  REP (i, 1, len + 1) {
    t = 1 - t; // alternating, t == i % 2
    REP (j, 0, m + 3) {
      dp[t][j] = 0;
    }
    REP (j, 0, m + 3) {
      ll &d1 = dp[t][j];
      d1 += dp[1 - t][j];
      d1 %= mod;
      ll &d2 = dp[t][next_state(j, s[i - 1])];
      d2 += dp[1 - t][j];
      d2 %= mod;
    }
  }
  // accepting states are {1, 3}
  cout << (dp[len % 2][1] + dp[len % 2][3]) % mod << endl;
}
0