結果

問題 No.443 GCD of Permutation
ユーザー Kmcode1
提出日時 2016-11-11 23:10:08
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,135 bytes
コンパイル時間 1,438 ms
コンパイル使用メモリ 169,528 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-25 09:25:00
合計ジャッジ時間 2,356 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 23 WA * 5
権限があれば一括ダウンロードができます

ソースコード

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