Problem p00889 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

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

ll nums[200000];
ll m[200000];
map<ll, ll> mods;

void make_input(int N, int S, int W) {
  int g = S;
  rep(i, N) {
    nums[i] = (g / 7) % 10;
    if (g % 2 == 0)
      g /= 2;
    else
      g = (g / 2) ^ W;
  }
}

int main() {
  int N, S, Q, W;
  while (cin >> N >> S >> W >> Q && (N || S || W || Q)) {
    ll res = 0;
    ll n = 1;
    mods.clear();
    make_input(N, S, W);
    if (Q == 2) {
      int cnt = 0;
      rep(i, N) {
        if (nums[N - 1 - i] % 2 == 0) {
          cnt++;
        }
        if (nums[N - 1 - i] != 0)
          res += cnt;
      }
    } else if (Q == 5) {
      int cnt = 0;
      rep(i, N) {
        if (nums[N - 1 - i] % 5 == 0) {
          cnt++;
        }
        if (nums[N - 1 - i] != 0)
          res += cnt;
      }
    } else {
      make_input(N, S, W);
      mods[0]++;
      ll num = 0;
      rep(i, N) {
        ll pre = (i == 0 ? 0 : m[i - 1]);
        m[i] = pre + n * nums[N - i - 1];
        m[i] %= Q;
        n = (n * 10) % Q;
      }
      rep(i, N) {
        //	cout << m[i] << endl;
        if (nums[N - i - 1] != 0)
          res += mods[m[i]];
        mods[m[i]]++;
      }
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

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

ll nums[200000];
ll m[200000];
multiset<ll> mods;

void make_input(int N, int S, int W) {
  int g = S;
  rep(i, N) {
    nums[i] = (g / 7) % 10;
    if (g % 2 == 0)
      g /= 2;
    else
      g = (g / 2) ^ W;
  }
}

int main() {
  int N, S, Q, W;
  while (cin >> N >> S >> W >> Q && (N || S || W || Q)) {
    int res = 0;
    ll n = 1;
    mods.clear();

    make_input(N, S, W);
    mods.insert(0);
    ll num = 0;
    rep(i, N) {
      ll pre = (i == 0 ? 0 : m[i - 1]);
      m[i] = pre + n * nums[N - i - 1];
      m[i] %= Q;
      n = (n * 10) % Q;
    }
    rep(i, N) {
      //	cout << m[i] << endl;
      if (nums[N - i - 1] != 0)
        res += mods.count(m[i]);
      mods.insert(m[i]);
    }
    cout << res << endl;
    ;
  }
  return 0;
}