結果

問題 No.1006 Share an Integer
ユーザー sahiyasahiya
提出日時 2020-12-19 19:57:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,339 bytes
コンパイル時間 1,827 ms
コンパイル使用メモリ 128,316 KB
実行使用メモリ 34,896 KB
最終ジャッジ日時 2023-10-21 09:29:45
合計ジャッジ時間 7,124 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,700 KB
testcase_02 AC 2 ms
5,704 KB
testcase_03 RE -
testcase_04 AC 2 ms
5,704 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
5,708 KB
testcase_09 AC 2 ms
5,712 KB
testcase_10 AC 2 ms
5,708 KB
testcase_11 AC 225 ms
22,748 KB
testcase_12 AC 422 ms
34,040 KB
testcase_13 AC 454 ms
34,896 KB
testcase_14 AC 431 ms
34,896 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 365 ms
32,988 KB
testcase_19 AC 364 ms
30,940 KB
testcase_20 AC 384 ms
33,172 KB
testcase_21 AC 446 ms
34,868 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <deque>
#include <forward_list>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef bitset<16> BS;
struct edge {
    int to, cost, id;
};
const ll MOD = 1E+09 + 7;
const ll INF = 1E18;
const int MAX_N = 2E+06;

ll X;

ll spf[MAX_N + 1]; //spf[i]はiの素因数のうち最小のものを示す

void smallest_prime_factor(ll n);
map<ll, ll> prime_factor(ll n);
ll div_count(map<ll, ll>& mp);

ll EQ[MAX_N + 1];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> X;
    smallest_prime_factor(X);
    ll res = INF;
    for (ll i = 1; i <= X / 2 + 1; i++) {
        auto a = prime_factor(i);
        auto b = prime_factor(X - i);
        ll an = div_count(a);
        ll bn = div_count(b);
        ll eq = abs(2 * i - X - an + bn);
        EQ[i] = eq, EQ[X - i] = eq;
        res = min(res, abs(2 * i - X - an + bn));
    }
    vector<P> ans;
    for (ll i = 1; i <= X; i++) {
        if (EQ[i] == res) {
            ans.push_back(P(i, X - i));
        }
    }
    sort(ans.begin(), ans.end());

    /*
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            cout << "i = " << i << ", j = " << j << ", dp = " << dp[i][j] << "\n";
        }
    }
    */

    for (auto p : ans) {
        cout << p.first << " " << p.second << "\n";
    }

    return 0;
}

//n以下のspfを求める
void smallest_prime_factor(ll n)
{
    for (ll i = 1; i <= n; i++) {
        spf[i] = i;
    }
    for (ll i = 2; i * i <= n; i++) {
        if (spf[i] == i) {
            for (ll j = i * i; j <= n; j += i) {
                spf[j] = i;
            }
        }
    }
}

//spfを元にnを素因数分解
map<ll, ll> prime_factor(ll n)
{
    map<ll, ll> res;
    while (n != 1) {
        res[spf[n]]++;
        n /= spf[n];
    }
    return res;
}

ll div_count(map<ll, ll>& mp)
{
    ll res = 1;
    for (auto p : mp) {
        res *= (p.second + 1);
    }
    return res;
}
0