답안 #490947

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
490947 2021-11-30T01:21:21 Z kawaii Multiply (CEOI17_mul) C++17
40 / 100
338 ms 131076 KB
#include <bits/stdc++.h>
using namespace std; 
 
string n, m;
int a, b;

string add(string a, string b){
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    a.push_back('0'); b.push_back('0');
    while(a.size() > b.size()) b.push_back('0');
    while(a.size() < b.size()) a.push_back('0');
    int cnt = 0, du = 0;
    string res = "";
    for(int i = 0; i < a.size(); i++){
        cnt = a[i] + b[i] - 96 + du;
        du = cnt / 10;
        res.push_back(char(cnt % 10 + 48));
    }
    while(res.back() == '0') res.pop_back();
    reverse(res.begin(), res.end());
    return res;
}

string chia(string a){
    string res = "";
    int p = 0;
    for(int i = 0; i < a.size(); i++){
        p = p * 10 + a[i] - 48;
        if(p >= 2){  
            res.push_back(char(p / 2 + 48));
            p %= 2;
        }
        else res.push_back('0');
    } 
    reverse(res.begin(), res.end());
    while(res.back() == '0') res.pop_back();
    reverse(res.begin(), res.end());
    if(res.size() == 0) res.push_back('0');
    return res;
}

string nhan(string a, string b){
    if(b == "0") return "0"; 
    if(b == "1") return a;
    string ans = nhan(a, chia(b));
    //cout << a <<" "<< chia(b) << "\n";
    if(b.back() % 2 == 0) return add(ans, ans);
    else return add(add(ans, ans), a);
}

void solve(){ 
    cout << nhan(n, m);
}
 
signed main(){  
    ios_base::sync_with_stdio(NULL); cin.tie(nullptr); cout.tie(nullptr);
    cin >> a >> b >> n >> m;
    solve();
}

Compilation message

mul.cpp: In function 'std::string add(std::string, std::string)':
mul.cpp:15:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   15 |     for(int i = 0; i < a.size(); i++){
      |                    ~~^~~~~~~~~~
mul.cpp: In function 'std::string chia(std::string)':
mul.cpp:28:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   28 |     for(int i = 0; i < a.size(); i++){
      |                    ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Runtime error 338 ms 131076 KB Execution killed with signal 9
12 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Correct 0 ms 204 KB Output is correct
5 Correct 0 ms 204 KB Output is correct
6 Correct 0 ms 204 KB Output is correct
7 Correct 0 ms 204 KB Output is correct
8 Correct 1 ms 204 KB Output is correct
9 Correct 0 ms 204 KB Output is correct
10 Correct 0 ms 204 KB Output is correct
11 Runtime error 338 ms 131076 KB Execution killed with signal 9
12 Halted 0 ms 0 KB -