結果

問題 No.441 和か積
ユーザー dgd1724dgd1724
提出日時 2016-11-12 07:05:20
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 2,574 bytes
コンパイル時間 1,923 ms
コンパイル使用メモリ 151,180 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-09 10:24:20
合計ジャッジ時間 2,885 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

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.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 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; }
	}
}

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);
	if (S == P) { std::cout << "E" << std::endl; }
	else {
		if (Compare_MultiplePrecision(S, P)) { std::cout << "S" << std::endl; }
		else { std::cout << "P" << std::endl; }
	}
}

0