結果
| 問題 | No.638 Sum of "not power of 2" | 
| コンテスト | |
| ユーザー |  kichirb3 | 
| 提出日時 | 2018-03-07 09:32:26 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 2 ms / 1,000 ms | 
| コード長 | 1,321 bytes | 
| コンパイル時間 | 846 ms | 
| コンパイル使用メモリ | 79,796 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-09-25 02:38:53 | 
| 合計ジャッジ時間 | 1,392 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 12 | 
ソースコード
// 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;
}
            
            
            
        