結果

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

テストケース

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

ソースコード

diff #

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

string s;

map<int,int> S;

int __gcd(int a, int b){
	if (a > b){
		swap(a, b);
	}
	while (a){
		swap(a, b);
		a %= b;
	}
	return b;
}
int ans = 0;
vector<int> v;
inline void  dfs(int f =5,int las=-1){
	if (f == 0){
		vector<int> tmp;
		do{
			int num = 0;
			for (int i = 0; i < v.size(); i++){
				num *= 10;
				num += v[i];
			}
			tmp.push_back(num);
		} while (next_permutation(v.begin(), v.end()));
		for (int i = 0; i < tmp.size(); i++){
			for (int j = i + 1; j < tmp.size(); j++){
				ans = __gcd(ans, abs(tmp[j] - tmp[i]));
			}
		}
		tmp.clear();
		return;
	}
	for (auto it = S.begin(); it != S.end(); it++){
		if ((*it).first >= las){
			if ((*it).second){
				(*it).second--;
				v.push_back((*it).first);
				dfs(f - 1,(*it).first);
				v.pop_back();
			}
		}
	}
}
int main(){
	cin >> s;
	for (int i = 0; i < s.size(); i++){
		S[s[i] - '0']++;
	}
	if (S.size() == 1){
		cout << s << endl;
		return 0;
	}
	if (S.size()>=4){
		puts("1");
		return 0;
	}
	dfs(min((int)(s.size()), 5));
	cout << ans << endl;
	return 1;
}
0