結果
| 問題 |
No.443 GCD of Permutation
|
| コンテスト | |
| ユーザー |
Kmcode1
|
| 提出日時 | 2016-11-11 23:12:39 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,246 bytes |
| コンパイル時間 | 1,503 ms |
| コンパイル使用メモリ | 168,028 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-11-25 09:27:08 |
| 合計ジャッジ時間 | 2,531 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 20 WA * 8 |
ソースコード
#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()));
}
int N = 1;
for (int i = 2; i < 10; i++){
int GG = __gcd(N, i);
N /= GG;
N *= i;
}
gc = __gcd(gc, N);
cout << gc << endl;
return 0;
}
Kmcode1