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


// Problem : C - Ubiquity
// Contest : AtCoder - AtCoder Beginner Contest 178
// URL : https://atcoder.jp/contests/abc178/tasks/abc178_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// $%U%$
// Mere bas ka nahi hai
// still i'm trying
// apna time aayega
//
//        .--------------.
//        | Try First One|
//        '--------------'
//                |     .--------------.
//                |     |              |
//                V     V              |
//              .--------------.       |
//              |      AC.     |<---.  |
//              '--------------'    |  |
//              (True)|  |(False)   |  |
//           .--------'  |          |  |
//           |           V          |  |
//           |  .--------------.    |  |
//           |  |   Try Again  |----'  |
//           |  '--------------'       |
//           |                         |
//           |  .--------------.       |
//           '->| Try Next One |-------'
//              '--------------'
//
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll power(ll x, ll y, ll m) {
  if (y <= 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
vector<ll> fac((int)1e6 + 5);
ll nCr(ll n, ll r, ll m) {
  if (r > n)
    return 0;
  ll a = 1, b = 1, i;
  return ((fac[n] % m) * power((fac[r] * fac[n - r]) % m, m - 2, m)) % m;
}
#define Test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define endl '\\n'
const ll mod = (ll)1e9 + 7;
void solve() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return;
  }
  ll ans = 0;
  vector<ll> po2, po8;
  ll temp2 = 1;
  ll temp8 = 1;
  for (ll i = 0; i <= (ll)1e6; ++i) {
    po2.push_back(temp2);
    po8.push_back(temp8);
    temp2 = (temp2 * 2) % mod;
    temp8 = (temp8 * 8) % mod;
  }
  for (ll i = 2; i <= min(n, n); ++i) {
    ans += (((nCr(n, i, mod) * po8[n - i]) % mod) * (po2[i] - 2 + mod) % mod) %
           mod;
    ans %= mod;
  }
  cout << ans << endl;
}
int main() {
  fac[0] = 1;
  for (int i = 1; i <= (int)1e6; ++i) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
  }
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // Test
  solve();
  return 0;
}


Error: TLE

Fixed submission:


// Problem : C - Ubiquity
// Contest : AtCoder - AtCoder Beginner Contest 178
// URL : https://atcoder.jp/contests/abc178/tasks/abc178_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

// $%U%$
// Mere bas ka nahi hai
// still i'm trying
// apna time aayega
//
//        .--------------.
//        | Try First One|
//        '--------------'
//                |     .--------------.
//                |     |              |
//                V     V              |
//              .--------------.       |
//              |      AC.     |<---.  |
//              '--------------'    |  |
//              (True)|  |(False)   |  |
//           .--------'  |          |  |
//           |           V          |  |
//           |  .--------------.    |  |
//           |  |   Try Again  |----'  |
//           |  '--------------'       |
//           |                         |
//           |  .--------------.       |
//           '->| Try Next One |-------'
//              '--------------'
//
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll power(ll x, ll y, ll m) {
  if (y <= 0)
    return 1;
  ll p = power(x, y / 2, m) % m;
  p = (p * p) % m;
  return (y % 2 == 0) ? p : (x * p) % m;
}
vector<ll> fac((int)1e6 + 5);
ll nCr(ll n, ll r, ll m) {
  if (r > n)
    return 0;
  ll a = 1, b = 1, i;
  return ((fac[n] % m) * power((fac[r] * fac[n - r]) % m, m - 2, m)) % m;
}
#define Test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define endl '\\n'
const ll mod = (ll)1e9 + 7;
void solve() {
  ll n;
  cin >> n;
  if (n == 1) {
    cout << 0 << endl;
    return;
  }
  ll ans = 0;
  for (ll i = 2; i <= min(n, n); ++i) {
    ans += (((nCr(n, i, mod) * power(8, n - i, mod)) % mod) *
            (power(2, i, mod) - 2 + mod) % mod) %
           mod;
    // cout << nCr(n,i,mod)  << n << " " << i << endl;
    ans %= mod;
  }
  cout << ans << endl;
}
int main() {
  fac[0] = 1;
  for (int i = 1; i <= (int)1e6; ++i) {
    fac[i] = fac[i - 1] * i;
    fac[i] %= mod;
  }
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // Test
  solve();
  return 0;
}