結果

問題 No.1017 Reiwa Sequence
ユーザー outline
提出日時 2021-01-13 06:53:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 517 ms / 2,000 ms
コード長 2,057 bytes
コンパイル時間 1,785 ms
コンパイル使用メモリ 131,676 KB
最終ジャッジ日時 2025-01-17 17:14:05
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <queue>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <tuple>
#include <deque>
#include <array>
#include <numeric>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <chrono>
#include <random>
#include <limits>
#include <iterator>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
using namespace std;

using ll = long long;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template<class T>
inline bool chmax(T& x, T y){
    if(x < y){
        x = y;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T& x, T y){
    if(x > y){
        x = y;
        return true;
    }
    return false;
}

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

    int n;
    cin >> n;
    vector<int> a(n);
    for(int i = 0; i < n; ++i)  cin >> a[i];

    int m = min(n, 22);
    vector<int> sum(1 << m), cnt(150000 * m + 1);
    for(int mask = 0; mask < 1 << m; ++mask){
        for(int i = 0; i < m; ++i){
            if(mask >> i & 1)   sum[mask] += a[i];
        }
        cnt[sum[mask]] += 1;
    }

    int S = -1;
    for(int i = 1; i <= 150000 * m; ++i){
        if(cnt[i] > 1){
            S = i;
            break;
        }
    }
    if(S == -1){
        cout << "No\n";
        return 0;
    }

    int s = -1, t = -1;
    for(int mask = 0; mask < 1 << m; ++mask){
        if(sum[mask] != S)  continue;
        if(s == -1) s = mask;
        else    t = mask;
    }

    int andmask = s & t;
    s ^= andmask, t ^= andmask;

    for(int i = 0; i < m; ++i){
        if(s >> i & 1)  a[i] = a[i];
        else if(t >> i & 1) a[i] = -a[i];
        else    a[i] = 0;
    }
    for(int i = m; i < n; ++i)  a[i] = 0;

    cout << "Yes\n";
    for(int i = 0; i < n; ++i){
        cout << a[i] << (i == n - 1 ? '\n' : ' ');
    }

    return 0;
}
0