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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

const int mod = 1000000007;

struct Mod {
  int num;
  Mod() : num(0) { ; }
  Mod(long long int n) : num((n % mod + mod) % mod) { ; }
  Mod(int n) : num((n % mod + mod) % mod) { ; }
  operator int() { return num; }
};

Mod operator+(Mod a, Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(long long int a, Mod b) { return Mod(a) + b; }
Mod operator-(Mod a, Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(long long int a, Mod b) { return Mod(a) - b; }
Mod operator*(Mod a, Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(long long int a, Mod b) { return Mod(a) * b; }
Mod operator+=(Mod &a, Mod b) { return a = a + b; }
Mod operator+=(long long int &a, Mod b) { return a = a + b; }
Mod operator-=(Mod &a, Mod b) { return a = a - b; }
Mod operator-=(long long int &a, Mod b) { return a = a - b; }
Mod operator*=(Mod &a, Mod b) { return a = a * b; }
Mod operator*=(long long int &a, Mod b) { return a = a * b; }
Mod operator^(Mod a, int n) {
  if (n == 0)
    return Mod(1);
  Mod res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}
Mod inv(Mod a) { return a ^ (mod - 2); }
Mod operator/(Mod a, Mod b) {
  assert(b != 0);
  return a * inv(b);
}
Mod operator/(long long int a, Mod b) {
  assert(b != 0);
  return Mod(a) * inv(b);
}

#define MAX_N 1024000

Mod fact[MAX_N], factinv[MAX_N];
void init() {
  fact[0] = Mod(1);
  factinv[0] = 1;
  for (int i = 0; i < MAX_N - 1; ++i) {
    fact[i + 1] = fact[i] * Mod(i + 1);
    factinv[i + 1] = factinv[i] / Mod(i + 1);
  }
}
Mod comb(int a, int b) { return fact[a] * factinv[b] * factinv[a - b]; }

vector<map<vector<int>, Mod>> dp;

vector<vector<Mod>> keisann(const vector<vector<Mod>> l,
                            const vector<vector<Mod>> r) {
  vector<vector<Mod>> ans(l.size(), vector<Mod>(r[0].size()));
  assert(l[0].size() == r.size());
  for (int h = 0; h < l.size(); ++h) {
    for (int i = 0; i < r.size(); ++i) {
      for (int w = 0; w < r[0].size(); ++w) {

        ans[h][w] += l[h][i] * r[i][w];
      }
    }
  }
  return ans;
}

vector<vector<Mod>> powgyou(vector<vector<Mod>> a, const long long int n) {
  if (n == 1)
    return a;
  else {
    vector<vector<Mod>> ans(a.size(), vector<Mod>(a[0].size(), 0));
    ans = powgyou(a, n / 2);
    ans = keisann(ans, ans);
    if (n % 2) {
      ans = keisann(ans, a);
    }
    return ans;
  }
}

/*int N; cin >> N;
vector<pair<int, int>>cs;
for (int i = 0; i < N; ++i) {
        int x, r; cin >> x >> r;
        int l = x - r; int r = x + r;
        cs.emplace_back({ l,r });
}
sort(cs.begin(), cs.end(), [](const pair<int, int>l, const int pair<int, int>r)
{ return l.first == r.first ? l.second < r.second : l.first < r.first; }));
return 0;*/

/*int H, W; cin >> W >> H;
dp.resize(H+1);
vector<int>s(W,0);
s[0] = Mod(1);
dp[0][s] = Mod(1);
for (int i = 1; i <= H; ++i) {
        for (auto it = dp[i - 1].begin(); it != dp[i - 1].end(); ++it) {
                vector<int>pre((*it).first);
                for (int j = 0; j < (1 <<( W)); ++j) {
                        bitset<6>bs(j);
                        vector<int>now(W);
                        int num = 2;
                        for (int k = 0; k < W; ++k) {
                                if (bs[k]) {
                                        now[k] = num;
                                        num++;
                                }
                        }
                        for (int k = 0; k < W; ++k) {
                                if (now[k] && pre[k]) {
                                        now[k] = pre[k];
                                }
                        }
                        for (int k = 0; k < W - 1; ++k) {
                                if (now[k] && now[k + 1]) {
                                        int amin = min(now[k], now[k + 1]);
                                        now[k] = amin;
                                        now[k + 1] = amin;
                                }
                        }
                        for (int k = W - 1; k > 0; --k) {
                                if (now[k] && now[k - 1]) {
                                        int amin = min(now[k], now[k - 1]);
                                        now[k] = amin;
                                        now[k - 1] = amin;
                                }
                        }
                        int nnum = 2;
                        for (int k = 0; k < W; ++k) {
                                if (now[k]>=2) {
                                        int aa = now[k];
                                        for (int l = 0; l < W; ++l) {
                                                if (now[l] == aa)now[l] = nnum;
                                        }
                                        nnum++;
                                }
                        }
                        bool isok = false;
                        for (int k = 0; k < W; ++k) {
                                if (now[k] == 1) {
                                        isok = true;
                                }
                        }
                        if (isok) {

                                dp[i][now] += (*it).second;
                        }

                }
        }
}
Mod ans(0);
for (auto it = dp[H].begin(); it != dp[H].end(); ++it) {
        if ((*it).first[W - 1] == 1) {
                ans += (*it).second;
        }
}
cout << ans << endl;
return 0;*/

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
                      BidirectionalIterator first2,
                      BidirectionalIterator last2) {
  if ((first1 == last1) || (first2 == last2)) {
    return false;
  }
  BidirectionalIterator m1 = last1;
  BidirectionalIterator m2 = last2;
  --m2;
  while (--m1 != first1 && !(*m1 < *m2)) {
  }
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    // ?
    while (first2 != m2 && !(*m1 < *first2)) {
      ++first2;
    }
    first1 = m1;
    std::iter_swap(first1, first2);
    ++first1;
    ++first2;
  }
  if ((first1 != last1) && (first2 != last2)) {
    // ?
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2)) {
      std::iter_swap(--m1, m2);
      ++m2;
    }
    // ?
    std::reverse(first1, m1);
    std::reverse(first1, last1);
    std::reverse(m2, last2);
    std::reverse(first2, last2);
  }
  return !result;
}

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first, BidirectionalIterator middle,
                      BidirectionalIterator last) {
  return next_combination(first, middle, middle, last);
}

int main(void) {
  /*int N; cin >> N;
  vector<pair<int, int>>cs;
  for (int i = 0; i < N; ++i) {
          int x, r; cin >> x >> r;
          int l = x - r; int r = x + r;
          cs.emplace_back({ l,r });
  }
  sort(cs.begin(), cs.end(), [](const pair<int, int>l, const int pair<int,
  int>r) { return l.first == r.first ? l.second < r.second : l.first < r.first;
  })); return 0;*/

  /*int H, W; cin >> W >> H;
  dp.resize(H+1);
  vector<int>s(W,0);
  s[0] = Mod(1);
  dp[0][s] = Mod(1);
  for (int i = 1; i <= H; ++i) {
          for (auto it = dp[i - 1].begin(); it != dp[i - 1].end(); ++it) {
                  vector<int>pre((*it).first);
                  for (int j = 0; j < (1 <<( W)); ++j) {
                          bitset<6>bs(j);
                          vector<int>now(W);
                          int num = 2;
                          for (int k = 0; k < W; ++k) {
                                  if (bs[k]) {
                                          now[k] = num;
                                          num++;
                                  }
                          }
                          for (int k = 0; k < W; ++k) {
                                  if (now[k] && pre[k]) {
                                          now[k] = pre[k];
                                  }
                          }
                          for (int k = 0; k < W - 1; ++k) {
                                  if (now[k] && now[k + 1]) {
                                          int amin = min(now[k], now[k + 1]);
                                          now[k] = amin;
                                          now[k + 1] = amin;
                                  }
                          }
                          for (int k = W - 1; k > 0; --k) {
                                  if (now[k] && now[k - 1]) {
                                          int amin = min(now[k], now[k - 1]);
                                          now[k] = amin;
                                          now[k - 1] = amin;
                                  }
                          }
                          int nnum = 2;
                          for (int k = 0; k < W; ++k) {
                                  if (now[k]>=2) {
                                          int aa = now[k];
                                          for (int l = 0; l < W; ++l) {
                                                  if (now[l] == aa)now[l] =
  nnum;
                                          }
                                          nnum++;
                                  }
                          }
                          bool isok = false;
                          for (int k = 0; k < W; ++k) {
                                  if (now[k] == 1) {
                                          isok = true;
                                  }
                          }
                          if (isok) {

                                  dp[i][now] += (*it).second;
                          }

                  }
          }
  }
  Mod ans(0);
  for (auto it = dp[H].begin(); it != dp[H].end(); ++it) {
          if ((*it).first[W - 1] == 1) {
                  ans += (*it).second;
          }
  }
  cout << ans << endl;
  return 0;*/

  /*int k = 0;
  while (1) {

          k++;
          int a; cin >> a;
          if (a) {
                  cout << "Case " << k << ": " << a << endl;
          }
          else {
                  return 0;
          }
  }
  return 0;*/

  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<string> sts;
    for (int i = 0; i < N; ++i) {
      string st;
      cin >> st;
      sts.emplace_back(st);
    }
    int alphanum = 0;
    vector<bool> ex(26, false);
    vector<bool> start(26, false);
    bool ok = true;
    for (int i = 0; i < N; ++i) {
      if (sts[i].size() != 1)
        start[sts[i][0] - 'A'] = true;
      for (int j = 0; j < sts[i].size(); ++j) {
        if (ex[sts[i][j] - 'A']) {

        } else {
          alphanum++;
          if (alphanum > 10) {
            ok = false;
          }
          ex[sts[i][j] - 'A'] = true;
        }
      }
    }
    vector<long long int> kakeru(26);
    {
      for (int i = 0; i < N - 1; ++i) {
        int num = 1;
        for (int j = sts[i].size() - 1; j >= 0; --j) {
          kakeru[sts[i][j] - 'A'] += num;
          num *= 10;
        }
      }
      int num = 1;
      for (int j = sts[N - 1].size() - 1; j >= 0; --j) {
        kakeru[sts[N - 1][j] - 'A'] -= num;
        num *= 10;
      }
    }
    vector<int> compress;
    for (int i = 0; i < kakeru.size(); ++i) {
      if (ex[i])
        compress.push_back(kakeru[i]);
    }
    vector<int> perm(10);
    for (int i = 0; i < 10; ++i) {
      perm[i] = i;
    }

    int ansnum = 0;
    do {
      int k = 0;
      bool isok = true;
      for (int i = 0; i < 26; ++i) {
        if (ex[i]) {
          if (perm[k] == 0) {
            if (start[i]) {
              isok = false;
              break;
            }
          }
          k++;
        }
      }
      if (!isok)
        continue;
      long long int ans = 0;
      for (int i = 0; i < alphanum; ++i) {
        ans += perm[i] * compress[i];
      }

      if (!ans) {
        ansnum++;
      }
    } while (next_permutation(perm.begin(), perm.end()));
    for (int j = 10 - alphanum; j != 0; --j) {
      ansnum /= j;
    }
    cout << ansnum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

const int mod = 1000000007;

struct Mod {
  int num;
  Mod() : num(0) { ; }
  Mod(long long int n) : num((n % mod + mod) % mod) { ; }
  Mod(int n) : num((n % mod + mod) % mod) { ; }
  operator int() { return num; }
};

Mod operator+(Mod a, Mod b) { return Mod((a.num + b.num) % mod); }
Mod operator+(long long int a, Mod b) { return Mod(a) + b; }
Mod operator-(Mod a, Mod b) { return Mod((mod + a.num - b.num) % mod); }
Mod operator-(long long int a, Mod b) { return Mod(a) - b; }
Mod operator*(Mod a, Mod b) { return Mod(((long long)a.num * b.num) % mod); }
Mod operator*(long long int a, Mod b) { return Mod(a) * b; }
Mod operator+=(Mod &a, Mod b) { return a = a + b; }
Mod operator+=(long long int &a, Mod b) { return a = a + b; }
Mod operator-=(Mod &a, Mod b) { return a = a - b; }
Mod operator-=(long long int &a, Mod b) { return a = a - b; }
Mod operator*=(Mod &a, Mod b) { return a = a * b; }
Mod operator*=(long long int &a, Mod b) { return a = a * b; }
Mod operator^(Mod a, int n) {
  if (n == 0)
    return Mod(1);
  Mod res = (a * a) ^ (n / 2);
  if (n % 2)
    res = res * a;
  return res;
}
Mod inv(Mod a) { return a ^ (mod - 2); }
Mod operator/(Mod a, Mod b) {
  assert(b != 0);
  return a * inv(b);
}
Mod operator/(long long int a, Mod b) {
  assert(b != 0);
  return Mod(a) * inv(b);
}

#define MAX_N 1024000

Mod fact[MAX_N], factinv[MAX_N];
void init() {
  fact[0] = Mod(1);
  factinv[0] = 1;
  for (int i = 0; i < MAX_N - 1; ++i) {
    fact[i + 1] = fact[i] * Mod(i + 1);
    factinv[i + 1] = factinv[i] / Mod(i + 1);
  }
}
Mod comb(int a, int b) { return fact[a] * factinv[b] * factinv[a - b]; }

vector<map<vector<int>, Mod>> dp;

vector<vector<Mod>> keisann(const vector<vector<Mod>> l,
                            const vector<vector<Mod>> r) {
  vector<vector<Mod>> ans(l.size(), vector<Mod>(r[0].size()));
  assert(l[0].size() == r.size());
  for (int h = 0; h < l.size(); ++h) {
    for (int i = 0; i < r.size(); ++i) {
      for (int w = 0; w < r[0].size(); ++w) {

        ans[h][w] += l[h][i] * r[i][w];
      }
    }
  }
  return ans;
}

vector<vector<Mod>> powgyou(vector<vector<Mod>> a, const long long int n) {
  if (n == 1)
    return a;
  else {
    vector<vector<Mod>> ans(a.size(), vector<Mod>(a[0].size(), 0));
    ans = powgyou(a, n / 2);
    ans = keisann(ans, ans);
    if (n % 2) {
      ans = keisann(ans, a);
    }
    return ans;
  }
}

/*int N; cin >> N;
vector<pair<int, int>>cs;
for (int i = 0; i < N; ++i) {
        int x, r; cin >> x >> r;
        int l = x - r; int r = x + r;
        cs.emplace_back({ l,r });
}
sort(cs.begin(), cs.end(), [](const pair<int, int>l, const int pair<int, int>r)
{ return l.first == r.first ? l.second < r.second : l.first < r.first; }));
return 0;*/

/*int H, W; cin >> W >> H;
dp.resize(H+1);
vector<int>s(W,0);
s[0] = Mod(1);
dp[0][s] = Mod(1);
for (int i = 1; i <= H; ++i) {
        for (auto it = dp[i - 1].begin(); it != dp[i - 1].end(); ++it) {
                vector<int>pre((*it).first);
                for (int j = 0; j < (1 <<( W)); ++j) {
                        bitset<6>bs(j);
                        vector<int>now(W);
                        int num = 2;
                        for (int k = 0; k < W; ++k) {
                                if (bs[k]) {
                                        now[k] = num;
                                        num++;
                                }
                        }
                        for (int k = 0; k < W; ++k) {
                                if (now[k] && pre[k]) {
                                        now[k] = pre[k];
                                }
                        }
                        for (int k = 0; k < W - 1; ++k) {
                                if (now[k] && now[k + 1]) {
                                        int amin = min(now[k], now[k + 1]);
                                        now[k] = amin;
                                        now[k + 1] = amin;
                                }
                        }
                        for (int k = W - 1; k > 0; --k) {
                                if (now[k] && now[k - 1]) {
                                        int amin = min(now[k], now[k - 1]);
                                        now[k] = amin;
                                        now[k - 1] = amin;
                                }
                        }
                        int nnum = 2;
                        for (int k = 0; k < W; ++k) {
                                if (now[k]>=2) {
                                        int aa = now[k];
                                        for (int l = 0; l < W; ++l) {
                                                if (now[l] == aa)now[l] = nnum;
                                        }
                                        nnum++;
                                }
                        }
                        bool isok = false;
                        for (int k = 0; k < W; ++k) {
                                if (now[k] == 1) {
                                        isok = true;
                                }
                        }
                        if (isok) {

                                dp[i][now] += (*it).second;
                        }

                }
        }
}
Mod ans(0);
for (auto it = dp[H].begin(); it != dp[H].end(); ++it) {
        if ((*it).first[W - 1] == 1) {
                ans += (*it).second;
        }
}
cout << ans << endl;
return 0;*/

int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

int dx8[8] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first1, BidirectionalIterator last1,
                      BidirectionalIterator first2,
                      BidirectionalIterator last2) {
  if ((first1 == last1) || (first2 == last2)) {
    return false;
  }
  BidirectionalIterator m1 = last1;
  BidirectionalIterator m2 = last2;
  --m2;
  while (--m1 != first1 && !(*m1 < *m2)) {
  }
  bool result = (m1 == first1) && !(*first1 < *m2);
  if (!result) {
    // ?
    while (first2 != m2 && !(*m1 < *first2)) {
      ++first2;
    }
    first1 = m1;
    std::iter_swap(first1, first2);
    ++first1;
    ++first2;
  }
  if ((first1 != last1) && (first2 != last2)) {
    // ?
    m1 = last1;
    m2 = first2;
    while ((m1 != first1) && (m2 != last2)) {
      std::iter_swap(--m1, m2);
      ++m2;
    }
    // ?
    std::reverse(first1, m1);
    std::reverse(first1, last1);
    std::reverse(m2, last2);
    std::reverse(first2, last2);
  }
  return !result;
}

template <class BidirectionalIterator>
bool next_combination(BidirectionalIterator first, BidirectionalIterator middle,
                      BidirectionalIterator last) {
  return next_combination(first, middle, middle, last);
}

int main(void) {
  /*int N; cin >> N;
  vector<pair<int, int>>cs;
  for (int i = 0; i < N; ++i) {
          int x, r; cin >> x >> r;
          int l = x - r; int r = x + r;
          cs.emplace_back({ l,r });
  }
  sort(cs.begin(), cs.end(), [](const pair<int, int>l, const int pair<int,
  int>r) { return l.first == r.first ? l.second < r.second : l.first < r.first;
  })); return 0;*/

  /*int H, W; cin >> W >> H;
  dp.resize(H+1);
  vector<int>s(W,0);
  s[0] = Mod(1);
  dp[0][s] = Mod(1);
  for (int i = 1; i <= H; ++i) {
          for (auto it = dp[i - 1].begin(); it != dp[i - 1].end(); ++it) {
                  vector<int>pre((*it).first);
                  for (int j = 0; j < (1 <<( W)); ++j) {
                          bitset<6>bs(j);
                          vector<int>now(W);
                          int num = 2;
                          for (int k = 0; k < W; ++k) {
                                  if (bs[k]) {
                                          now[k] = num;
                                          num++;
                                  }
                          }
                          for (int k = 0; k < W; ++k) {
                                  if (now[k] && pre[k]) {
                                          now[k] = pre[k];
                                  }
                          }
                          for (int k = 0; k < W - 1; ++k) {
                                  if (now[k] && now[k + 1]) {
                                          int amin = min(now[k], now[k + 1]);
                                          now[k] = amin;
                                          now[k + 1] = amin;
                                  }
                          }
                          for (int k = W - 1; k > 0; --k) {
                                  if (now[k] && now[k - 1]) {
                                          int amin = min(now[k], now[k - 1]);
                                          now[k] = amin;
                                          now[k - 1] = amin;
                                  }
                          }
                          int nnum = 2;
                          for (int k = 0; k < W; ++k) {
                                  if (now[k]>=2) {
                                          int aa = now[k];
                                          for (int l = 0; l < W; ++l) {
                                                  if (now[l] == aa)now[l] =
  nnum;
                                          }
                                          nnum++;
                                  }
                          }
                          bool isok = false;
                          for (int k = 0; k < W; ++k) {
                                  if (now[k] == 1) {
                                          isok = true;
                                  }
                          }
                          if (isok) {

                                  dp[i][now] += (*it).second;
                          }

                  }
          }
  }
  Mod ans(0);
  for (auto it = dp[H].begin(); it != dp[H].end(); ++it) {
          if ((*it).first[W - 1] == 1) {
                  ans += (*it).second;
          }
  }
  cout << ans << endl;
  return 0;*/

  /*int k = 0;
  while (1) {

          k++;
          int a; cin >> a;
          if (a) {
                  cout << "Case " << k << ": " << a << endl;
          }
          else {
                  return 0;
          }
  }
  return 0;*/

  while (1) {
    int N;
    cin >> N;
    if (!N)
      break;
    vector<string> sts;
    for (int i = 0; i < N; ++i) {
      string st;
      cin >> st;
      sts.emplace_back(st);
    }
    int alphanum = 0;
    vector<bool> ex(26, false);
    vector<bool> start(26, false);
    bool ok = true;
    for (int i = 0; i < N; ++i) {
      if (sts[i].size() != 1)
        start[sts[i][0] - 'A'] = true;
      for (int j = 0; j < sts[i].size(); ++j) {
        if (ex[sts[i][j] - 'A']) {

        } else {
          alphanum++;
          if (alphanum > 10) {
            ok = false;
          }
          ex[sts[i][j] - 'A'] = true;
        }
      }
    }
    vector<int> kakeru(26);
    {
      for (int i = 0; i < N - 1; ++i) {
        int num = 1;
        for (int j = sts[i].size() - 1; j >= 0; --j) {
          kakeru[sts[i][j] - 'A'] += num;
          num *= 10;
        }
      }
      int num = 1;
      for (int j = sts[N - 1].size() - 1; j >= 0; --j) {
        kakeru[sts[N - 1][j] - 'A'] -= num;
        num *= 10;
      }
    }
    vector<int> compress;
    for (int i = 0; i < kakeru.size(); ++i) {
      if (kakeru[i])
        compress.push_back(kakeru[i]);
    }
    vector<int> perm(10);
    for (int i = 0; i < 10; ++i) {
      perm[i] = i;
    }

    int ansnum = 0;
    do {
      map<char, int> mp;
      int k = 0;
      bool isok = true;
      vector<int> to(26);
      for (int i = 0; i < 26; ++i) {
        if (ex[i]) {
          if (perm[k] == 0) {
            if (start[i]) {
              isok = false;
              break;
            }
          }
          to[i] = perm[k];
          k++;
        }
      }
      if (!isok)
        continue;
      long long int ans = 0;
      for (int i = 0; i < alphanum; ++i) {
        ans += perm[i] * compress[i];
      }

      if (!ans) {
        ansnum++;
      }
    } while (next_permutation(perm.begin(), perm.end()));
    for (int j = 10 - alphanum; j != 0; --j) {
      ansnum /= j;
    }
    cout << ansnum << endl;
  }
  return 0;
}