結果

問題 No.1312 Snake Eyes
ユーザー face4
提出日時 2020-12-09 20:24:59
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 873 bytes
コンパイル時間 556 ms
コンパイル使用メモリ 64,776 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-30 13:05:46
合計ジャッジ時間 3,069 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 85
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
using namespace std;
typedef long long ll;

// sqrt(n) より大きいpを用いてnをp進で表したとき
// 3桁以上になることはありえない

int main(){
    ll n;
    cin >> n;
    if(n == 1){
        cout << 2 << endl;
        return 0;
    }else if(n == 2){
        cout << 3 << endl;
        return 0;
    }
    ll ans = n-1;
    for(ll i = 2; i*i <= n; i++){
        ll cp = n;
        ll least = cp%i;
        bool f = true;
        while(cp > 0 && f){
            f &= (cp%i)==least;
            cp /= i;
        }
        if(f)   ans = min(ans, i);
        // p進2桁になる場合を考慮してやる必要がある
        // p進 aa
        if(n%i) continue;
        ll p = n/i-1;
        if(p > i)   ans = min(ans, p);
        p = i-1;
        if(p > n/i)   ans = min(ans, p);
    }
    cout << ans << endl;
    return 0;
}
0