Advertisement
Josif_tepe

Untitled

Feb 25th, 2024
672
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #include <fstream>
  4. #include <string>
  5. #include <set>
  6. #include <vector>
  7. #define MOD 1000000007
  8. #define ll long long
  9. using namespace std;
  10.  
  11. string add(string a, string b) {
  12.     if(a.size() < b.size()) {
  13.         swap(a, b);
  14.     }
  15.     int i = (int) a.size() - 1, j = (int) b.size() - 1;
  16.     string res = "";
  17.     int carry = 0;
  18.     while(j >= 0) {
  19.         int sum = (a[i] - '0') + (b[j] - '0') + carry;
  20.         if(sum > 9) {
  21.             sum -= 10;
  22.             carry = 1;
  23.         }
  24.         else {
  25.             carry = 0;
  26.         }
  27.         res += (sum + '0');
  28.         i--;
  29.         j--;
  30.     }
  31.     while(i >= 0) {
  32.         int sum = (a[i] - '0') + carry;
  33.         if(sum > 9) {
  34.             sum -= 10;
  35.             carry = 1;
  36.         }
  37.         else {
  38.             carry = 0;
  39.         }
  40.         res += (sum + '0');
  41.         i--;
  42.     }
  43.     if(carry == 1) {
  44.         res += "1";
  45.     }
  46.     reverse(res.begin(), res.end());
  47.     return res;
  48. }
  49. string multiply(string a, string b) {
  50.     int sz_a = (int) a.size();
  51.     int sz_b = (int) b.size();
  52.     vector<int> v(sz_a + sz_b, 0);
  53.    
  54.     for(int i = sz_a - 1; i >= 0; i--){
  55.         for(int j = sz_b - 1; j >= 0; j--) {
  56.             v[i + j + 1] += (a[i] - '0') * (b[j] - '0');
  57.         }
  58.     }
  59.     for(int i = sz_a + sz_b - 1; i >= 0; i--) {
  60.         if(v[i] > 9) {
  61.             v[i - 1] += v[i] / 10;
  62.             v[i] %= 10;
  63.         }
  64.     }
  65.     int i = 0;
  66.     while(v[i] == 0) {
  67.         i++;
  68.     }
  69.     string res = "";
  70.     while(i < sz_a + sz_b) {
  71.         res += (v[i] + '0');
  72.         i++;
  73.     }
  74.     if(res == "") {
  75.         res = "0";
  76.     }
  77.     return res;
  78. }
  79. int main() {
  80.     int n;
  81.     cin >> n;
  82.    
  83.     string sum = "0";
  84.     string power_of_two = "1";
  85.    
  86.     for(int i = 0; i < n; i++) {
  87.         power_of_two = multiply(power_of_two, "2");
  88.         sum = add(sum, power_of_two);
  89.     }
  90.     cout << sum << endl;
  91.     return 0;
  92. }
  93.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement