結果
問題 |
No.443 GCD of Permutation
|
ユーザー |
![]() |
提出日時 | 2016-11-11 22:48:27 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,078 bytes |
コンパイル時間 | 1,349 ms |
コンパイル使用メモリ | 169,600 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-11-25 09:03:20 |
合計ジャッジ時間 | 2,377 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 WA * 1 |
other | AC * 9 WA * 19 |
ソースコード
#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 0; }