結果
| 問題 | No.442 和と積 |
| コンテスト | |
| ユーザー |
dgd1724
|
| 提出日時 | 2016-11-12 22:28:05 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0 + boost 1.89.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 3,830 bytes |
| 記録 | |
| コンパイル時間 | 2,389 ms |
| コンパイル使用メモリ | 168,960 KB |
| 実行使用メモリ | 13,640 KB |
| 最終ジャッジ日時 | 2024-11-25 21:39:12 |
| 合計ジャッジ時間 | 4,958 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 15 WA * 2 TLE * 1 |
ソースコード
#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.length() <= 18) {
unsigned long long temp = std::stoull(A);
A = std::to_string(temp);
}
else {
std::reverse(A.begin(), A.end());
while (A.back() == '0') { A.pop_back(); }
std::reverse(A.begin(), A.end());
if (A == "") { A = "0"; }
}
}
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, 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 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 bool Compare_MultiplePrecision(const std::string &A, const std::string &B) {
if (A == B) { return true; }
if (A.length() > B.length()) { return true; }
else if (A.length() < B.length()) { return false; }
else {
unsigned int i = 0;
while (A[i] == B[i]) { i++; }
if (A[i] > B[i]) { return true; }
else { return false; }
}
}
inline std::string Greatest_Common_Divisor(const std::string &str1, const std::string &str2) {
std::string A = str1, B = str2;
int al = A.length(), bl = B.length();
while (abs(al - bl) >= 2) {
std::string temp;
if (al > bl) {
temp = B;
for (int i = 0; i < al - bl - 1; i++) { temp += "0"; }
A = Subtract_MultiplePrecision(A, temp);
al = A.length();
}
else {
temp = A;
for (int i = 0; i < bl - al - 1; i++) { temp += "0"; }
B = Subtract_MultiplePrecision(B, temp);
bl = B.length();
}
}
while (A != "0" && B != "0") {
if (Compare_MultiplePrecision(A, B)) {
A = Subtract_MultiplePrecision(A, B);
}
else {
B = Subtract_MultiplePrecision(B, A);
}
}
return(Add_MultiplePrecision(A, B));
}
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;
}
dgd1724