結果

問題 No.1006 Share an Integer
ユーザー @abcde@abcde
提出日時 2020-03-06 23:47:08
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 2,000 ms
コード長 1,811 bytes
コンパイル時間 1,643 ms
コンパイル使用メモリ 173,732 KB
実行使用メモリ 11,640 KB
最終ジャッジ日時 2023-08-04 13:07:25
合計ジャッジ時間 3,288 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
11,604 KB
testcase_01 AC 5 ms
11,416 KB
testcase_02 AC 6 ms
11,480 KB
testcase_03 AC 5 ms
11,428 KB
testcase_04 AC 5 ms
11,416 KB
testcase_05 AC 6 ms
11,424 KB
testcase_06 AC 5 ms
11,424 KB
testcase_07 AC 6 ms
11,432 KB
testcase_08 AC 6 ms
11,432 KB
testcase_09 AC 6 ms
11,376 KB
testcase_10 AC 5 ms
11,524 KB
testcase_11 AC 38 ms
11,368 KB
testcase_12 AC 68 ms
11,640 KB
testcase_13 AC 73 ms
11,480 KB
testcase_14 AC 73 ms
11,372 KB
testcase_15 AC 61 ms
11,488 KB
testcase_16 AC 29 ms
11,428 KB
testcase_17 AC 40 ms
11,332 KB
testcase_18 AC 61 ms
11,484 KB
testcase_19 AC 57 ms
11,416 KB
testcase_20 AC 62 ms
11,404 KB
testcase_21 AC 71 ms
11,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define repex(i, a, b, c) for(int i = a; i < b; i += c)
#define repx(i, a, b) repex(i, a, b, 1)
#define rep(i, n) repx(i, 0, n)
#define repr(i, a, b) for(int i = a; i >= b; i--)
#define pb push_back
#define a first
#define b second
#define all(x) x.begin(), x.end()
int a[2020202];

// 与えられた正の整数のすべての約数を抽出.
// @param X: 約数を抽出したい正の整数.
// @return ret: すべての約数.
vector<int> div(int X){
    vector<int> ret;
    ret.pb(1);
    for(int d = 2; d * d <= X; d++){
        if(X % d == 0){
            ret.pb(d);
            if(d * d != X) ret.pb(X / d);
        }
    }
    if(X > 1) ret.pb(X);
    sort(all(ret));
    return ret;
}

int main(){
    
    // 1. 入力情報.
    int X;
    scanf("%d", &X);
    
    // 2. 正の約数の個数.
    // X = 2000000 で, 6389[ms] だったので, div関数使うパターンは, 却下.
    // repx(i, 1, X){
    //     vector<int> v = div(i);
    //     a[i] = i - v.size();
    // }
    // rep(i, 20) printf("%d ", a[i]);
    
    // X = 2000000 で, 88[ms] だったので, だいぶ実行速度が改善した.
    memset(a, 1, sizeof(a));
    repx(i, 2, X + 1) repex(j, i, X + 1, i) a[j]++;
    repx(i, 1, X + 1) a[i] = i - a[i];
    // rep(i, 20) printf("%d ", a[i]);
    
    // 3. |f(A) - f(B)| が 最小となるものを抽出.
    int minD = 123456789;
    repx(i, 1, X){
        int d = abs(a[i] - a[X - i]);
        if(d < minD) minD = d;
    }
    
    // 4. 3. で 取得した最小値をとる A を 抽出.
    vector<pair<int, int>> ans;
    repx(i, 1, X){
        int d = abs(a[i] - a[X - i]);
        if(d == minD) ans.pb({i, X - i});
    }
    
    // 5. 出力.
    for(auto &p : ans) printf("%d %d\n", p.a, p.b);
    return 0;
}
0