Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- string add_two_numbers(string a, string b) {
- if(a.size() < b.size()) {
- swap(a, b);
- }
- int carry = 0;
- string res = "";
- int i = (int) a.size() - 1, j = (int) b.size() - 1;
- while(j >= 0 and i >= 0) {
- int digit_a = (int) (a[i] - '0');
- int digit_b = (int) (b[j] - '0');
- int sum = digit_a + digit_b + carry;
- if(sum < 10) {
- carry = 0;
- }
- else {
- carry = 1;
- sum -= 10;
- }
- res += (sum + '0');
- i--;
- j--;
- }
- while(i >= 0) {
- int digit_a = (int) (a[i] - '0');
- int sum = digit_a + carry;
- if(sum < 10) {
- carry = 0;
- }
- else {
- carry = 1;
- sum -= 10;
- }
- res += (sum + '0');
- i--;
- }
- if(carry == 1) {
- res += "1";
- }
- reverse(res.begin(), res.end());
- return res;
- }
- string multiply_two_numbers(string a, string b) {
- vector<int> v((int) a.size() + (int) b.size(), 0);
- int sz_a = (int) a.size();
- int sz_b = (int) b.size();
- for(int i = sz_a - 1; i >= 0; i--) {
- for(int j = sz_b - 1; j >= 0; j--) {
- v[i + j + 1] += (a[i] - '0') * (b[j] - '0');
- }
- }
- for(int i = sz_a + sz_b - 1; i >= 0; i--) {
- if(v[i] >= 10) {
- v[i - 1] += v[i] / 10;
- v[i] %= 10;
- }
- }
- string res = "";
- int i = 0;
- while(v[i] == 0) {
- i++;
- }
- while(i < sz_a + sz_b) {
- res += (v[i] + '0');
- i++;
- }
- return res;
- }
- int main() {
- ios_base::sync_with_stdio(false);
- int n;
- cin >> n;
- string power_of_two = "2";
- string res = "0";
- for(int i = 0; i < n; i++) {
- res = add_two_numbers(res, power_of_two);
- power_of_two = multiply_two_numbers(power_of_two, "2");
- }
- cout << res << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement