Problem p01489 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<double> VEC;
typedef vector<VEC> MAT;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define MOD 1000000007

// AOJ2372
LL fib(int k) { // k番目のfibonacci数を求める
  LL a[2][2] = {{1, 1}, {1, 0}};
  LL t[2][2];
  LL b[2][2] = {{1, 0}, {0, 1}};
  if (k == 0)
    return 1;
  k--;
  while (k) {
    if (k & 1) {
      memset(t, 0, sizeof(t));
      REP(p, 2) REP(q, 2) REP(r, 2) t[p][q] += (a[p][r] * b[r][q]) % MOD;
      REP(p, 2) REP(q, 2) b[p][q] = (t[p][q] % MOD);
    }
    memset(t, 0, sizeof(t));
    REP(p, 2) REP(q, 2) REP(r, 2) t[p][q] += (a[p][r] * a[r][q]) % MOD;
    REP(p, 2) REP(q, 2) a[p][q] = (t[p][q] % MOD);
    k >>= 1;
  }
  return (b[0][0] + b[0][1]) % MOD;
}

int main() {
  LL u, l, f, g, s, k;
  cin >> k;
  u = 2000000000;
  l = 0;
  while (u - l > 1) {
    LL mid = (u + l) / 2;
    if (k <= mid * (mid + 1))
      u = mid;
    else
      l = mid;
  }
  if (k <= u * (u + 1))
    f = u;
  else
    f = l;
  g = k - f * (f - 1);
  if (k <= f * f)
    s = 2 * f;
  else {
    g -= f;
    s = 2 * f + 1;
  }
  if (g * 2 <= f + 1)
    g = 2 * g - 1;
  else {
    g -= (f + 1) / 2 + 1;
    g = f - 2 * g - (f % 2);
  }
  cout << (fib(g) * fib(s - g) % MOD) << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// conversion
//------------------------------------------
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef vector<LL> VLL;

// container util

//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())
#define MT(a, b, c) MP(a, MP(b, c))
#define T1 first
#define T2 second.first
#define T3 second.second

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

typedef pair<string, int> edge;
typedef pair<string, int> state;

int main() {
  int n, ne, s, g;
  string inf("{");
  while (cin >> n >> ne >> s >> g, n) {
    VS vs(n, inf);
    vector<vector<edge>> graph(n);
    REP(i, ne) {
      int _from, _to;
      string _str;
      cin >> _from >> _to >> _str;
      graph[_from].PB(MP(_str, _to));
    }
    // 無駄な辺の消去
    REP(i, n) {
      if (graph[i].size() == 0)
        continue;
      SORT(graph[i]);
      int tlen = graph[i][0].first.length();
      int flag = 1;
      while (flag) {
        flag = 0;
        for (vector<edge>::iterator it = graph[i].begin(); it != graph[i].end();
             it++) {
          if (it->first.substr(0, tlen) != graph[i][0].first) {
            flag = 1;
            graph[i].erase(it);
            break;
          }
        }
      }
    }
    // dijkstra
    priority_queue<state, vector<state>, greater<state>> q;
    q.push(MP("", s));
    while (!q.empty()) {
      state tmp = q.top();
      q.pop();
      string curs = tmp.first;
      int curp = tmp.second;
      if (curs.length() > 6 * n)
        break; // loop
      if (curp == g) {
        vs[g] = curs;
        break;
      }
      int tlen = min(curs.length(), vs[curp].length());
      if (curs.substr(0, tlen) > vs[curp].substr(0, tlen))
        continue;
      vs[curp] = min(curs, vs[curp]);
      REP(i, graph[curp].size())
      q.push(MP(tmp.first + graph[curp][i].first, graph[curp][i].second));
    }
    if (vs[g] == inf)
      cout << "NO" << endl;
    else
      cout << vs[g] << endl;
  }
  return 0;
}