結果

問題 No.443 GCD of Permutation
ユーザー tubo28tubo28
提出日時 2016-11-11 23:04:44
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,044 bytes
コンパイル時間 1,694 ms
コンパイル使用メモリ 171,924 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2024-11-25 10:41:36
合計ジャッジ時間 2,594 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 25 WA * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define all(c) begin(c), end(c)
#define dump(x) cerr << __LINE__ << ":\t" #x " = " << x << endl
#define int ll

signed main(){
    string s;
    while(cin >> s){
        int n = s.size();
        if(count(all(s), s[0]) == n){
            cout << s << endl;
        } else if(s.size() <= 12){
            sort(all(s));
            ll g = 0;
            do {
                g = __gcd(g, stoll(s));
            } while(next_permutation(all(s)));
            cout << g << endl;
        } else {
            int sum = 0;
            int g = 0;
            rep(i,n){
                int x = s[i] - '0';
                g = __gcd(g, x);
                sum += x;
            }
            rep(i,n){
                s[i] = (s[i] - '0') / g + '0';
            }
            sum /= g;
            if(sum%9 == 0) g *= 9;
            else if(sum%3 == 0) g *= 3;
            else g *= 1;
            cout << g << endl;
        }
    }
}
0