結果

問題 No.442 和と積
ユーザー dgd1724
提出日時 2016-11-20 06:04:34
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 4,531 bytes
コンパイル時間 2,254 ms
コンパイル使用メモリ 170,004 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-07-04 22:43:28
合計ジャッジ時間 2,295 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <bits/stdc++.h>
//const static double de_PI = 3.14159265358979323846;
//const static int de_MOD = 1000000007;
//const static int de_MAX = 999999999;
//const static int de_MIN = -999999999;
inline void Erase_LeadingZero(std::string &A) {
if (A == "") {}
else if (A.length() <= 18) {
unsigned long long temp = std::stoull(A);
A = std::to_string(temp);
}
else {
std::reverse(A.begin(), A.end());
while (!A.empty() && A.back() == '0') { A.pop_back(); }
std::reverse(A.begin(), A.end());
if (A == "") { A = "0"; }
}
}
inline int Compare_MultiplePrecision(const std::string &str1, const std::string &str2) {
if (str1 == str2) { return 0; }
std::string A = str1, B = str2;
Erase_LeadingZero(A);
Erase_LeadingZero(B);
if (A.length() > B.length()) { return 1; }
else if (A.length() < B.length()) { return -1; }
else {
unsigned int i = 0;
while (A[i] == B[i]) { i++; }
if (A[i] > B[i]) { return 1; }
else { return -1; }
}
}
inline std::string Add_MultiplePrecision(const std::string &str1, const std::string &str2) {
std::string A = str1, B = str2;
int al = A.length(), bl = B.length(), cl = std::max(A.length(), B.length());
std::vector<int> a(cl), b(cl), c(cl + 1);
for (int i = 0; i < al; i++) { a[i] = A[al - 1 - i] - '0'; }
for (int i = 0; i < bl; i++) { b[i] = B[bl - 1 - i] - '0'; }
int carry = 0;
for (int i = 0; i < cl; i++) {
c[i] = (a[i] + b[i] + carry) % 10;
c[i + 1] = carry = (a[i] + b[i] + carry) / 10;
}
std::string ans(cl + 1, 0);
for (int i = 0; i < cl + 1; i++) { ans[cl - i] = static_cast<char>(c[i]) + '0'; }
Erase_LeadingZero(ans);
return(ans);
}
inline std::string Subtract_MultiplePrecision(const std::string &str1, const std::string &str2) {
std::string A = str1, B = str2;
int al = A.length(), bl = B.length();
std::vector<int> a(al), b(al), c(al);
for (int i = 0; i < al; i++) { a[i] = A[al - 1 - i] - '0'; }
for (int i = 0; i < bl; i++) { b[i] = B[bl - 1 - i] - '0'; }
for (int i = 0; i < al; i++) {
if (a[i] >= b[i]) {
c[i] = a[i] - b[i];
}
else {
c[i] = a[i] + 10 - b[i];
b[i + 1]++;
}
}
std::string ans(al, 0);
for (int i = 0; i < al; i++) { ans[al - i - 1] = static_cast<char>(c[i]) + '0'; }
Erase_LeadingZero(ans);
return(ans);
}
inline std::string Multiply_MultiplePrecision(const std::string &str1, const std::string &str2) {
std::string A = str1, B = str2;
int al = A.length(), bl = B.length(), cl = al + bl;
std::vector<int> a(al), b(bl), c(cl + 1);
for (int i = 0; i < al; i++) { a[i] = A[al - 1 - i] - '0'; }
for (int i = 0; i < bl; i++) { b[i] = B[bl - 1 - i] - '0'; }
for (int i = 0; i < bl; i++) {
for (int j = 0; j < al; j++) { c[i + j] += b[i] * a[j]; }
}
for (int i = 0; i < cl; i++) {
c[i + 1] += c[i] / 10;
c[i] = c[i] % 10;
}
std::string ans(cl, 0);
for (int i = 0; i < cl; i++) { ans[cl - i - 1] = static_cast<char>(c[i]) + '0'; }
Erase_LeadingZero(ans);
return(ans);
}
inline std::string Divide_MultiplePrecision(const std::string &str1, const std::string &str2, std::string &surplus) {
if (str2 == "0") { std::cout << "0 divide" << std::endl; return ""; }
switch (Compare_MultiplePrecision(str1, str2)) {
case 0:surplus = "0"; return("1");
case -1:surplus = str2; return("0");
}
std::string A = str1, B = str2, ans;
int bl = B.length(), cont = 0;
while (Compare_MultiplePrecision(A, B) == 1) {
cont = 0;
std::string Ad = A.substr(0, bl);
if (Compare_MultiplePrecision(Ad, B) == -1) { Ad = A.substr(0, bl + 1); }
A.replace(0, Ad.length(), "");
while (Compare_MultiplePrecision(Ad, B) >= 0) {
Ad = Subtract_MultiplePrecision(Ad, B);
cont++;
}
ans.push_back(static_cast<char>(cont) + '0');
Erase_LeadingZero(Ad);
if (Ad != "0") { A = Ad + A; }
else { ans.push_back('0'); }
}
if (A == "") { surplus = "0"; }
else { surplus = A; }
return(ans);
}
inline std::string Greatest_Common_Divisor(const std::string &str1, const std::string &str2) {
std::string A = str1, B = str2;
while (A != "0" && B != "0") {
if (Compare_MultiplePrecision(A, B) == 1) {
Divide_MultiplePrecision(A, B, A);
Erase_LeadingZero(A);
}
else {
Divide_MultiplePrecision(B, A, B);
Erase_LeadingZero(B);
}
}
if (A == "0") { return B; }
else { return A; }
}
int main(void) {
//std::ifstream inf("123.txt"); std::cin.rdbuf(inf.rdbuf());
std::string A, B, S, P;
std::cin >> A >> B;
S = Add_MultiplePrecision(A, B);
P = Multiply_MultiplePrecision(A, B);
std::cout << Greatest_Common_Divisor(S, P) << std::endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0