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

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define ar array
#define pb push_back
#define fi(a, b) for (ll i = a; i < (b); i++)
#define fj(a, b) for (ll j = a; j < (b); j++)
#define fk(a, b) for (ll k = a; k < (b); k++)
// hash base 177013
const int mod = 1e9 + 7;
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = ((res % mod) * (a % mod)) % mod;
    }
    a = ((a % mod) * (a % mod)) % mod;
    b = b >> 1;
  }
  return res % mod;
}
const int mxn = 1e6 + 3;
vector<ll> fact(mxn), inv(mxn);
void facto() {
  fact[0] = fact[1] = 1;
  fi(2, mxn) {
    fact[i] = (fact[i - 1] * i) % mod;
    // assert(fact[i]>=0&&fact[i]<=mod);
  }
  inv[0] = inv[1] = 1;
  fi(2, mxn) {
    inv[i] = binpow(fact[i], mod - 2) % mod;
    // assert(inv[i]>=0&&inv[i]<=mod);
  }
}

ll ncr(ll a, ll b) {
  // assert(a-b>=0);
  return (((fact[a] * inv[b]) % mod) * inv[a - b] % mod) % mod;
}

int main() {
  FAST;
  facto();
  // #ifndef ONLINE_JUDGE
  //	freopen("input.txt","r",stdin);
  //	freopen("output.txt","w",stdout);
  // #endif
  // ll testcase;
  // cin>>testcase;
  // while(testcase--)
  {
    ll n;
    cin >> n;
    ll ans = 1, ans2 = 1, ans3 = 1;
    fi(0, n) {
      ans = (ans * 9) % mod;
      ans2 = (ans2 * 8) % mod;
      ans3 = (ans3 * 10) % mod;
    }
    // cout<<ans2<<'\\n';
    ll out = (ans3 + ans2) % mod - (2 * ans) % mod;
    if (out < 0) {
      out += mod;
    }
    cout << out % mod << '\\n';
    // map<int,int> m;
    // fi(1,n+1)
    //{
    // ll pos_zero=ncr(n,i);
    // fj(1,n+1)
    //{
    // if(i+j>n) break;
    // ll pos_one=ncr(n-i,j);
    // ll rem=n-i-j;
    // ans=(ans+(((binpow(8,rem)*pos_zero)%mod)*pos_one%mod)%mod)%mod;
    //}
    //}
    // cout<<ans<<'\\n';
  }
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("sse4")
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>
#define ll long long int
#define ar array
#define pb push_back
#define fi(a, b) for (ll i = a; i < (b); i++)
#define fj(a, b) for (ll j = a; j < (b); j++)
#define fk(a, b) for (ll k = a; k < (b); k++)
// hash base 177013
const int mod = 1e9 + 7;
ll binpow(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1) {
      res = ((res % mod) * (a % mod)) % mod;
    }
    a = ((a % mod) * (a % mod)) % mod;
    b = b >> 1;
  }
  return res % mod;
}
const int mxn = 1e6 + 3;
vector<ll> fact(mxn), inv(mxn);
void facto() {
  fact[0] = fact[1] = 1;
  fi(2, mxn) {
    fact[i] = (fact[i - 1] * i) % mod;
    // assert(fact[i]>=0&&fact[i]<=mod);
  }
  inv[0] = inv[1] = 1;
  fi(2, mxn) {
    inv[i] = binpow(fact[i], mod - 2) % mod;
    // assert(inv[i]>=0&&inv[i]<=mod);
  }
}

ll ncr(ll a, ll b) {
  // assert(a-b>=0);
  return (((fact[a] * inv[b]) % mod) * inv[a - b] % mod) % mod;
}

int main() {
  FAST;
  facto();
  // #ifndef ONLINE_JUDGE
  //	freopen("input.txt","r",stdin);
  //	freopen("output.txt","w",stdout);
  // #endif
  // ll testcase;
  // cin>>testcase;
  // while(testcase--)
  {
    ll n;
    cin >> n;
    ll ans = 0;
    // map<int,int> m;
    fi(1, n + 1) {
      ll pos_zero = ncr(n, i);
      fj(1, n + 1) {
        if (i + j > n)
          break;
        ll pos_one = ncr(n - i, j);
        // m[j]++;
        ll rem = n - i - j;
        assert(rem >= 0);
        ans = (ans +
               (((binpow(8, rem) * pos_zero) % mod) * pos_one % mod) % mod) %
              mod;
      }
    }
    cout << ans << '\\n';
    // for(auto i:m)
    //{
    // cout<<i.first<<' '<<i.second<<'\\n';
    //}
  }
}