alexjercan/bugnet huggingface dataset:
Problem p00885 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef pair<bool, int> pbi;

pbi dp[41][4];
int n, p[41], t[41];

pbi solve(int i, int k) {
  if (~dp[i][k].second)
    return dp[i][k];

  if (i == n - 1)
    return dp[i][k] = make_pair(true, -p[i]);

  pbi res(false, i);
  if (k < 3 && abs(p[i + 1] - p[i]) * (k + 1) <= t[i + 1] - t[i]) {
    pbi tmp = solve(i + 1, k + 1);
    if (tmp.first)
      tmp.second -= abs(p[i + 1] - p[i]);
    res = max(res, tmp);
  }
  if (p[i] * (k + 1) + p[i + 1] <= t[i + 1] - t[i]) {
    pbi tmp = solve(i + 1, 1);
    if (tmp.first)
      tmp.second -= p[i] + p[i + 1];
    res = max(res, tmp);
  }
  return dp[i][k] = res;
}

int main() {
  for (; scanf("%d", &n), n;) {
    p[0] = t[0] = 0;
    rep(i, n) scanf("%d%d", p + i + 1, t + i + 1);
    n++;

    rep(i, n) rep(k, 4) dp[i][k].second = -1;

    pbi res = solve(0, 0);
    if (res.first)
      printf("OK %d\\n", -res.second);
    else
      printf("NG %d\\n", res.second + 1);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef pair<bool, int> pbi;

int n, p[41], t[41];

pbi solve(int i, int k) {
  if (i == n - 1)
    return make_pair(true, -p[i]);

  pbi res(false, i);
  if (k < 3 && abs(p[i + 1] - p[i]) * (k + 1) <= t[i + 1] - t[i]) {
    pbi tmp = solve(i + 1, k + 1);
    if (tmp.first)
      tmp.second -= abs(p[i + 1] - p[i]);
    res = max(res, tmp);
  }
  if (p[i] * (k + 1) + p[i + 1] <= t[i + 1] - t[i]) {
    pbi tmp = solve(i + 1, 1);
    if (tmp.first)
      tmp.second -= p[i] + p[i + 1];
    res = max(res, tmp);
  }
  return res;
}

int main() {
  for (; scanf("%d", &n), n;) {
    p[0] = t[0] = 0;
    rep(i, n) scanf("%d%d", p + i + 1, t + i + 1);
    n++;

    pbi res = solve(0, 0);
    if (res.first)
      printf("OK %d\\n", -res.second);
    else
      printf("NG %d\\n", res.second + 1);
  }

  return 0;
}
Problem p00885 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, bg, ed) for (int i = bg; i < ed; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define fi first
#define se second
#define pb push_back
typedef long long LL;
const LL INF = 1e10;

template <typename T> bool chmin(T &l, T r) {
  bool ret = l > r;
  if (ret)
    l = r;
  return ret;
}

void solve(int n) {
  vector<int> p(n);
  vector<int> t(n);
  REP(i, n) cin >> p[i] >> t[i];
  vector<LL> dp(4, INF);
  dp[1] = p[0];
  if (p[0] > t[0]) {
    cout << "NG 1" << endl;
    return;
  }
  FOR(i, 1, n) {
    vector<LL> nxt(4, INF);
    LL tt = t[i] - t[i - 1];
    LL dd = abs(p[i] - p[i - 1]);
    FOR(j, 1, 3) {
      if (tt >= dd * (j + 1))
        chmin(nxt[j + 1], dp[j] + dd);
    }
    FOR(j, 1, 4) {
      if (tt >= p[i - 1] * (j + 1) + p[i])
        chmin(nxt[1], dp[j] + p[i - 1] + p[i]);
    }
    if (min({nxt[0], nxt[1], nxt[2], nxt[3]}) == INF) {
      cout << "NG " << i + 1 << endl;
      return;
    }

    swap(dp, nxt);
    // REP(j,4)cout<<dp[j]<<" ";cout<<endl;
  }
  LL ret = INF;
  REP(i, 4) chmin(ret, dp[i] + p.back());
  cout << "OK " << ret << endl;
}
int main() {
  int n;
  while (cin >> n, n)
    solve(n);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define FOR(i, bg, ed) for (int i = bg; i < ed; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

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

pair<string, vector<string>> parse(string s) {
  vector<string> ret(1, "");
  for (auto &it : s) {
    if (it == ':' || it == ',')
      ret.pb("");
    else if (it == '.')
      break;
    else
      ret.back() += it;
  }
  string key = ret.front();
  reverse(ALL(ret));
  ret.pop_back();
  reverse(ALL(ret));
  // cout<<key<<":";for(auto &it:ret)cout<<it;cout<<endl;
  return {key, ret};
}
int main() {
  int n;
  while (cin >> n, n) {
    map<string, vector<string>> g;
    string tmp;
    vector<string> v;
    string st;
    cin >> st;
    tie(tmp, v) = parse(st);
    set<string> now(ALL(v));
    REP(i, n - 1) {
      string s;
      cin >> s;
      string key;
      vector<string> group;
      tie(key, group) = parse(s);
      g[key] = group;
    }
    bool update = true;
    set<string> used;
    while (update) {
      // for(auto &it:now)cout<<it<<",";cout<<endl;
      update = false;
      set<string> nxt;
      for (auto &it : now)
        if (g.count(it)) {
          update = true;
          if (used.count(it) == 0) {
            used.insert(it);
            for (auto &i : g[it])
              nxt.insert(i);
          }
        } else {
          nxt.insert(it);
        }
      swap(now, nxt);
    }
    cout << now.size() << endl;
  }
  return 0;
}
Problem p00885 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int n, dis[41], t[41], maxball, ans;

void saiki(int x, int now, int ball, int sum, int kai) {
  if (ans < sum)
    return;
  maxball = max(maxball, kai);
  if (kai == n && !ball)
    ans = min(ans, sum);
  if (ball)
    saiki(0, now + x * (1 + ball), 0, sum + x, kai);
  if (ball == 3 || kai == n)
    return;

  int nx = dis[kai];
  int nsum = sum + abs(x - dis[kai]);
  int nnow = now + abs(x - dis[kai]) * (1 + ball);
  if (nnow <= t[kai])
    saiki(nx, t[kai], ball + 1, nsum, kai + 1);
}

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> dis[i] >> t[i];
    sort(t, t + n);
    maxball = 0, ans = 100000000;
    saiki(0, 0, 0, 0, 0);
    if (maxball == n)
      cout << "OK " << ans << endl;
    else
      cout << "NG " << maxball + 1 << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n, dis[41], t[41], maxball, ans;

void saiki(int x, int now, int ball, int sum, int kai) {
  maxball = max(maxball, kai);
  if (kai == n && !ball)
    ans = min(ans, sum);
  if (ball)
    saiki(0, now + x * (1 + ball), 0, sum + x, kai);
  if (ball == 3 || kai == n)
    return;

  int nx = dis[kai];
  int nsum = sum + abs(x - dis[kai]);
  int nnow = now + abs(x - dis[kai]) * (1 + ball);
  if (nnow <= t[kai])
    saiki(nx, t[kai], ball + 1, nsum, kai + 1);
}

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> dis[i] >> t[i];
    sort(t, t + n);
    maxball = 0, ans = 100000000;
    saiki(0, 0, 0, 0, 0);
    if (maxball == n)
      cout << "OK " << ans << endl;
    else
      cout << "NG " << maxball + 1 << endl;
  }
  return 0;
}
Problem p00885 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
#define INF (1e9)
#define N 41
#define B 4

int main() {
  int n, p[N], t[N], dp[N][B], tim, pos, ans;
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> p[i] >> t[i];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < B; j++)
        dp[i][j] = INF;
    dp[0][0] = tim = pos = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < B; j++) {
        if (dp[i][j] == INF)
          continue;
        if (i >= 1 && j >= 1 && t[i - 1] + p[i - 1] * (j + 1) + p[i] <= t[i])
          dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + p[i - 1] + p[i]);
        if (j <= 2 && tim + abs(p[i] - pos) * (j + 1) <= t[i])
          dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j] + abs(p[i] - pos));
      }
      tim = t[i], pos = p[i];
    }
    ans = INF;
    for (int i = 0; i < B; i++)
      ans = min(ans, dp[n][i]);
    if (ans != INF)
      cout << "OK " << ans + p[n - 1] << endl;
    else {
      for (int i = 0; i <= n; i++) {
        int flag = 1;
        for (int j = 0; j < B; j++)
          if (dp[i][j] != INF)
            flag = 0;
        if (flag) {
          cout << "NG " << i << endl;
          break;
        }
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#define N 40
using namespace std;
int n, p[N], t[N], ans, k;
void func(int, int, int, int, int);

int main() {
  while (1) {
    cin >> n;
    if (!n)
      break;
    for (int i = 0; i < n; i++)
      cin >> p[i] >> t[i];
    ans = (1e9);
    func(0, 0, 0, 0, 0);
    if (ans == (1e9))
      cout << "NG " << k + 1 << endl;
    else
      cout << "OK " << ans + p[n - 1] << endl;
  }
  return 0;
}

void func(int x, int pos, int tim, int sum, int b) {
  if (x == n)
    ans = min(sum, ans);
  else {
    int r = 0;
    if (x && tim + pos * (1 + b) + p[x] <= t[x])
      func(x + 1, p[x], t[x], sum + pos + p[x], 1);
    else
      r++;
    if (b != 3 && tim + abs(p[x] - pos) * (1 + b) <= t[x])
      func(x + 1, p[x], t[x], sum + abs(p[x] - pos), b + 1);
    else
      r++;
    if (r == 2)
      k = x;
  }
}
Problem p00885 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const ll INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
int p[50], t[50], dp[50][5];
int n;

int solve(int i, int j) {
  if (i == n)
    return p[n];
  if (dp[i][j] >= 0)
    return dp[i][j];
  int res = INF;
  if (p[i] * (j + 1) + p[i + 1] <= t[i + 1] - t[i])
    chmin(res, solve(i + 1, 1) + p[i] + p[i + 1]);
  if (j < 3 && abs(p[i] - p[i + 1]) * (j + 1) <= t[i + 1] - t[i])
    chmin(res, solve(i + 1, j + 1) + abs(p[i] - p[i + 1]));
  return dp[i][j] = res;
}

int main() {
  while (cin >> n, n) {
    MS(dp, -1);
    FOR(i, 1, n + 1) cin >> p[i] >> t[i];
    int ans = solve(0, 0);
    if (ans == INF) {
      while (ans == INF) {
        MS(dp, -1);
        n--;
        ans = solve(0, 0);
      }
      cout << "NG " << n + 1 << endl;
    } else
      cout << "OK " << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const ll INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
int p[50], t[50], dp[50][5];
int n;

int solve(int i, int j) {
  if (i == n)
    return p[n];
  int res = INF;
  if (p[i] * (j + 1) + p[i + 1] <= t[i + 1] - t[i])
    chmin(res, solve(i + 1, 1) + p[i] + p[i + 1]);
  if (j < 3 && abs(p[i] - p[i + 1]) * (j + 1) <= t[i + 1] - t[i])
    chmin(res, solve(i + 1, j + 1) + abs(p[i] - p[i + 1]));
  return dp[i][j] = res;
}

int main() {
  while (cin >> n, n) {
    MS(dp, -1);
    FOR(i, 1, n + 1) cin >> p[i] >> t[i];
    int ans = solve(0, 0);
    if (ans == INF) {
      while (ans == INF) {
        MS(dp, -1);
        n--;
        ans = solve(0, 0);
      }
      cout << "NG " << n + 1 << endl;
    } else
      cout << "OK " << ans << endl;
  }
  return 0;
}
Problem p00886 (C++) — buggy submission, verdict: Time Limit Exceeded

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

void solve(int N) {
  vector<int> v(401);
  int max_len = 0;
  for (int i = 0; i < N * (N - 1) / 2; i++) {
    int j;
    cin >> j;
    v[j]++;
    max_len = max(max_len, j);
  }

  set<vector<int>> ans;
  for (int i = 0; i < (1 << (N - 1)); i++) {
    vector<int> s;
    auto count = v;
    bool flag = false;

    if (i % 2)
      s.push_back(max_len);
    else
      s.push_back(0);

    int t = max_len;
    int j = 0;
    while (t) {
      if (!count[t]) {
        t--;
        continue;
      }

      int position = (i & (1 << j)) ? max_len - t : t;
      for (auto k : s) {
        int dist = abs(k - position);
        if (count[dist])
          count[dist]--;
        else {
          flag = true;
          break;
        }
      }
      if (flag)
        break;
      s.push_back(position);
      j++;
    }
    if (flag)
      continue;
    sort(s.begin(), s.end());
    vector<int> ad;
    for (int i = 0; i < N - 1; i++)
      ad.push_back(s[i + 1] - s[i]);
    ans.insert(ad);
  }

  for (auto avec : ans) {
    for (int i = 0; i < N - 1; i++) {
      if (i < N - 2)
        cout << avec[i] << " ";
      else
        cout << avec[i] << endl;
    }
  }

  cout << "-----" << endl;
}

int main() {
  int N;
  while (cin >> N, N) {
    solve(N);
  }

  return 0;
}

Error: TLE

Fixed submission:

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

void solve(int N) {
  vector<int> v(401);
  int max_len = 0;
  for (int i = 0; i < N * (N - 1) / 2; i++) {
    int j;
    cin >> j;
    v[j]++;
    max_len = max(max_len, j);
  }

  set<vector<int>> ans;
  for (int i = 0; i < (1 << N); i++) {
    vector<int> s;
    auto count = v;
    bool flag = false;

    if (i % 2)
      s.push_back(max_len);
    else
      s.push_back(0);

    int t = max_len;
    int j = 0;
    while (t) {
      if (!count[t]) {
        t--;
        continue;
      }

      int position = (i & (1 << j)) ? max_len - t : t;
      for (auto k : s) {
        int dist = abs(k - position);
        if (count[dist])
          count[dist]--;
        else {
          flag = true;
          break;
        }
      }
      if (flag)
        break;
      s.push_back(position);
      j++;
    }
    if (flag)
      continue;
    sort(s.begin(), s.end());
    vector<int> ad;
    for (int i = 0; i < N - 1; i++)
      ad.push_back(s[i + 1] - s[i]);
    ans.insert(ad);
  }

  for (auto avec : ans) {
    for (auto i : avec)
      cout << i << " ";
    cout << endl;
  }

  cout << "----" << endl;
}

int main() {
  int N;
  while (cin >> N, N) {
    solve(N);
  }

  return 0;
}
Problem p00887 (C++) — buggy submission, verdict: Runtime Error

// 59
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  for (int m, n, d; cin >> m >> n >> d, m | n | d;) {
    vector<vector<int>> v(m * n, vector<int>(m * n + 1));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        for (int k = 0; k < n; k++) {
          for (int l = 0; l < m; l++) {
            int cd = abs(i - k) + abs(j - l);
            v[i * m + j][k * m + l] = cd == 0 || cd == d;
          }
        }
        cin >> v[i * m + j].back();
      }
    }
    int dn = 0;
    for (int i = 0; i < n * m; i++) {
      int j;
      for (j = dn; j < v.size(); j++) {
        if (v[j][i])
          break;
      }
      if (j == v.size())
        continue;
      swap(v[dn], v[j]);
      for (int k = dn + 1; k < v.size(); k++) {
        if (v[k][i]) {
          for (int l = 0; l < v[k].size(); l++) {
            v[k][l] ^= v[dn][l];
          }
        }
      }
      dn++;
    }
    int i;
    for (i = dn; i < v.size(); i++) {
      if (v[i].back())
        break;
    }
    cout << (i == v.size()) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

// 59
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

int main() {
  for (int m, n, d; cin >> m >> n >> d, m | n | d;) {
    vector<vector<int>> v(m * n, vector<int>(m * n + 1));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        for (int k = 0; k < n; k++) {
          for (int l = 0; l < m; l++) {
            int cd = abs(i - k) + abs(j - l);
            v[i * n + j][k * n + l] = cd == 0 || cd == d;
          }
        }
        cin >> v[i * n + j].back();
      }
    }
    int dn = 0;
    for (int i = 0; i < n * m; i++) {
      int j;
      for (j = dn; j < v.size(); j++) {
        if (v[j][i])
          break;
      }
      if (j == v.size())
        continue;
      swap(v[dn], v[j]);
      for (int k = dn + 1; k < v.size(); k++) {
        if (v[k][i]) {
          for (int l = 0; l < v[k].size(); l++) {
            v[k][l] ^= v[dn][l];
          }
        }
      }
      dn++;
    }
    int i;
    for (i = dn; i < v.size(); i++) {
      if (v[i].back())
        break;
    }
    cout << (i == v.size()) << endl;
  }
  return 0;
}
Problem p00889 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;

typedef long long ll;

int N, S, W, Q, a[100005];

ll mod_pow(ll x, ll n) {
  ll res = 1;
  while (n) {
    if (n % 2)
      res = res * x % Q;
    x = x * x % Q;
    n /= 2;
  }
  return res;
}

void init() {
  int g = S;
  for (int i = 0; i < N; i++) {
    a[i] = (g / 7) % 10;
    if (g % 2 == 0) {
      g = (g / 2);
    } else {
      g = (g / 2) ^ W;
    }
  }
}

signed main() {

  while (1) {

    cin >> N >> S >> W >> Q;
    if (!N && !S && !W && !Q)
      break;

    init();

    map<int, int> cnt;

    int ans = 0, A = 1, B = 0;
    int sum = 0;

    if (a[0] != 0) {
      cnt[a[0] % Q]++;
      sum++;
      if (a[0] % Q == 0)
        ans++;
    }

    for (int i = 1; i < N; i++) {

      A = A * 10 % Q;
      B = (B * 10 + a[i]) % Q;

      if (a[i] != 0) {

        int num = (mod_pow(A, Q - 2) * (a[i] + (Q - B))) % Q;

        if (!(A == 0 && (a[i] + (Q - B)) % Q != 0)) {
          cnt[num]++;
          sum++;
          assert((num * A + B) % Q == a[i] % Q);
        }
      }

      int num = (mod_pow(A, Q - 2) * (Q - B)) % Q;

      if (!(A == 0 && (Q - B) % Q != 0)) {
        if (A != 0)
          ans += cnt[num];
        else
          ans += sum;
        assert((num * A + B) % Q == 0);
      }
    }

    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;

typedef long long ll;

int N, S, W, Q, a[100005];

ll mod_pow(ll x, ll n) {
  if (x == 0)
    return 1;
  ll res = 1;
  while (n) {
    if (n % 2)
      res = res * x % Q;
    x = x * x % Q;
    n /= 2;
  }
  return res;
}

void init() {
  int g = S;
  for (int i = 0; i < N; i++) {
    a[i] = (g / 7) % 10;
    if (g % 2 == 0) {
      g = (g / 2);
    } else {
      g = (g / 2) ^ W;
    }
  }
}

signed main() {

  while (1) {

    cin >> N >> S >> W >> Q;
    if (!N && !S && !W && !Q)
      break;

    init();

    map<int, int> cnt;

    int ans = 0, A = 1, B = 0;

    cnt[a[0] % Q]++;
    if (a[0] % Q == 0)
      ans++;
    assert(a[0] != 0);
    for (int i = 1; i < N; i++) {

      A = A * 10 % Q;
      B = (B * 10 + a[i]) % Q;

      if (a[i] != 0) {

        int num = mod_pow(A, Q - 2) * (a[i] + (Q - B)) % Q;

        cnt[num]++;
      }

      int num = mod_pow(A, Q - 2) * (Q - B) % Q;

      ans += cnt[num];
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p00889 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
#define F first
#define S second
const ll MOD = 1000000007;
const ll E = 1e18;

const ll MX = 100000;

ll a[MX];

int main() {
  ll n, s, w, p;
  while (cin >> n >> s >> w >> p) {
    if (n == 0) {
      break;
    }
    int g = s;
    for (int i = 0; i < n; i++) {
      a[i] = (g / 7) % 10;
      if (g % 2 == 0) {
        g /= 2;
      } else {
        g = (g / 2) ^ w;
      }
      // cout<<a[i]<<" ";
    }
    reverse(a, a + n);
    // for(int i=0;i<n;i++){cout<<a[i]<<" ";}
    // cout<<endl;
    if (p == 2 || p == 5) {
      ll ans = 0;
      ll w = 0;
      for (int i = 0; i < n; i++) {
        if (a[i] % p == 0) {
          w++;
        }
        if (a[i] != 0) {
          ans += w;
        }
      }
      cout << ans << endl;
    } else {
      map<ll, ll> M;
      ll ans = 0;
      ll sum = 0;
      M[0]++;
      ll k = 1;
      for (int i = 0; i < n; i++) {
        sum += k * a[i];
        sum %= p;
        if (a[i] != 0) {
          ans += M[sum];
        }
        M[sum]++;
        (k *= 10) %= p;
        // cout<<i<<" "<<ans<<endl;
      }
      cout << ans << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#define ll long long
const int N = 1001000;
#define BIG (int)(1e9 + 7)
#include <bits/stdc++.h>
using namespace std;
int n, s, w, arr[N];
string q;
const int base = 10;
ll b[N], h[N];
ll get(int l, int r) {
  ll res = h[r];
  ll minus = (h[l - 1] * b[r - l + 1]) % BIG;
  res -= minus;
  res = (res % BIG + BIG) % BIG;
  return res;
}
void solve() {
  cin >> n >> s >> w >> q;
  int g = s;
  if (n == 0)
    return;
  for (int i = 0; i < n; i++) {
    arr[i] = (g / 7) % 10;
    if (g % 2 == 0)
      g = g / 2;
    else
      g = (g / 2) ^ w;
  }
  b[0] = 1ll;
  h[0] = 0ll;
  for (int i = 0; i < n; i++) {
    h[i + 1] = (base * h[i] + arr[i]) % BIG;
    b[i + 1] = (base * b[i]) % BIG;
  }
  ll hq = 0ll;
  for (int i = 0; i < q.size(); i++)
    hq = (hq * base + q[i] - '0') % BIG;
  ll ans = 0;
  // cout << "Hello: w = " << w << endl;
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      if (arr[i - 1] == 0)
        break;
      ll mul = get(i, j);
      if (mul % hq == 0)
        ans++, cout << "1";
    }
  }
  // cout << "result: " << endl;
  cout << ans << endl;
  solve();
}
int main(void) {
  solve();
  return 0;
}