結果

問題 No.1006 Share an Integer
ユーザー bond_cmprogbond_cmprog
提出日時 2020-03-06 22:39:07
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 677 ms / 2,000 ms
コード長 2,448 bytes
コンパイル時間 2,211 ms
コンパイル使用メモリ 179,048 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-04-22 09:46:26
合計ジャッジ時間 9,189 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 360 ms
7,680 KB
testcase_12 AC 636 ms
10,624 KB
testcase_13 AC 677 ms
11,136 KB
testcase_14 AC 673 ms
10,900 KB
testcase_15 AC 567 ms
9,984 KB
testcase_16 AC 268 ms
6,656 KB
testcase_17 AC 362 ms
7,680 KB
testcase_18 AC 564 ms
9,984 KB
testcase_19 AC 536 ms
9,812 KB
testcase_20 AC 629 ms
10,240 KB
testcase_21 AC 660 ms
10,884 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
#define ll long long
using namespace std;
//typedef vector<unsigned int>vec;
//typedef vector<ll>vec;
//typedef vector<vec> mat;
typedef pair<int, int> P;
typedef pair<ll,ll> LP;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INF = 1000000000;
const ll LINF = 1000000000000000000;//1e18
const ll  MOD = 1000000007;
const double PI = acos(-1.0);
const double EPS = 1e-10;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void add(T &a, T b){a = ((a+b) % MOD + MOD) % MOD;};

template<typename T>
vector<T> smallest_prime_factors(T n) {

    vector<T> spf(n + 1);
    for (int i = 0; i <= n; i++) spf[i] = i;


    for (T i = 2; i * i <= n; i++) {

        // 素数だったら
        if (spf[i] == i) {

            for (T j = i * i; j <= n; j += i) {

                // iを持つ整数かつまだ素数が決まっていないなら
                if (spf[j] == j) {
                    spf[j] = i;
                }
            }
        }
    }

    return spf;
}

template<typename T>
map<T, T> factorization(T x, vector<T> &spf) {
    map<T, T> ret;
    while (x != 1) {
        ret[spf[x]]++;
        x /= spf[x];
    }
    //sort(ret.begin(), ret.end());
    return ret;
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int X;
    cin >> X;
    auto spf = smallest_prime_factors(X);
    vector<pair<int, int>> ans;
    int ma = INF;
    for(int i=X;i>=0;i--){
        int d1  = 1, d2 = 1;
        if(i > 1){
            auto res1 = factorization(i, spf);
            for(auto x : res1) d1 *= (x.second + 1);
        }
        if(X-i > 1){
            auto res2 = factorization(X-i, spf);
            for(auto x : res2) d2 *= (x.second + 1);
        }
        int f = abs((i - d1) - (X-i - d2));
        if(f == ma){
            ans.emplace_back(i, X-i);
            //ans.emplace_back(X-i, i);
        }
        else if(f < ma){
            ma = f;
            ans = vector<pair<int,int>> ();
            ans.emplace_back(i, X-i);
            //ans.emplace_back(X-i, i);
        }
    }
    //cout << ma << endl;
    sort(ans.begin(), ans.end());
    for(auto x : ans) cout << x.first << " " << x.second << endl;
}
0