#35644: 還是TLE


samlin961112@gmail.com (林哲甫)

School : 新北市私立南山高級中學
ID : 220506
IP address : [219.70.213.92]
Last Login :
2024-05-30 22:39:10
f255. 肥貓的保險箱密碼 -- wseds | From: [219.70.213.92] | Post Date : 2023-06-10 21:37

#include <iostream>
#include <vector>
using namespace std;
vector<int> multiplyArrayByInt(const vector<int> &num) {
  int n = num.size();
  vector<int> product(n, 0);
  int carry = 0;
  for (int i = n - 1; i >= 0; i--) {
    int digit = num[i] * 2 + carry;
    carry = digit / 10;
    product[i] = digit%10;
  }
  if (carry > 0) {
    product.insert(product.begin(), carry);
  }
  return product;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  vector<vector<int>> m(10001);
  m[0] = {1};

  for (int i = 1; i <= 10000; i++) {
    m[i] = multiplyArrayByInt(m[i - 1]);
  }
  while (cin >> n) {
    if (n == 0) {
      break;
    } else {
      for (int digit : m[n]) {
        cout << digit;
      }
      cout << endl;
    }
  }

  return 0;
}

 
#35901: Re: 還是TLE


cges30901 (cges30901)

School : No School
ID : 30877
IP address : [27.242.9.59]
Last Login :
2024-09-03 08:48:54
f255. 肥貓的保險箱密碼 -- wseds | From: [39.15.2.77] | Post Date : 2023-06-23 13:57

#include
#include
using namespace std;
vector multiplyArrayByInt(const vector &num) {
  int n = num.size();
  vector product(n, 0);
  int carry = 0;
  for (int i = n - 1; i >= 0; i--) {
    int digit = num[i] * 2 + carry;
    carry = digit / 10;
    product[i] = digit%10;
  }
  if (carry > 0) {
    product.insert(product.begin(), carry);
  }
  return product;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  vector> m(10001);
  m[0] = {1};

  for (int i = 1; i <= 10000; i++) {
    m[i] = multiplyArrayByInt(m[i - 1]);
  }
  while (cin >> n) {
    if (n == 0) {
      break;
    } else {
      for (int digit : m[n]) {
        cout << digit;
      }
      cout << endl;
    }
  }

  return 0;
}

  1. 改用字串建表
  2. endl改成'\n'
 
ZeroJudge Forum