Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- string sum(string a, string b){
- reverse(a.begin(), a.end());
- reverse(b.begin(), b.end());
- string ans;
- int c = 0;
- if(a.size() < b.size())
- swap(a, b);
- for(int i = 0; i < b.size(); ++i){
- int m = (a[i] - '0') + (b[i] - '0');
- m += c;
- int x = m % 10;
- ans += to_string(x);
- c = m / 10;
- }
- for(int i = b.size(); i < a.size(); ++i){
- int m = (a[i] - '0') + c;
- ans += to_string(m % 10);
- c = m / 10;
- }
- if(c > 0){
- ans += to_string(c);
- }
- reverse(ans.begin(), ans.end());
- return ans;
- }
- string mul(string s, int n){
- string a;
- int c = 0;
- reverse(s.begin(), s.end());
- for(int i = 0; i < s.size(); ++i){
- int m = (s[i] - '0') * n;
- m += c;
- int b = m % 10;
- a += to_string(b);
- c = m / 10;
- }
- if(c > 0){
- a += to_string(c);
- }
- reverse(a.begin(), a.end());
- return a;
- }
- int main() {
- string s;
- int n;
- cin >> s >> n;
- cout << sum(mul(s, n / 10) + '0', mul(s, n % 10));
- }
Advertisement
Add Comment
Please, Sign In to add comment