Problem p03114 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int Mod = 1e9 + 7;
typedef long long ll;

void inm_to(int &a, int b) { a = (ll)a * b % Mod; }
int inm(int a, int b) { return (ll)a * b % Mod; }
void add_to(int &a, int b) {
  a += b;
  if (a >= Mod)
    a -= Mod;
}

int power(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      inm_to(ans, a);
    inm_to(a, a);
    b >>= 1;
  }
  return ans;
}

int inv(int a) { return power(a, Mod - 2); }

/////////////////////////////////////////////////////////

int N, M, cnt, pos[50], where[50], comp;
map<int, int> id;
pair<int, int> R[50];
int coef[1 << 16], dp[1 << 16], dp2[1 << 16];

void dfs(int node, int comp) {
  if (where[node])
    return;

  where[node] = comp;
  int i;
  for (i = 1; i <= M; ++i)
    if (R[i].first == pos[node])
      dfs(id[R[i].second], comp);

  for (i = 1; i <= M; ++i)
    if (R[i].second == pos[node])
      dfs(id[R[i].first], comp);
}

void calc_subsets() {
  int i, j, k;
  for (i = 0; i < (1 << comp); ++i) {
    coef[i] = 1;

    for (j = 0; j + 1 < cnt; ++j)
      if ((i & (1 << where[j])) && (i & (1 << where[j + 1])))
        inm_to(coef[i], 1 + inm(9, inv(power(10, pos[j + 1] - pos[j]) - 1)));
  }
}

void do_dp() {
  int i, j;
  for (i = 0; i < (1 << comp); ++i) {
    for (j = i; j; j = (j - 1) & i)
      add_to(dp2[i], inm(dp[j], coef[i ^ j]));
    add_to(dp2[i], inm(dp[0], coef[i]));
  }
}

void solve() {
  int i, j;
  dp[0] = 1;

  for (i = 0; i < 9; ++i) {
    do_dp();
    for (j = 0; j < (1 << comp); ++j)
      dp[j] = dp2[j], dp2[j] = 0;
  }

  int ans = dp[(1 << comp) - 1];

  inm_to(ans, power(10, pos[0]));
  inm_to(ans, power(10, N - pos[cnt - 1]));

  for (i = 0; i + 1 < cnt; ++i)
    inm_to(ans, inm(power(10, pos[i + 1] - pos[i]) - 1, inv(9)));
  cout << inm(ans, inv(9)) << '\\n';
}

int main() {
  //  freopen("input", "r", stdin);
  cin.sync_with_stdio(false);

  cin >> N >> M;

  int i, x, y;
  for (i = 1; i <= M; ++i) {
    cin >> x >> y;
    --x;
    R[i] = {x, y};
    id[x] = 1;
    id[y] = 1;
  }

  cnt = 0;
  for (auto &it : id) {
    it.second = cnt;
    pos[cnt] = it.first;
    cnt++;
  }

  comp = 0;
  for (i = 0; i < cnt; ++i)
    if (!where[i]) {
      ++comp;
      dfs(i, comp);
    }

  for (i = 0; i < cnt; ++i)
    where[i]--;

  calc_subsets();
  solve();

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int Mod = 1e9 + 7;
typedef long long ll;

void inm_to(int &a, int b) { a = (ll)a * b % Mod; }
int inm(int a, int b) { return (ll)a * b % Mod; }
void add_to(int &a, int b) {
  a += b;
  if (a >= Mod)
    a -= Mod;
}

int power(int a, int b) {
  int ans = 1;
  while (b) {
    if (b & 1)
      inm_to(ans, a);
    inm_to(a, a);
    b >>= 1;
  }
  return ans;
}

int inv(int a) { return power(a, Mod - 2); }

/////////////////////////////////////////////////////////

int N, M, cnt, pos[50], where[50];
map<int, int> id;
pair<int, int> R[50];
int coef[1 << 16], dp[1 << 16], dp2[1 << 16];

void dfs(int node, int comp) {
  if (where[node])
    return;

  where[node] = comp;
  int i;
  for (i = 1; i <= M; ++i)
    if (R[i].first == pos[node])
      dfs(id[R[i].second], comp);

  for (i = 1; i <= M; ++i)
    if (R[i].second == pos[node])
      dfs(id[R[i].first], comp);
}

void calc_subsets() {
  int i, j, k;
  for (i = 0; i < (1 << cnt); ++i) {
    coef[i] = 1;

    for (j = 0; coef[i] && j < cnt; ++j)
      for (k = j + 1; coef[i] && k < cnt; ++k)
        if (where[j] == where[k] && ((i >> j) & 1) != ((i >> k) & 1))
          coef[i] = 0;

    if (!coef[i])
      continue;

    for (j = 0; j + 1 < cnt; ++j)
      if ((i & (1 << j)) && (i & (1 << (j + 1))))
        inm_to(coef[i], 1 + inm(9, inv(power(10, pos[j + 1] - pos[j]) - 1)));
  }
}

void do_dp() {
  int i, j;
  for (i = 0; i < (1 << cnt); ++i) {
    for (j = i; j; j = (j - 1) & i)
      add_to(dp2[i], inm(dp[j], coef[i ^ j]));
    add_to(dp2[i], inm(dp[0], coef[i]));
  }
}

void solve() {
  int i, j;
  dp[0] = 1;

  for (i = 0; i < 9; ++i) {
    do_dp();
    for (j = 0; j < (1 << cnt); ++j)
      dp[j] = dp2[j], dp2[j] = 0;
  }

  int ans = dp[(1 << cnt) - 1];

  inm_to(ans, power(10, pos[0]));
  inm_to(ans, power(10, N - pos[cnt - 1]));

  for (i = 0; i + 1 < cnt; ++i)
    inm_to(ans, inm(power(10, pos[i + 1] - pos[i]) - 1, inv(9)));
  cout << inm(ans, inv(9)) << '\\n';
}

int main() {
  //   freopen("input", "r", stdin);
  cin.sync_with_stdio(false);

  cin >> N >> M;

  int i, x, y;
  for (i = 1; i <= M; ++i) {
    cin >> x >> y;
    --x;
    R[i] = {x, y};
    id[x] = 1;
    id[y] = 1;
  }

  cnt = 0;
  for (auto &it : id) {
    it.second = cnt;
    pos[cnt] = it.first;
    cnt++;
  }

  int comp = 0;
  for (i = 0; i < cnt; ++i)
    if (!where[i]) {
      ++comp;
      dfs(i, comp);
    }

  calc_subsets();
  solve();

  return 0;
}