Problem p03867 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll mod = 1e9 + 7;

ll powll(ll x, ll y) {
  ll res = 1LL;
  while (y) {
    if (y & 1LL)
      res *= x;
    res %= mod;
    x = (x * x) % mod;
    y >>= 1LL;
  }
  return res;
}

ll naive(ll n, ll k) {
  set<VI> st;
  int m = (n + 1) / 2;
  VI a(n);
  REP(mask, powll(k, m)) {
    REP(i, m) {
      a[i] = (mask / powll(k, i)) % k;
      a[n - 1 - i] = a[i];
    }
    VI b(n);
    REP(s, n) {
      REP(i, n) b[i] = a[(i + s) % n];
      st.insert(b);
    }
  }
  return st.size();
}

int main() {
  ll n, k;
  cin >> n >> k;

  if (k <= 10) {
    cout << naive(n, k) << endl;
  }

  VL factor;
  for (ll x = 1; x * x <= n; ++x) {
    if (n % x == 0) {
      factor.push_back(x);
      if (x * x != n)
        factor.push_back(n / x);
    }
  }
  sort(ALL(factor));

  int m = factor.size();
  VL way(m);
  ll ans = 0;

  REP(i, m) {
    ll x = factor[i];
    way[i] = powll(k, (x + 1) / 2);
    REP(j, i) {
      if (x % factor[j] == 0) {
        way[i] = (way[i] - way[j] + mod) % mod;
      }
    }
    ll tmp;
    if (x % 2)
      tmp = (way[i] * x) % mod;
    else
      tmp = (way[i] * (x / 2)) % mod;
    ans = (ans + tmp) % mod;
    // cout << "f " << x << "  ";
    // cout << tmp << endl;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define ALL(c) (c).begin(), (c).end()

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

int in() {
  int x;
  scanf("%d", &x);
  return x;
}
ll lin() {
  ll x;
  scanf("%lld", &x);
  return x;
}

const ll mod = 1e9 + 7;

ll powll(ll x, ll y) {
  ll res = 1LL;
  while (y) {
    if (y & 1LL)
      res *= x;
    res %= mod;
    x = (x * x) % mod;
    y >>= 1LL;
  }
  return res;
}

ll naive(ll n, ll k) {
  set<VI> st;
  int m = (n + 1) / 2;
  VI a(n);
  REP(mask, powll(k, m)) {
    REP(i, m) {
      a[i] = (mask / powll(k, i)) % k;
      a[n - 1 - i] = a[i];
    }
    VI b(n);
    REP(s, n) {
      REP(i, n) b[i] = a[(i + s) % n];
      st.insert(b);
    }
  }
  return st.size();
}

int main() {
  ll n, k;
  cin >> n >> k;

  // if (k <= 10){
  //     cout << naive(n, k) << endl;
  // }

  VL factor;
  for (ll x = 1; x * x <= n; ++x) {
    if (n % x == 0) {
      factor.push_back(x);
      if (x * x != n)
        factor.push_back(n / x);
    }
  }
  sort(ALL(factor));

  int m = factor.size();
  VL way(m);
  ll ans = 0;

  REP(i, m) {
    ll x = factor[i];
    way[i] = powll(k, (x + 1) / 2);
    REP(j, i) {
      if (x % factor[j] == 0) {
        way[i] = (way[i] - way[j] + mod) % mod;
      }
    }
    ll tmp;
    if (x % 2)
      tmp = (way[i] * x) % mod;
    else
      tmp = (way[i] * (x / 2)) % mod;
    ans = (ans + tmp) % mod;
    // cout << "f " << x << "  ";
    // cout << tmp << endl;
  }
  cout << ans << endl;

  return 0;
}