結果

問題 No.443 GCD of Permutation
ユーザー mayoko_mayoko_
提出日時 2016-11-11 23:18:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,141 bytes
コンパイル時間 858 ms
コンパイル使用メモリ 84,700 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-16 20:32:37
合計ジャッジ時間 1,784 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<vector>
#include<string>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<queue>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
	string N;
	cin >> N;
	int n = N.size();
	sort(N.begin(), N.end());
	if (n <= 8) {
		int ans = 0;
		do {
			ans = __gcd(ans, stoi(N));
		} while (next_permutation(N.begin(), N.end()));
		cout << ans << endl;
		return 0;
	}
	if (N[0] == N[n-1]) {
		cout << N << endl;
		return 0;
	}
	if (N[n-2] == '0') {
		cout << N[n-1] << endl;
		return 0;
	}
	int sum = 0;
	int cnt0 = 0, cnt2 = 0, cnt4 = 0, cnt8 = 0;
	for (char c : N) {
		int num = c-'0';
		sum += num;
		if (num == 0) ++cnt0;
		else {
			if (num%2 == 0) ++cnt2;
			if (num%4 == 0) ++cnt4;
			if (num%8 == 0) ++cnt8;
		}
	}
	int ans = 1;
	if (sum%9 == 0) ans *= 9;
	else if (sum%3 == 0) ans *= 3;
	if (cnt8 > 0 && cnt8 + cnt0 == n) ans *= 8;
	else if (cnt4 > 0 && cnt4 + cnt0 == n) ans *= 4;
	else if (cnt2 > 0 && cnt2 + cnt0 == n) ans *= 2;
	cout << ans << endl;
	return 0;
}
0