prithivMLmods/Coder-Stat huggingface dataset:
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <string.h>
// #include <queue>
using namespace std;
#define ll long long
#define ld long double
#define itvector(it, x) for (auto it : x)
#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
// #define Phuong // <3 love my GF
//  still learning :C; can't even do Div 2 C and D
//  this is so sad
typedef pair<int, int> ii;

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  FOR(i, 0, n) cin >> a[i];
  ll res = 0;
  FOR(i, 0, n - 1) {
    int k;
    cin >> k;
    if (a[i] <= k) {
      res += a[i];
      k -= a[i];
    } else {
      res += k;
      k = 0;
    }
    if (a[i + 1] <= k) {
      res += a[i + 1];
      a[i + 1] = 0;
    } else {
      res += k;
      a[i + 1] -= k;
    }
  }
  cout << res;
  return (0);
}


Fixed submission:
#include <bits/stdc++.h>
#include <string.h>
// #include <queue>
using namespace std;
#define ll long long
#define ld long double
#define itvector(it, x) for (auto it : x)
#define FOR(x, a, b) for (int x = a; x <= b; ++x)
#define FOD(x, a, b) for (int x = a; x >= b; --x)
// #define Phuong // <3 love my GF
//  still learning :C; can't even do Div 2 C and D
//  this is so sad
typedef pair<int, int> ii;

int main() {
#ifdef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
#endif // ONLINE_JUDGE
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int n;
  cin >> n;
  vector<int> a(n + 1);
  FOR(i, 0, n) cin >> a[i];
  ll res = 0;
  FOR(i, 0, n - 1) {
    int k;
    cin >> k;
    if (a[i] <= k) {
      res += a[i];
      k -= a[i];
    } else {
      res += k;
      k = 0;
    }
    if (a[i + 1] <= k) {
      res += a[i + 1];
      a[i + 1] = 0;
    } else {
      res += k;
      a[i + 1] -= k;
    }
  }
  cout << res;
  return (0);
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long n, ans = 0;
  cin >> n;
  vector<long> a(n + 1);
  vector<long> b(n);
  for (int i = 0; i < n + 1; i++)
    cin >> a.at(i);
  for (int i = 0; i < n; i++)
    cin >> b.at(i);
  for (int i = 0; i < n; i++) {
    if (a.at(i) <= b.at(i)) {
      b.at(i) -= a.at(i);
      ans += a.at(i);
      if (a.at(i + 1) <= b.at(i)) {
        ans += a.at(i + 1);
        a.at(i + 1) = 0;
      } else {
        ans += b.at(i);
        a.at(i + i) -= b.at(i);
      };
    } else {
      ans += b.at(i);
    };
  };
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long n, ans = 0;
  cin >> n;
  vector<long> a(n + 1);
  vector<long> b(n);
  for (int i = 0; i < n + 1; i++)
    cin >> a.at(i);
  for (int i = 0; i < n; i++)
    cin >> b.at(i);
  for (int i = 0; i < n; i++) {
    if (a.at(i) <= b.at(i)) {
      b.at(i) -= a.at(i);
      ans += a.at(i);
      if (a.at(i + 1) <= b.at(i)) {
        ans += a.at(i + 1);
        a.at(i + 1) = 0;
      } else {
        ans += b.at(i);
        a.at(i + 1) -= b.at(i);
      };
    } else {
      ans += b.at(i);
    };
  };
  cout << ans << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n + 1), b(n);
  for (int i = 0; i < n + 1; ++i) {
    cin >> a[i];
  }
  for (int j = 0; j < n; ++j) {
    cin >> b[j];
  }
  long long ans = 0;
  vector<long long> a2 = a, b2 = b;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= b[i]) {
      ans += a[i];
      b[i] -= a[i];
      a[i] = 0;
    } else {
      ans += b[i];
      b[i] = 0;
    }

    if (b[i] > 0) {
      if (a[i + 1] <= b[i]) {
        ans += a[i + 1];
        b[i] -= a[i + 1];
        a[i + 1] = 0;
      } else {
        ans += b[i];
        a[i + 1] -= b[i];
        b[i] = 0;
      }
    }
  }

  long long ans2 = 0;
  for (int i = n; i >= 0; --i) {
    if (a2[i + 1] <= b2[i]) {
      ans2 += a2[i + 1];
      b2[i] -= a2[i + 1];
      a2[i + 1] = 0;
    } else {
      ans2 += b2[i];
      b2[i] = 0;
    }

    if (b2[i] > 0) {
      if (a2[i] <= b2[i]) {
        ans2 += a2[i];
        b2[i] -= a2[i];
        a2[i] = 0;
      } else {
        ans2 += b2[i];
        a2[i] -= b2[i];
        b2[i] = 0;
      }
    }
  }
  cout << max(ans, ans2) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<long long> a(n + 1), b(n);
  for (int i = 0; i < n + 1; ++i) {
    cin >> a[i];
  }
  for (int j = 0; j < n; ++j) {
    cin >> b[j];
  }
  long long ans = 0;
  vector<long long> a2 = a, b2 = b;
  for (int i = 0; i < n; ++i) {
    if (a[i] <= b[i]) {
      ans += a[i];
      b[i] -= a[i];
      a[i] = 0;
    } else {
      ans += b[i];
      b[i] = 0;
    }

    if (b[i] > 0) {
      if (a[i + 1] <= b[i]) {
        ans += a[i + 1];
        b[i] -= a[i + 1];
        a[i + 1] = 0;
      } else {
        ans += b[i];
        a[i + 1] -= b[i];
        b[i] = 0;
      }
    }
  }

  long long ans2 = 0;
  for (int i = n - 1; i >= 0; --i) {
    if (a2[i + 1] <= b2[i]) {
      ans2 += a2[i + 1];
      b2[i] -= a2[i + 1];
      a2[i + 1] = 0;
    } else {
      ans2 += b2[i];
      b2[i] = 0;
    }

    if (b2[i] > 0) {
      if (a2[i] <= b2[i]) {
        ans2 += a2[i];
        b2[i] -= a2[i];
        a2[i] = 0;
      } else {
        ans2 += b2[i];
        a2[i] -= b2[i];
        b2[i] = 0;
      }
    }
  }
  cout << max(ans, ans2) << endl;
  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int N;
  int A[10001], B[10000];
  int sum = 0;

  cin >> N;
  for (int i = 0; i <= N; ++i) {
    cin >> A[i];
    sum += A[i];
  }
  for (int i = 0; i < N; ++i)
    cin >> B[i];

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= 1; ++j) {
      if (B[i] > A[i + j]) {
        B[i] -= A[i + j];
        A[i + j] = 0;
      } else {
        A[i + j] -= B[i];
        B[i] = 0;
      }
    }
  }

  for (int i = 0; i <= N; ++i) {
    sum -= A[i];
  }
  cout << sum << endl;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int N;
  long long A[100001], B[100000];
  long long sum = 0;

  cin >> N;
  for (int i = 0; i <= N; ++i) {
    cin >> A[i];
    sum += A[i];
  }
  for (int i = 0; i < N; ++i)
    cin >> B[i];

  for (int i = 0; i < N; ++i) {
    for (int j = 0; j <= 1; ++j) {
      if (B[i] > A[i + j]) {
        B[i] -= A[i + j];
        A[i + j] = 0;
      } else {
        A[i + j] -= B[i];
        B[i] = 0;
      }
    }
  }

  for (int i = 0; i <= N; ++i) {
    sum -= A[i];
  }
  cout << sum << endl;
}
Problem p02959 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

const long long INF = 1e7 + 1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()

using ll = long long int;
using ull = unsigned long long int;
using vi = vector<ll>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

/*--------------------template--------------------*/

int main() {
  cin >> n;
  ull A[n + 1], B[n];
  REP(i, n + 1) {
    cin >> A[i];
    cnt += A[i];
  }
  REP(i, n) { cin >> B[i]; }
  REP(i, n) {
    if (A[i] >= B[i]) {
      A[i] -= B[i];
    } else if (A[i] < B[i]) {
      B[i] -= A[i];
      A[i] = 0;
      while (A[i + 1] > 0 && B[i] > 0) {
        A[i + 1] -= 1;
        B[i] -= 1;
      }
    }
  }
  REP(i, n + 1) { sum += A[i]; }
  cout << cnt - sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const long long INF = 1e7 + 1;
const long long MOD = 1000000007;
const long double PI = 3.1415926;

#define FOR(i, r, n) for (ll i = (ll)(r); i < (ll)(n); ++i)
#define RFOR(i, r, n) for (ll i = (ll)(n - 1); i >= r; --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()

using ll = long long int;
using ull = unsigned long long int;
using vi = vector<ll>;
using vp = vector<pair<ll, ll>>;
using vs = vector<string>;
using vc = vector<char>;
using lst = list<ll>;

ll n, m, k, ans = 0, sum = 0, cnt = 0;
string s;
char c;

/*--------------------template--------------------*/

int main() {
  cin >> n;
  ull A[n + 1], B[n];
  REP(i, n + 1) {
    cin >> A[i];
    cnt += A[i];
  }
  REP(i, n) { cin >> B[i]; }
  REP(i, n) {
    if (A[i] >= B[i]) {
      A[i] -= B[i];
    } else if (A[i] < B[i]) {
      B[i] -= A[i];
      A[i] = 0;
      if (A[i + 1] > B[i]) {
        A[i + 1] -= B[i];
        B[i] = 0;
      } else {
        B[i] -= A[i + 1];
        A[i + 1] = 0;
      }
    }
  }
  REP(i, n + 1) { sum += A[i]; }
  cout << cnt - sum << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
// This code was made by Chinese_zjc_
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#define int long long
#define INF 0x3fffffffffffffff
using namespace std;
inline int rd(int &ddd) {
  char tmp = getchar();
  int xxx = 1;
  ddd = 0;
  while (('0' > tmp || tmp > '9') && tmp != '-' && tmp != EOF) {
    tmp = getchar();
  }
  if (tmp == EOF) {
    return EOF;
  }
  if (tmp == '-') {
    xxx = -1;
    tmp = getchar();
  }
  while ('0' <= tmp && tmp <= '9') {
    ddd = (ddd << 3) + (ddd << 1) + (tmp ^ 48);
    tmp = getchar();
  }
  return ddd *= xxx;
}
inline char rd(char &ddd) {
  char tmp = getchar();
  while (tmp == ' ' || tmp == '\n') {
    tmp = getchar();
  }
  if (tmp == EOF) {
    return EOF;
  }
  return ddd = tmp;
}
inline int max(const int &A, const int &B) { return A < B ? B : A; }
inline int min(const int &A, const int &B) { return A < B ? A : B; }
int n, a[100001], x, ans;
signed main() {
  rd(n);
  for (int i = 1; i <= n + 1; ++i) {
    rd(a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    rd(x);
    ans += min(a[i], x);
    x -= min(a[i], x);
    ans += min(a[i + 1], x);
    a[i + 1] -= min(a[i + 1], x);
  }
  printf("%lld", ans);
  return 0;
}


Fixed submission:
// This code was made by Chinese_zjc_
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#define int long long
#define INF 0x3fffffffffffffff
using namespace std;
inline int rd(int &ddd) {
  char tmp = getchar();
  int xxx = 1;
  ddd = 0;
  while (('0' > tmp || tmp > '9') && tmp != '-' && tmp != EOF) {
    tmp = getchar();
  }
  if (tmp == EOF) {
    return EOF;
  }
  if (tmp == '-') {
    xxx = -1;
    tmp = getchar();
  }
  while ('0' <= tmp && tmp <= '9') {
    ddd = (ddd << 3) + (ddd << 1) + (tmp ^ 48);
    tmp = getchar();
  }
  return ddd *= xxx;
}
inline char rd(char &ddd) {
  char tmp = getchar();
  while (tmp == ' ' || tmp == '\n') {
    tmp = getchar();
  }
  if (tmp == EOF) {
    return EOF;
  }
  return ddd = tmp;
}
inline int max(const int &A, const int &B) { return A < B ? B : A; }
inline int min(const int &A, const int &B) { return A < B ? A : B; }
int n, a[100005], x, ans;
signed main() {
  rd(n);
  for (int i = 1; i <= n + 1; ++i) {
    rd(a[i]);
  }
  for (int i = 1; i <= n; ++i) {
    rd(x);
    ans += min(a[i], x);
    x -= min(a[i], x);
    ans += min(a[i + 1], x);
    a[i + 1] -= min(a[i + 1], x);
  }
  printf("%lld", ans);
  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)

int main() {
  int n;
  cin >> n;
  vector<long> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  vector<long> b(n);
  rep(i, n + 1) cin >> b[i];
  b[n] = 0;

  long c;
  c = 0;
  rep(i, n + 1) {
    long l = min(a[i], b[i]);
    c += l;
    long r = min(a[i + 1], b[i] - l);
    c += r;
    a[i + 1] -= r;
    b[i] -= r;
  }
  cout << c << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)

int main() {
  int n;
  cin >> n;
  vector<long> a(n + 1);
  rep(i, n + 1) cin >> a[i];
  vector<long> b(n + 1);
  rep(i, n) cin >> b[i];
  b[n] = 0;

  long c;
  c = 0;
  rep(i, n + 1) {
    long l = min(a[i], b[i]);
    c += l;
    long r = min(a[i + 1], b[i] - l);
    c += r;
    a[i + 1] -= r;
    b[i] -= r;
  }
  cout << c << endl;
  return 0;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  long b, c, t;
  cin >> n;
  vector<long> a(n);

  for (int i = 0; i < n + 1; ++i)
    cin >> a[i];
  c = 0;
  for (int i = 0; i < n; ++i) {
    cin >> b;
    t = min(b, a[i] + a[i + 1]);
    c += t;
    a[i + 1] -= t - min(b, a[i]);
  }
  cout << c;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n;
  long b, c, t;
  cin >> n;
  vector<long> a(n + 1);

  for (int i = 0; i < n + 1; ++i)
    cin >> a[i];
  c = 0;
  for (int i = 0; i < n; ++i) {
    cin >> b;
    t = min(b, a[i] + a[i + 1]);
    c += t;
    a[i + 1] -= t - min(b, a[i]);
  }
  cout << c;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;

ll GCD(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return GCD(y, x % y);
}

ll LCM(ll x, ll y) { return x * y / GCD(x, y); }

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1), b(n);
  rep(i, n + 1) cin >> a[i];
  rep(i, n) cin >> b[i];

  ll res = 0;
  rep(i, n) {
    ll tmp = min(a[i], b[i]);
    a[i] -= tmp;
    b[i] -= tmp;
    res += tmp;
    tmp = min(b[i], a[i + 1]);
    a[i - 1] -= tmp;
    res += tmp;
  }
  cout << res << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;

ll GCD(ll x, ll y) {
  if (y == 0)
    return x;
  else
    return GCD(y, x % y);
}

ll LCM(ll x, ll y) { return x * y / GCD(x, y); }

int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1), b(n);
  rep(i, n + 1) cin >> a[i];
  rep(i, n) cin >> b[i];

  ll res = 0;
  rep(i, n) {
    ll tmp = min(a[i], b[i]);
    a[i] -= tmp;
    b[i] -= tmp;
    res += tmp;
    tmp = min(b[i], a[i + 1]);
    a[i + 1] -= tmp;
    res += tmp;
  }
  cout << res << endl;
}
Problem p02959 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
  int N;
  cin >> N;
  vector<ll> A(N + 1), B(N);
  for (int i = 0; i < N + 1; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }

  ll ans = 0;
  for (int i = 0; i < N + 1; i++) {
    ll sub = min(A[i], B[i]);
    B[i] -= sub;
    ans += sub;

    sub = min(B[i], A[i + 1]);
    A[i + 1] -= sub;
    ans += sub;
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define ll long long

int main() {
  int N;
  cin >> N;
  vector<int> A(N + 1), B(N);
  for (int i = 0; i < N + 1; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> B[i];
  }

  ll ans = 0;
  for (int i = 0; i < N + 1; i++) {
    ll sub = min(A[i], B[i]);
    B[i] -= sub;
    ans += sub;

    sub = min(B[i], A[i + 1]);
    A[i + 1] -= sub;
    ans += sub;
  }

  cout << ans << endl;

  return 0;
}
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
// C

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  int N = s.size();
  ll dp[10001][13] = {0};
  if (s[0] == '?') {
    for (int i(0); i <= 9; i++) {
      dp[1][i] = 1;
    }
  } else {
    dp[1][s[0] - '0'] = 1;
  }
  for (int i(2); i <= N; i++) {
    if (s[i - 1] == '?') {
      for (int j(0); j < 13; j++) {
        for (int k(0); k < 10; k++) {
          int next_amari = (j * 10 + k) % 13;
          dp[i][next_amari] += dp[i - 1][j];
          dp[i][next_amari] %= mod;
        }
      }
    } else {
      for (int j(0); j < 13; j++) {
        int next_amari = (j * 10 + (s[i - 1] - '0')) % 13;
        dp[i][next_amari] += dp[i - 1][j];
        dp[i][next_amari] %= mod;
      }
    }
  }
  cout << dp[N][5] << endl;
  return 0;
}


Fixed submission:
// C

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod = 1e9 + 7;

int main() {
  string s;
  cin >> s;
  int N = s.size();
  ll dp[100001][13] = {0};
  if (s[0] == '?') {
    for (int i(0); i <= 9; i++) {
      dp[1][i] = 1;
    }
  } else {
    dp[1][s[0] - '0'] = 1;
  }
  for (int i(2); i <= N; i++) {
    if (s[i - 1] == '?') {
      for (int j(0); j < 13; j++) {
        for (int k(0); k < 10; k++) {
          int next_amari = (j * 10 + k) % 13;
          dp[i][next_amari] += dp[i - 1][j];
          dp[i][next_amari] %= mod;
        }
      }
    } else {
      for (int j(0); j < 13; j++) {
        int next_amari = (j * 10 + (s[i - 1] - '0')) % 13;
        dp[i][next_amari] += dp[i - 1][j];
        dp[i][next_amari] %= mod;
      }
    }
  }
  cout << dp[N][5] << endl;
  return 0;
}
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define ll long long
#define vi vector<int>
#define pi pair<int, int>
#define mp make_pair
#define endl '\n'
#define _ << ' '
#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min(min(a, d), min(b, c))

using namespace std;
const ll mod = (ll)1e9 + 7;
const ll INF = (ll)1e9 + 1;

ll dp[100001][13];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("B.txt", "r", stdin);
#endif
  string s;
  cin >> s;
  int n = s.length();
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 13; j++) {
      if (s[i] == '?') {
        for (int d = 0; d < 10; d++)
          dp[i + 1][(j * 10 + d) % 13] =
              (dp[i][j] + dp[i + 1][(j * 10 + d) % 13]) % mod;
      } else {
        dp[i + 1][(j * 10 + s[i] - '0') % 13] =
            (dp[i][j] + dp[i + 1][(j * 10 + s[i] - '0') % 13]) % mod;
      }
    }
  }
  cout << dp[n][5];
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define all(a) a.begin(), a.end()
#define F first
#define S second
#define pb push_back
#define ll long long
#define vi vector<int>
#define pi pair<int, int>
#define mp make_pair
#define endl '\n'
#define _ << ' '
#define min3(a, b, c) min(a, min(b, c))
#define min4(a, b, c, d) min(min(a, d), min(b, c))

using namespace std;
const ll mod = (ll)1e9 + 7;
const ll INF = (ll)1e9 + 1;

ll dp[100001][13];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  // freopen("B.txt", "r", stdin);
#endif
  string s;
  cin >> s;
  int n = s.length();
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 13; j++) {
      if (s[i] == '?') {
        for (int d = 0; d < 10; d++)
          dp[i + 1][(j * 10 + d) % 13] =
              (dp[i][j] + dp[i + 1][(j * 10 + d) % 13]) % mod;
      } else {
        dp[i + 1][(j * 10 + s[i] - '0') % 13] =
            (dp[i][j] + dp[i + 1][(j * 10 + s[i] - '0') % 13]) % mod;
      }
    }
  }
  cout << dp[n][5];
}
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
typedef long long ll;

ll n;
char M[101010][13] = {};
ll dp[1010][1010] = {};
ll cnt = LLONG_MAX;
ll B = 1000000007;

int main() {
  string s;
  cin >> s;

  dp[0][0] = 1;
  rep(i, s.size()) {
    if (s[i] == '?') {
      rep(j, 10) {
        rep(k, 13) {
          int a = (k * 10 + j) % 13;

          dp[i + 1][a] += dp[i][k];
          dp[i + 1][a] %= B;
        }
      }
    } else {
      s[i] -= '0';
      rep(k, 13) {

        int a = (k * 10 + s[i]) % 13;

        dp[i + 1][a] += dp[i][k];
        dp[i + 1][a] %= B;
      }
    }
  }

  cout << dp[s.size()][5] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
typedef long long ll;

ll n;
char M[101010][13] = {};
ll dp[101010][1010] = {};
ll cnt = LLONG_MAX;
ll B = 1000000007;

int main() {
  string s;
  cin >> s;

  dp[0][0] = 1;
  rep(i, s.size()) {
    if (s[i] == '?') {
      rep(j, 10) {
        rep(k, 13) {
          int a = (k * 10 + j) % 13;

          dp[i + 1][a] += dp[i][k];
          dp[i + 1][a] %= B;
        }
      }
    } else {
      s[i] -= '0';
      rep(k, 13) {

        int a = (k * 10 + s[i]) % 13;

        dp[i + 1][a] += dp[i][k];
        dp[i + 1][a] %= B;
      }
    }
  }

  cout << dp[s.size()][5] << endl;
}
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

unsigned dp[10000][13] = {0};

int main() {
  string S;
  cin >> S;

  if (S[0] == '?')
    for (int ii = 0; ii < 10; ++ii) {
      dp[0][ii] = 1;
    }
  else
    dp[0][S[0] - '0'] = 1;

  for (int ii = 1; ii < S.size(); ++ii) {
    for (int tens = 0; tens < 13; ++tens) {
      if (S[ii] == '?') {
        for (int ones = 0; ones < 10; ++ones) {
          int residue = (tens * 10 + ones) % 13;
          dp[ii][residue] += dp[ii - 1][tens];
          dp[ii][residue] %= 1000000007;
        }
      } else {
        int ones = S[ii] - '0';
        int residue = (tens * 10 + ones) % 13;
        dp[ii][residue] += dp[ii - 1][tens];
        dp[ii][residue] %= 1000000007;
      }
    }
  }

  cout << dp[S.size() - 1][5] << endl;

  return 0;
}


Fixed submission:
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <stdint.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

unsigned dp[100000][13] = {0};

int main() {
  string S;
  cin >> S;

  if (S[0] == '?')
    for (int ii = 0; ii < 10; ++ii) {
      dp[0][ii] = 1;
    }
  else
    dp[0][S[0] - '0'] = 1;

  for (int ii = 1; ii < S.size(); ++ii) {
    for (int tens = 0; tens < 13; ++tens) {
      if (S[ii] == '?') {
        for (int ones = 0; ones < 10; ++ones) {
          int residue = (tens * 10 + ones) % 13;
          dp[ii][residue] += dp[ii - 1][tens];
          dp[ii][residue] %= 1000000007;
        }
      } else {
        int ones = S[ii] - '0';
        int residue = (tens * 10 + ones) % 13;
        dp[ii][residue] += dp[ii - 1][tens];
        dp[ii][residue] %= 1000000007;
      }
    }
  }

  cout << dp[S.size() - 1][5] << endl;

  return 0;
}
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
// 解説見た
#include <bits/stdc++.h>
typedef long long lint;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
const int MOD = 1000000007;
int main() {
  string s;
  cin >> s;
  int dp[10001][13] = {0};
  dp[0][0] = 1;

  rrep(i, s.size() + 1) {
    if (s[i - 1] == '?') {
      rep(num, 10) {
        rep(j, 13) {
          dp[i][(j * 10 + num) % 13] += dp[i - 1][j];
          dp[i][(j * 10 + num) % 13] %= MOD;
        }
      }
    } else {
      int num = s[i - 1] - '0';
      rep(j, 13) {
        dp[i][(j * 10 + num) % 13] += dp[i - 1][j];
        dp[i][(j * 10 + num) % 13] %= MOD;
      }
    }
  }
  cout << dp[s.size()][5] << endl;
}

Fixed submission:
// 解説見た
#include <bits/stdc++.h>
typedef long long lint;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; ++i)
#define rrep(i, n) for (int i = 1, i##_len = (n); i < i##_len; ++i)
const int MOD = 1000000007;
int main() {
  string s;
  cin >> s;
  int dp[100001][13] = {0};
  dp[0][0] = 1;

  rrep(i, s.size() + 1) {
    if (s[i - 1] == '?') {
      rep(num, 10) {
        rep(j, 13) {
          dp[i][(j * 10 + num) % 13] += dp[i - 1][j];
          dp[i][(j * 10 + num) % 13] %= MOD;
        }
      }
    } else {
      int num = s[i - 1] - '0';
      rep(j, 13) {
        dp[i][(j * 10 + num) % 13] += dp[i - 1][j];
        dp[i][(j * 10 + num) % 13] %= MOD;
      }
    }
  }
  cout << dp[s.size()][5] << endl;
}
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;

int main() {
  char temp[10000];
  scanf("%s", temp);
  string s = temp;
  vector<vector<ll>> dp(s.length(), vector<ll>(13));
  if (s[0] == '?') {
    for (int i = 0; i < 10; ++i)
      ++dp[0][i];
  } else {
    ++dp[0][s[0] - '0'];
  }
  for (int i = 1; i < s.length(); ++i) {
    if (s[i] == '?') {
      for (int j = 0; j < 13; ++j) {
        for (int k = 0; k < 10; ++k) {
          dp[i][(10 * j + k) % 13] += dp[i - 1][j];
          dp[i][(10 * j + k) % 13] %= MOD;
        }
      }
    } else {
      for (int j = 0; j < 13; ++j) {
        dp[i][(10 * j + s[i] - '0') % 13] = dp[i - 1][j];
        dp[i][(10 * j + s[i] - '0') % 13] %= MOD;
      }
    }
  }
  printf("%lld", dp[s.length() - 1][5]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back
#define fi first
#define se second

const ll MOD = 1000000007;

int main() {
  char temp[100000];
  scanf("%s", temp);
  string s = temp;
  vector<vector<ll>> dp(s.length(), vector<ll>(13));
  if (s[0] == '?') {
    for (int i = 0; i < 10; ++i)
      ++dp[0][i];
  } else {
    ++dp[0][s[0] - '0'];
  }
  for (int i = 1; i < s.length(); ++i) {
    if (s[i] == '?') {
      for (int j = 0; j < 13; ++j) {
        for (int k = 0; k < 10; ++k) {
          dp[i][(10 * j + k) % 13] += dp[i - 1][j];
          dp[i][(10 * j + k) % 13] %= MOD;
        }
      }
    } else {
      for (int j = 0; j < 13; ++j) {
        dp[i][(10 * j + s[i] - '0') % 13] = dp[i - 1][j];
        dp[i][(10 * j + s[i] - '0') % 13] %= MOD;
      }
    }
  }
  printf("%lld", dp[s.length() - 1][5]);
  return 0;
}
Problem p02960 (Python)

Buggy submission (status: Runtime Error):
M = 10**9 + 7
a = [1] + [0] * 12
p = 1
for c in reversed(input()):
    r = (0, p * 10) if c == "?" else (int(c), int(c) + 1)
    a = [sum(a[(i - j) % 13] for j in range(*r, p)) % M for i in range(13)]
    p = p * 10 % 13
print(a[5])


Fixed submission:
M = 10**9 + 7
a = [1] + [0] * 12
p = 1
for c in reversed(input()):
    x, y = (0, p * 10) if c == "?" else (p * int(c), p * int(c) + 1)
    a = [sum(a[(i - j) % 13] for j in range(x, y, p)) % M for i in range(13)]
    p = p * 10 % 13
print(a[5])
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {

  string s;
  cin >> s;

  const int n = 13;
  vector<vector<ll>> dp(100001, vector<ll>(n));

  ll mod = 1000000007;
  int mul = 1;

  dp[s.length()][0] = 1;
  for (int i = s.length() - 1; i >= 0; i--) {
    if (s[i] == '?') {
      for (int k = 0; k < 10; k++) {
        for (int j = 0; j < n; j++) {
          dp[i][(k * mul + j) % n] += dp[i + 1][j];
          dp[i][(k * mul + j) % n] %= mod;
        }
      }
    } else {
      int k = (int)(s[i] - '0');
      for (int j = 0; j < n; j++) {
        dp[i][(k * mul + j) % n] += dp[i + 1][j];
        dp[i][(k * mul + j) % n] %= mod;
      }
    }
    mul *= 10;
  }

  cout << dp[0][5] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {

  string s;
  cin >> s;

  const int n = 13;
  vector<vector<ll>> dp(100001, vector<ll>(n));

  ll mod = 1000000007;
  int mul = 1;

  dp[s.length()][0] = 1;
  for (int i = s.length() - 1; i >= 0; i--) {
    if (s[i] == '?') {
      for (int k = 0; k < 10; k++) {
        for (int j = 0; j < n; j++) {
          dp[i][(k * mul + j) % n] += dp[i + 1][j];
          dp[i][(k * mul + j) % n] %= mod;
        }
      }
    } else {
      int k = (int)(s[i] - '0');
      for (int j = 0; j < n; j++) {
        dp[i][(k * mul + j) % n] += dp[i + 1][j];
        dp[i][(k * mul + j) % n] %= mod;
      }
    }
    mul *= 10;
    mul %= n;
  }

  cout << dp[0][5] << endl;
  return 0;
}
Problem p02960 (Python)

Buggy submission (status: Runtime Error):
M = 10**9 + 7
a = [1] + [0] * 12
p = 1
for c in reversed(input()):
    r = [x % 13 for x in range(0, p * 10, p)] if c == "?" else (p * int(c),)
    a = [sum(a[(i - j, i - j + 13)[i < j]] for j in r) % M for i in range(13)]
    p = p * 10 % 13
print(a[5])


Fixed submission:
M = 10**9 + 7
a = [1] + [0] * 12
p = 1
for c in reversed(input()):
    r = [x % 13 for x in range(0, p * 10, p)] if c == "?" else (p * int(c) % 13,)
    a = [sum(a[(i - j, i - j + 13)[i < j]] for j in r) % M for i in range(13)]
    p = p * 10 % 13
print(a[5])
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <random>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;
typedef long long int ll;

ll MOD = 1000000007;

int main() {

  string S;
  cin >> S;

  vector<vector<ll>> dp(S.length() + 1, vector<ll>(13, 0));

  dp[0][0] = 1;

  for (ll i = 0; i <= S.length(); i++) {

    // 0~9までを入れたとき前からのMODがどう変化するか
    for (ll j = 0; j < 10; j++) {
      // ？ではなく数字が書かれているときは数字分の遷移しか考えない
      if (S[i] != '?' && (S[i] - '0') != j) {
        continue;
      }
      for (ll h = 0; h < 13; h++) {
        dp[i + 1][(h * 10 + j) % 13] += dp[i][h];
      }
    }

    // 答えが超えないように割っておく
    for (ll j = 0; j < 13; j++) {
      dp[i + 1][j] %= MOD;
    }
  }

  cout << dp[S.length()][5] << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <random>
#include <sstream>
#include <stack>
#include <string>
#include <typeinfo>
#include <vector>

using namespace std;
typedef long long int ll;

ll MOD = 1000000007;

int main() {

  string S;
  cin >> S;

  vector<vector<ll>> dp(S.length() + 1, vector<ll>(13, 0));

  dp[0][0] = 1;

  for (ll i = 0; i < S.length(); i++) {

    // 0~9までを入れたとき前からのMODがどう変化するか
    for (ll j = 0; j < 10; j++) {
      // ？ではなく数字が書かれているときは数字分の遷移しか考えない
      if (S[i] != '?' && (S[i] - '0') != j) {
        continue;
      }
      for (ll h = 0; h < 13; h++) {
        dp[i + 1][(h * 10 + j) % 13] += dp[i][h];
      }
    }

    // 答えが超えないように割っておく
    for (ll j = 0; j < 13; j++) {
      dp[i + 1][j] %= MOD;
    }
  }

  cout << dp[S.length()][5] << endl;
}
Problem p02960 (Python)

Buggy submission (status: Time Limit Exceeded):
s = input()

mod = 1000000007
n = 13

dp = [0] * 13
dp[0] = 1

mul = 1
for i in reversed(range(len(s))):
    nextDp = [0] * 13
    if s[i] == "?":
        for k in range(10):
            for j in range(n):
                nextDp[(k * mul + j) % n] += dp[j]
        for j in range(n):
            nextDp[j] %= mod
    else:
        k = int(s[i])
        for j in range(n):
            nextDp[(k * mul + j) % n] = dp[j]

    mul *= 10
    mul %= n
    dp = nextDp
print(dp[5] % mod)


Fixed submission:
s = input()

mod = 1000000007
n = 13

dp = [0] * 13
dp[0] = 1

mul = 1
for i in reversed(range(len(s))):
    nextDp = [0] * 13
    if s[i] == "?":
        for k in range(10):
            for j in range(n):
                nextDp[(k * mul + j) % n] += dp[j]
        for j in range(n):
            nextDp[j] %= mod
    else:
        k = int(s[i])
        for j in range(n):
            nextDp[(k * mul + j) % n] = dp[j]

    mul *= 10
    mul %= n
    dp = nextDp
print(dp[5])
Problem p02960 (Python)

Buggy submission (status: Time Limit Exceeded):
import numpy as np


def solve(s):
    MOD = 10**9 + 7
    dp = np.zeros(13, dtype=np.int64)
    dp[0] = 1
    idx = np.zeros(13, dtype=np.int8)
    for i in range(13):
        idx[i * 10 % 13] = i
    window = np.ones(10, dtype=np.int8)
    for c in s:
        if c == "?":
            dp = np.convolve(dp[idx], window)
            dp[:9] += dp[13:]
            dp = dp[:13] % MOD
        else:
            dp = np.roll(dp[idx], int(c))
    return dp[5]


s = input()
print(solve(s))


Fixed submission:
import numpy as np


def solve(s):
    MOD = 10**9 + 7
    dp = np.zeros(13, dtype=np.int64)
    dp[0] = 1
    idx = np.zeros(13, dtype=np.int8)
    for i in range(13):
        idx[i * 10 % 13] = i
    window = np.ones(10, dtype=np.int8)
    for c in s:
        if c == "?":
            tdp = dp[idx]
            ndp = np.concatenate([tdp[4:], tdp])
            dp = np.convolve(ndp, window, mode="valid") % MOD
        else:
            dp = np.roll(dp[idx], int(c))
    return dp[5]


s = input()
print(solve(s))
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll dp[10001][13];
const ll MOD = (ll)1e9 + 7;

void solve() {
  string s;
  cin >> s;

  dp[0][0] = 1;
  int n = s.size();
  s.push_back('0');
  reverse(s.begin(), s.end());
  ll mul = 1;
  for (int d = 1; d <= n; d++) {
    char c = s[d];
    if (c == '?') {
      rep(i, 10) {
        rep(j, 13) {
          dp[d][(i * mul + j) % 13] += dp[d - 1][j];
          dp[d][(i * mul + j) % 13] %= MOD;
        }
      }
    } else {
      rep(j, 13) {
        dp[d][((c - '0') * mul + j) % 13] += dp[d - 1][j];
        dp[d][((c - '0') * mul + j) % 13] %= MOD;
      }
    }
    mul *= 10;
    mul %= 13;
  }

  cout << dp[n][5] << endl;
}

int main() {
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll dp[100005][13];
const ll MOD = (ll)1e9 + 7;

void solve() {
  string s;
  cin >> s;

  dp[0][0] = 1;
  int n = s.size();
  s.push_back('0');
  reverse(s.begin(), s.end());
  ll mul = 1;
  for (int d = 1; d <= n; d++) {
    char c = s[d];
    if (c == '?') {
      rep(i, 10) {
        rep(j, 13) {
          dp[d][(i * mul + j) % 13] += dp[d - 1][j];
          dp[d][(i * mul + j) % 13] %= MOD;
        }
      }
    } else {
      rep(j, 13) {
        dp[d][((c - '0') * mul + j) % 13] += dp[d - 1][j];
        dp[d][((c - '0') * mul + j) % 13] %= MOD;
      }
    }
    mul *= 10;
    mul %= 13;
  }

  cout << dp[n][5] << endl;
}

int main() {
  solve();
  return 0;
}
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
using ll = long long;
using ull = unsigned long long;
constexpr int MOD = 1000000007;

int mod(ll x) { return x > 0 ? x % MOD : (x + MOD) % MOD; }

int modm(ll x, ll m) { return x * m % MOD; }

int modpow(ll x, ll p) {
  int ret = 1;
  while (p > 0) {
    if (p & 1)
      ret = modm(ret, x);
    x = modm(x, x);
    p >>= 1;
  }
  return ret;
}

// x * modinv(d)
int modd(ll x, ll d) { return modm(x, modpow(d, MOD - 2)); }

int main(void) {
  IOS constexpr int N = 1e4;
  constexpr int M = 13;

  string s;
  cin >> s;
  int l = s.size();
  vector<vector<int>> dp(l, vector<int>(M, 0));
  dp[0][0] = 1;

  REP(i, l) {
    REP(j, M) {
      REP(k, 10) {
        if (s[i] - '0' == k || s[i] == '?') {
          dp[i + 1][(j * 10 + k) % M] =
              mod(0LL + dp[i + 1][(j * 10 + k) % M] + dp[i][j]);
        }
      }
    }
  }

  cout << dp[l][5] << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);
#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(n) (n).begin(), (n).end()
#define RALL(n) (n).rbegin(), (n).rend()
using ll = long long;
using ull = unsigned long long;
constexpr int MOD = 1000000007;

int mod(ll x) { return x > 0 ? x % MOD : (x + MOD) % MOD; }

int modm(ll x, ll m) { return x * m % MOD; }

int modpow(ll x, ll p) {
  int ret = 1;
  while (p > 0) {
    if (p & 1)
      ret = modm(ret, x);
    x = modm(x, x);
    p >>= 1;
  }
  return ret;
}

// x * modinv(d)
int modd(ll x, ll d) { return modm(x, modpow(d, MOD - 2)); }

int main(void) {
  IOS constexpr int N = 1e4;
  constexpr int M = 13;

  string s;
  cin >> s;
  int l = s.size();
  vector<vector<int>> dp(l + 1, vector<int>(M, 0));
  dp[0][0] = 1;

  REP(i, l) {
    REP(j, M) {
      REP(k, 10) {
        if (s[i] - '0' == k || s[i] == '?') {
          dp[i + 1][(j * 10 + k) % M] =
              mod(0LL + dp[i + 1][(j * 10 + k) % M] + dp[i][j]);
        }
      }
    }
  }

  cout << dp[l][5] << endl;

  return 0;
}
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

ll mod = 1000000007;
string S;
vector<vector<ll>> dp(10005, vector<ll>(13, 0));

int main() {
  cin >> S;
  int N = (int)S.size();

  for (int i = 0; i < N; i++) {
    char c = (char)S[i];
    int t = c - '0';
    if (c == '?')
      t = -1;
    dp[0][0] = 1;
    for (int j = 0; j < 10; j++) {
      if (t != -1 && t != j)
        continue;
      for (int k = 0; k < 13; k++) {
        dp[i + 1][(k * 10 + j) % 13] += dp[i][k];
      }
    }
    for (int j = 0; j < 13; j++) {
      dp[i + 1][j] %= mod;
    }
  }
  cout << dp[N][5] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;

ll mod = 1000000007;
string S;
vector<vector<ll>> dp(100005, vector<ll>(13, 0));

int main() {
  cin >> S;
  int N = (int)S.size();

  for (int i = 0; i < N; i++) {
    char c = (char)S[i];
    int t = c - '0';
    if (c == '?')
      t = -1;
    dp[0][0] = 1;
    for (int j = 0; j < 10; j++) {
      if (t != -1 && t != j)
        continue;
      for (int k = 0; k < 13; k++) {
        dp[i + 1][(k * 10 + j) % 13] += dp[i][k];
      }
    }
    for (int j = 0; j < 13; j++) {
      dp[i + 1][j] %= mod;
    }
  }
  cout << dp[N][5] << endl;
  return 0;
}
Problem p02960 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**7)

# Override `input` function because `stdin.readline()` is 10x faster than built-in `input()`
input = sys.stdin.readline


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h(typ) if m > 1 else typ(input()) for _ in range(n)]


def main():
    (S,) = read_h(typ=str)
    n = len(S)

    MOD = 10**9 + 7

    dp = [[0] * 13 for _ in range(10**5)]
    dp[0][0] = 1
    #  print('initial dp:', dp)

    for i, s in enumerate(S):
        c = -1 if s == "?" else ord(s) - ord("0")
        #  print('c:', c)

        for j in range(10):
            if c != -1 and c != j:
                continue

            for k in range(13):
                dp[i + 1][(k * 10 + j) % 13] += dp[i][k]

            #  print('updated dp:', dp[:])

        for j in range(13):
            dp[i + 1][j] %= MOD

        #  print('final dp:', dp[:])

    res = dp[n][5]
    print(res)


if __name__ == "__main__":
    main()


Fixed submission:
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(10**7)

# Override `input` function because `stdin.readline()` is 10x faster than built-in `input()`
input = sys.stdin.readline


def read_h(typ=int):
    return list(map(typ, input().split()))


def read_v(n, m=1, typ=int):
    return [read_h(typ) if m > 1 else typ(input()) for _ in range(n)]


def main():
    (S,) = read_h(typ=str)
    n = len(S)

    MOD = 10**9 + 7

    dp = [[0] * 13 for _ in range(10**5 + 2)]
    dp[0][0] = 1
    #  print('initial dp:', dp)

    for i, s in enumerate(S):
        c = -1 if s == "?" else ord(s) - ord("0")
        #  print('c:', c)

        for j in range(10):
            if c != -1 and c != j:
                continue

            for k in range(13):
                dp[i + 1][(k * 10 + j) % 13] += dp[i][k]

            #  print('updated dp:', dp[:])

        for j in range(13):
            dp[i + 1][j] %= MOD

        #  print('final dp:', dp[:])

    res = dp[n][5]
    print(res)


if __name__ == "__main__":
    main()
Problem p02960 (Python)

Buggy submission (status: Runtime Error):
import itertools

S = input()[::-1]
mod = 10**9 + 7
A = [0] * 13
N = 0
for i, v in enumerate(S):
    p = pow(10, i, 13)
    if v == "?":
        A[p] += 1
    else:
        N += int(v) * p
        N %= 13
dp = [0] * 13
dp[N] = 1
for i, v in enumerate(A):
    for _ in range(v):
        dp_old = dp.copy()
        for j, k in itertools.product(range(1, 10), range(13)):
            x = (j * i + k) % 13
            dp[x] += dp_old[k]
            dp[x] %= mod
print(dp[5])


Fixed submission:
import itertools

S = input()[::-1]
mod = 10**9 + 7
A = [0] * 13
N = 0
for i, v in enumerate(S):
    p = pow(10, i, 13)
    if v == "?":
        A[p] += 1
    else:
        N += int(v) * p
        N %= 13
dp = [0] * 13
dp[N] = 1
for i, v in enumerate(A):
    for _ in range(v):
        dp_old = dp[:]
        for j, k in itertools.product(range(1, 10), range(13)):
            x = (j * i + k) % 13
            dp[x] += dp_old[k]
            dp[x] %= mod
print(dp[5])
Problem p02960 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    MOD = 10**9 + 7
    S = input()
    N = len(S)
    dp = [[0 for _ in range(13)] for _ in range(N + 1)]
    dp[0][0] = 1
    for i, c in enumerate(S):
        for j in range(10):
            if c != "?" and c != str(j):
                continue
            for k in range(13):
                dp[i + 1][(10 * k + j) % 13] += dp[i][k]
                dp[i + 1][(10 * k + j) % 13] %= MOD
    print(dp[N][5])


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    MOD = 10**9 + 7
    S = input()
    N = len(S)
    dp = [[0 for _ in range(13)] for _ in range(N + 1)]
    dp[0][0] = 1
    for i, c in enumerate(S):
        for j in range(10):
            if c != "?" and c != str(j):
                continue
            for k in range(13):
                dp[i + 1][(10 * k + j) % 13] += dp[i][k]
        for k in range(13):
            dp[i + 1][k] %= MOD
    print(dp[N][5])


if __name__ == "__main__":
    main()
Problem p02960 (Python)

Buggy submission (status: Time Limit Exceeded):
# import sys


# fin = sys.stdin.readline
MOD = 10**9 + 7
S = input()[::-1]
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
for i, c in enumerate(S, start=1):
    base = 10 ** (i - 1) % 13
    if c != "?":
        d = int(c) * base % 13
        for r in range(13):
            c = (d + r) % 13
            dp[i][c] += dp[i - 1][r]
            dp[i][c] %= MOD
    else:
        for d in range(10):
            d = d * base % 13
            for r in range(13):
                c = (d + r) % 13
                dp[i][c] += dp[i - 1][r]
                dp[i][c] %= MOD
print(dp[-1][5])


Fixed submission:
# import sys


# fin = sys.stdin.readline
MOD = 10**9 + 7
S = input()[::-1]
dp = [[0] * 13 for _ in range(len(S) + 1)]
dp[0][0] = 1
for i, c in enumerate(S, start=1):
    base = pow(10, i - 1, 13)
    if c != "?":
        d = int(c) * base % 13
        for r in range(13):
            c = (d + r) % 13
            dp[i][c] += dp[i - 1][r]
            dp[i][c] %= MOD
    else:
        for d in range(10):
            d = d * base % 13
            for r in range(13):
                c = (d + r) % 13
                dp[i][c] += dp[i - 1][r]
                dp[i][c] %= MOD
print(dp[-1][5])
Problem p02960 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define srep(i, from, to) for (int i = from; i < (int)(to); i++)
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;

int main() {
  string s;
  cin >> s;
  int MOD = 1000000007;
  int N = 13;
  int len = s.length();
  int mul = 1;

  vvi dp(N, vi(len + 1, 0));
  dp[0][0] = 1;
  reverse(s.begin(), s.end());

  srep(i, 1, len + 1) {
    char c = s[i - 1];
    if (c == '?') {
      rep(k, 10) {
        rep(x, N) {
          int am = (x + k * mul) % N;
          dp[am][i] += dp[x][i - 1];
          dp[am][i] %= MOD;
        }
      }
    } else {
      int k = c - '0';
      rep(x, N) {
        int am = (x + k * mul) % N;
        dp[am][i] += dp[x][i - 1];
        dp[am][i] %= MOD;
      }
    }

    mul *= 10;
  }

  int ans = dp[5][len];
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reps(i, n) for (int i = 1; i <= (int)(n); i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define srep(i, from, to) for (int i = from; i < (int)(to); i++)
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;

int main() {
  string s;
  cin >> s;
  int MOD = 1000000007;
  int N = 13;
  int len = s.length();
  int mul = 1;

  vvi dp(N, vi(len + 1, 0));
  dp[0][0] = 1;
  reverse(s.begin(), s.end());

  srep(i, 1, len + 1) {
    char c = s[i - 1];
    if (c == '?') {
      rep(k, 10) {
        rep(x, N) {
          int am = (x + k * mul) % N;
          dp[am][i] += dp[x][i - 1];
          dp[am][i] %= MOD;
        }
      }
    } else {
      int k = c - '0';
      rep(x, N) {
        int am = (x + k * mul) % N;
        dp[am][i] += dp[x][i - 1];
        dp[am][i] %= MOD;
      }
    }

    mul *= 10;
    mul %= N;
  }

  int ans = dp[5][len];
  cout << ans << endl;

  return 0;
}