結果

問題 No.638 Sum of "not power of 2"
ユーザー kichirb3kichirb3
提出日時 2018-03-07 09:32:26
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 1,321 bytes
コンパイル時間 735 ms
コンパイル使用メモリ 79,420 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-25 07:19:41
合計ジャッジ時間 1,484 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// No.638 Sum of "not power of 2"
//
//

#include <iostream>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;

long long int solve(long long int N);
bool is_valid(long long int a, long long int b, set<long long int> &prohibits);

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

    long long N;
    cin >> N;
    long long ans = solve(N);
    if (ans != -1)
        cout << ans << " " << N - ans << endl;
    else
        cout << ans << endl;
}

long long int solve(long long int N) {
    set<long long> prohibits;
    for (auto i = 0; i < 61; ++i)
        prohibits.insert(pow(2, i));

    long long ans = -1;
    long long a, b;
    for (auto i = 0; i < 61; ++i) {
        long long t = pow(2, i);
        a = t - 1;
        b = N - a;
        if (is_valid(a, b, prohibits)) {
            ans = a;
            break;
        }
        a = t + 1;
        b = N - a;
        if (is_valid(a, b, prohibits)) {
            ans = a;
            break;
        }
    }
    return ans;
}

bool is_valid(long long int a, long long int b, set<long long int> &prohibits) {
    if (a < 1 || b < 1)
        return false;
    if (prohibits.find(a) != prohibits.end())
        return false;
    if (prohibits.find(b) != prohibits.end())
        return false;
    return true;
}
0