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

#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#define ull unsigned long long
#define int long long
#define ll long long
#define fr(i, n) for (int i = 0; i < n; i++)
#define frf(i, j, n) for (int i = j; i <= n; i++)
#define frd(i, n) for (int i = n; i >= 0; i--)
#define mp(i, j) make_pair(i, j)
#define pb(x) push_back(x)
using namespace std;
// const int MAX = 1e6 + 1;
// typedef vector<vector<ll>> matrix;
const ll PRIME = 1e9 + 7;
// typedef pair<int,pair<int,int> >  ppi;
// const ll MAX = 1e6 + 1;

unsigned long long power(unsigned long long x, int y, int p) {
  unsigned long long res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
  // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

unsigned long long factorial(int n) {
  const unsigned int M = 1000000007;

  unsigned long long f = 1;
  for (int i = 1; i <= n; i++)
    f = (f * i) % M; // Now f never can
                     // exceed 10^9+7
  return f;
}

// Returns n^(-1) mod p
unsigned long long modInverse(unsigned long long n, int p) {
  return power(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
unsigned long long nCrModPFermat(unsigned long long n, int r, int p) {
  // Base case
  if (r == 0)
    return 1;

  // Fill factorial array so that we
  // can find all factorial of r, n
  // and n-r
  unsigned long long fac[n + 1];
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = (fac[i - 1] * i) % p;

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
void solve() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << 0;
    return;
  }
  int ans = power(10, n, PRIME);
  ans = (ans - (2 * power(9, n, PRIME) % PRIME) + PRIME) % PRIME;
  ans = (ans + power(8, n, PRIME)) % PRIME;
  cout << ans;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t;
  // int k = 1;
  // cin >> t;
  t = 1;
  while (t--) {
    // cout << "Case #" << k++ << ": ";
    solve();
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#define ull unsigned long long
#define int long long
#define ll long long
#define fr(i, n) for (int i = 0; i < n; i++)
#define frf(i, j, n) for (int i = j; i <= n; i++)
#define frd(i, n) for (int i = n; i >= 0; i--)
#define mp(i, j) make_pair(i, j)
#define pb(x) push_back(x)
using namespace std;
// const int MAX = 1e6 + 1;
// typedef vector<vector<ll>> matrix;
const ll PRIME = 1e9 + 7;
// typedef pair<int,pair<int,int> >  ppi;
// const ll MAX = 1e6 + 1;

unsigned long long power(unsigned long long x, int y, int p) {
  unsigned long long res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
  // equal to p

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1)
      res = (res * x) % p;

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

unsigned long long factorial(int n) {
  const unsigned int M = 1000000007;

  unsigned long long f = 1;
  for (int i = 1; i <= n; i++)
    f = (f * i) % M; // Now f never can
                     // exceed 10^9+7
  return f;
}

// Returns n^(-1) mod p
unsigned long long modInverse(unsigned long long n, int p) {
  return power(n, p - 2, p);
}

// Returns nCr % p using Fermat's little
// theorem.
unsigned long long nCrModPFermat(unsigned long long n, int r, int p) {
  // Base case
  if (r == 0)
    return 1;

  // Fill factorial array so that we
  // can find all factorial of r, n
  // and n-r
  unsigned long long fac[n + 1];
  fac[0] = 1;
  for (int i = 1; i <= n; i++)
    fac[i] = (fac[i - 1] * i) % p;

  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
void solve() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << 0;
    return;
  }
  int ans = 0;
  frf(i, 1, n - 1) {
    // int temp=nCrModPFermat(n,i,PRIME);
    int temp = (power(9, n - i, PRIME) - power(8, n - i, PRIME)) % PRIME;
    temp = (temp * nCrModPFermat(n, i, PRIME)) % PRIME;
    ans += temp;
  }
  cout << ans;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int t;
  // int k = 1;
  // cin >> t;
  t = 1;
  while (t--) {
    // cout << "Case #" << k++ << ": ";
    solve();
  }
}