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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
    return a.first < b.first;
}*/
// use case
// sort(V.begin(), V.end(), compare_function);

/*struct compare
{
    bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second-a.first==b.second-b.first)
    {
        return a.first>b.first;
    }
    return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/

/*void print1(vector <int> X)
{
    for (int i=0; i<X.size(); i++)
    {
        cout<<X[i]<<" ";
    }
    cout<<endl;
    return;
}

void print2(vector <vector <int>> X)
{
    for (int i=0; i<X.size(); i++)
    {
    for (int j=0;j<X[i].size();j++) {
        cout<<X[i][j]<<" ";}
    cout<<endl;}
    return;
}*/

ll laip(ll a, ll b, ll liek) {
  a = a % liek;
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a;
  }
  ll c = (a * a) % liek;
  ll x = laip(c, b / 2, liek);
  if (b % 2 == 0) {
    return x;
  } else {
    return (a * x) % liek;
  }
}

ll cnk(int i, int s, vector<ll> &F) {
  ll li = 1000000005;
  ll liek = li + 2;
  ll a = F[i];
  ll bb = laip(F[s], li, liek);
  ll cc = laip(F[i - s], li, liek);
  ll x = a * bb;
  x = x % liek;
  x = x * cc;
  x = x % liek;
  return x;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int k;
  string s;
  cin >> k >> s;
  int l = s.length();
  int n = k + l;
  ll liek = 1000000007;
  vector<ll> F(n + 1, 1);
  vector<ll> L1(n + 1, 1);
  vector<ll> L2(n + 1, 1);
  L1[1] = 25;
  L2[1] = 26;
  for (int i = 2; i < n + 1; i++) {
    F[i] = (F[i - 1] * i) % liek;
    L1[i] = (L1[i - 1] * 25) % liek;
    L2[i] = (L2[i - 1] * 26) % liek;
  }
  ll ats = 0;
  for (int i = l - 1; i < n; i++) {
    ll x = cnk(i, l - 1, F);
    ll y = L1[i + 1 - l] * L2[n - 1 - i];
    y = y % liek;
    // cout<<i<<" "<<x*y<<endl;
    ats += (x * y) % liek;
    ats = ats % liek;
  }
  cout << ats << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

using namespace std;
typedef long long int ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

/*bool compare_function(const pair<int, int> a, const pair<int, int> b)
{
    return a.first < b.first;
}*/
// use case
// sort(V.begin(), V.end(), compare_function);

/*struct compare
{
    bool operator ()(const pair<int, int> &a, const pair<int, int> &b) {
    if (a.second-a.first==b.second-b.first)
    {
        return a.first>b.first;
    }
    return a.second-a.first < b.second-b.first;}
};
priority_queue <pair<int,int>, vector <pair<int,int>>, compare> Q;*/

/*void print1(vector <int> X)
{
    for (int i=0; i<X.size(); i++)
    {
        cout<<X[i]<<" ";
    }
    cout<<endl;
    return;
}

void print2(vector <vector <int>> X)
{
    for (int i=0; i<X.size(); i++)
    {
    for (int j=0;j<X[i].size();j++) {
        cout<<X[i][j]<<" ";}
    cout<<endl;}
    return;
}*/

ll laip(ll a, ll b, ll liek) {
  a = a % liek;
  if (b == 0) {
    return 1;
  }
  if (b == 1) {
    return a;
  }
  ll c = (a * a) % liek;
  ll x = laip(c, b / 2, liek);
  if (b % 2 == 0) {
    return x;
  } else {
    return (a * x) % liek;
  }
}

ll cnk(int i, int s, vector<ll> &F) {
  ll li = 1000000006 - 1;
  ll liek = li + 2;
  ll a = F[i];
  ll b = F[s];
  ll c = F[i - s];
  ll bb = laip(b, li, liek);
  ll cc = laip(c, li, liek);
  ll x = a * bb;
  x = x % liek;
  x = x * cc;
  x = x % liek;
  return x;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int k;
  string s;
  cin >> k >> s;
  int l = s.length();
  int n = k + l;
  map<pii, ll> M;
  ll liek = 1000000007;
  vector<ll> F(n + 1, 1);
  for (int i = 2; i < n + 1; i++) {
    F[i] = (F[i - 1] * i) % liek;
    // cout<<i<<" "<<F[i]<<endl;
  }
  ll ats = 0;
  for (int i = l - 1; i < n; i++) {
    ll x = cnk(i, l - 1, F);
    ll y = laip(25, i + 1 - l, liek) * laip(26, n - i - 1, liek);
    y = y % liek;
    // cout<<i<<" "<<x*y<<endl;
    ats += (x * y) % liek;
    ats = ats % liek;
  }
  cout << ats << endl;
  return 0;
}