Advertisement
Dmaxiya

R 格式 参考代码

Mar 20th, 2025
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long LL;
  5. const int maxn = 2000 + 100;
  6. struct BigInteger {
  7.     int length;
  8.     int num[maxn];
  9.  
  10.     void clear() {
  11.         length = 0;
  12.         memset(num, 0, sizeof(num));
  13.     }
  14.  
  15.     BigInteger() {
  16.         clear();
  17.         length = 1;
  18.     }
  19.  
  20.     BigInteger(int x) {
  21.         clear();
  22.         if (x == 0) {
  23.             length = 1;
  24.             return ;
  25.         }
  26.         while (x != 0) {
  27.             num[length++] = x % 10;
  28.             x /= 10;
  29.         }
  30.     }
  31.  
  32.     BigInteger(const string &str) {
  33.         clear();
  34.         for (int i = str.length() - 1; i >= 0; --i) {
  35.             if (str[i] == '.') {
  36.                 continue;
  37.             }
  38.             num[length++] = str[i] - '0';
  39.         }
  40.         fixLength();
  41.     }
  42.  
  43.     void fixLength() {
  44.         for (int i = maxn - 1; i >= 0; --i) {
  45.             if (num[i] != 0) {
  46.                 length = i + 1;
  47.                 return ;
  48.             }
  49.         }
  50.         length = 1;
  51.     }
  52.  
  53.     void fix() {
  54.         int s = 0;
  55.         for (int i = 0; i < maxn; ++i) {
  56.             num[i] += s;
  57.             s = num[i] / 10;
  58.             num[i] %= 10;
  59.         }
  60.         fixLength();
  61.     }
  62. };
  63.  
  64. BigInteger operator+(const BigInteger &a, const BigInteger &b) {
  65.     BigInteger c;
  66.     int len = max(a.length, b.length);
  67.     for (int i = 0; i < len; ++i) {
  68.         c.num[i] = a.num[i] + b.num[i];
  69.     }
  70.     c.fix();
  71.     return c;
  72. }
  73.  
  74. BigInteger operator*(const BigInteger &a, const BigInteger &b) {
  75.     BigInteger c;
  76.     for (int i = 0; i < a.length; ++i) {
  77.         for (int j = 0; j < b.length; ++j) {
  78.             c.num[i + j] += a.num[i] * b.num[j];
  79.         }
  80.     }
  81.     c.fix();
  82.     return c;
  83. }
  84.  
  85. ostream& operator<<(ostream &out, const BigInteger &x) {
  86.     for (int i = x.length - 1; i >= 0; --i) {
  87.         out << x.num[i];
  88.     }
  89.     return out;
  90. }
  91.  
  92. BigInteger fastPow(BigInteger res, int n) {
  93.     BigInteger ans;
  94.     for (ans = 1; n != 0; n >>= 1) {
  95.         if ((n & 1) == 1) {
  96.             ans = ans * res;
  97.         }
  98.         res = res * res;
  99.     }
  100.     return ans;
  101. }
  102.  
  103. int n, dig;
  104. string d;
  105.  
  106. int main() {
  107. #ifdef ExRoc
  108.     freopen("test.txt", "r", stdin);
  109. #endif
  110.     ios::sync_with_stdio(false);
  111.  
  112.     cin >> n >> d;
  113.     for (int i = d.length() - 1; i >= 0; --i) {
  114.         if (d[i] == '.') {
  115.             dig = d.length() - i - 1;
  116.             break;
  117.         }
  118.     }
  119.     BigInteger ans = fastPow(2, n) * BigInteger(d);
  120.     if (ans.num[dig - 1] >= 5) {
  121.         ans = ans + fastPow(10, dig);
  122.     }
  123.     for (int i = ans.length - 1; i >= dig; --i) {
  124.         cout << ans.num[i];
  125.     }
  126.     cout << endl;
  127.  
  128.     return 0;
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement