結果
問題 |
No.443 GCD of Permutation
|
ユーザー |
![]() |
提出日時 | 2016-11-11 22:52:34 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,325 bytes |
コンパイル時間 | 1,623 ms |
コンパイル使用メモリ | 169,916 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-11-25 09:08:24 |
合計ジャッジ時間 | 2,681 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 WA * 1 RE * 2 |
other | AC * 3 WA * 4 RE * 21 |
ソースコード
#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; int sum = 0; int n2 = 0; for (int i = 0; i < s.size(); i++){ S[s[i] - '0']++; sum += s[i] - '0'; if ((s[i] - '0') % 2 == 0){ } else{ n2 = 1; } } ans = 1; int A = 1; if (sum % 9 == 0){ A *= 9; } else{ if (sum % 3 == 0){ A *= 3; } } if (n2 == 0){ A *= 2; } ans = A; if (S.size() == 1){ cout << ans << endl; return 0; } if (S.size()>=4){ puts("1"); return 0; } dfs(min((int)(s.size()), 5)); cout << ans << endl; return 1; }