結果

問題 No.443 GCD of Permutation
ユーザー Kmcode1Kmcode1
提出日時 2016-11-11 23:10:08
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,135 bytes
コンパイル時間 1,313 ms
コンパイル使用メモリ 154,948 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-16 19:37:07
合計ジャッジ時間 2,410 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int __gcd(int a, int b){
	if (a > b){
		swap(a, b);
	}
	while (a){
		swap(a, b);
		a %= b;
	}
	return b;
}

string s;

set<int> S;
int main(){
	cin >> s;
	int sum = 0;
	bool f = true;
	for (int i = 0; i < s.size(); i++){
		S.insert(s[i] - '0');
		sum += (s[i] - '0');
		if ((s[i] - '0') % 2){
			f = false;
		}
	}
	if (S.size() == 1){
		cout << s << endl;
		return 0;
	}
	int gc = 0;
	for (auto it = S.begin(); it != S.end(); it++){
		for (auto itt = S.begin(); itt != S.end(); itt++){
			int sa = abs(10 * (*it) + (*itt) - (10 * (*itt) + (*it)));
			gc = __gcd(gc, sa);
		}
	}
	if (gc % 9 == 0 && sum % 9){
		gc /= 9;
	}
	if (gc % 3 == 0 && sum % 3){
		gc /= 3;
	}
	while (f==false&&gc % 2 == 0){
		gc /= 2;
	}
	sort(s.begin(), s.end());
	reverse(s.begin(), s.end());
	while (s.size() && s.back() == '0'){
		s.pop_back();
	}
	sort(s.begin(), s.end());
	if (s.size() <= 3){
		do{
			int num = 0;
			for (int i = 0; i < s.size(); i++){
				num *= 10;
				num += s[i] - '0';
			}
			gc = __gcd(gc, num);
		} while (next_permutation(s.begin(), s.end()));
	}
	cout << gc << endl;
	return 0;
}
0