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

#include <bits/stdc++.h>
#define start                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define LL long long
#define int ll // 方便
#define ls st << 1
#define rs st << 1 | 1
#define pii pair<int, int>
using namespace std;
const int maxn = (ll)1e5 + 5;
const int mod = 1000000007;
const int inf = 0x3f3f3f3f;
signed main() {
  start;
  int n;
  while (cin >> n && n) {
    int a = 0;
    for (int i = 0; i < 52; ++i) { // 一共输入后面的52位
      a <<= 1;
      char c;
      cin >> c;
      if (c == '1')
        a++;
    }
    a += 1ll << 52;     // 该方法计数，小数点前数字为1
    int e = 0, ans = a; // e表示指数部分，ans表示小数部分
    /*模拟加法*/
    while (n) {
      int t =
          ((1ll << 53) - 1 - ans) / a +
          1; // 前半部分代表刚好保证不进位需要加多少次，加1表示刚好进位需要加多少次
      if (t > n) { // 如果加上n次仍然不进位，则数字可加上n次的a
        ans += n * a;
        break;
      } else { // 将当前数字加上t次原数字
        ans += t * a;
        ++e;       // 进位，指数加一
        ans >>= 1; // 由于指数加一，二进制表示的小数需要右移一位
        a >>= 1; // 由于指数加一，原小数的最后一位对答案没有贡献，丢弃
        if (!a) // 如果原小数对答案已经没有贡献，则跳出
          break;
        n -= t; // 加了t次
      }
    }
    for (int i = 11; i >= 0; --i) // 输出12位指数
      cout << (((1ll << i) & e) ? 1 : 0);
    for (int i = 51; i >= 0; --i) // 输出52位小数
      cout << (((1ll << i) & ans) ? 1 : 0);
    cout << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

// UVa_11809
#include <cmath>
#include <iostream>
#include <sstream>
#include <string>

const int MAXN = 30 + 10;
const double EPS = 1e-3;
double mantissa_base10[MAXN][MAXN];
long long exponent_base10[MAXN][MAXN];

int main() {
#ifdef EagleBear
  freopen("EagleBear.in", "r", stdin);
  freopen("EagleBear.out", "w", stdout);
#endif
  for (int i = 0; i <= 9; ++i)
    for (int j = 1; j <= 30; ++j) {
      double mantissa_base2 = 1 - std::pow(2, -i - 1);
      long long exponent_base2 = std::pow(2, j) - 1;
      double idx_base10 =
          std::log10(mantissa_base2) + exponent_base2 * std::log10(2);

      exponent_base10[i][j] = idx_base10;
      mantissa_base10[i][j] = std::pow(10, idx_base10 - exponent_base10[i][j]);

      //			std::cout<<"i="<<i<<" j="<<j<<std::endl;
      //			std::cout<<"mantissa_base2="<<mantissa_base2<<std::endl;
      //			std::cout<<"exponent_base2="<<exponent_base2<<std::endl;
      //			std::cout<<"idx_base10="<<idx_base10<<std::endl;
      //			std::cout<<mantissa_base10[i][j]<<"
      //"<<exponent_base10[i][j]<<std::endl;
    }

  std::string s;
  while (std::cin >> s) {
    if (s == "0e0")
      break;
    for (std::string::iterator it = s.begin(); it != s.end(); ++it)
      if (*it == 'e')
        *it = ' ';
    std::istringstream sin(s);

    double mantissa = 0;
    long long exponent = 0;
    sin >> mantissa >> exponent;
    while (mantissa < 1) {
      mantissa *= 10;
      exponent--;
    }
    //		std::cout<<mantissa<<" "<<exponent<<std::endl;
    for (int i = 0; i <= 9; ++i)
      for (int j = 1; j <= 30; ++j)
        if ((std::abs(mantissa_base10[i][j] - mantissa) <= EPS ||
             std::abs(mantissa_base10[i][j] - mantissa / 10) <= EPS) &&
            exponent_base10[i][j] == exponent) {
          std::cout << i << " " << j << std::endl;
          break;
        }
  }
  return 0;
}