結果

問題 No.983 Convolution
ユーザー ferinferin
提出日時 2020-02-11 16:22:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 2,169 bytes
コンパイル時間 2,153 ms
コンパイル使用メモリ 203,988 KB
実行使用メモリ 4,412 KB
最終ジャッジ日時 2023-07-24 12:51:40
合計ジャッジ時間 4,246 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 13 ms
4,380 KB
testcase_04 AC 11 ms
4,380 KB
testcase_05 AC 10 ms
4,380 KB
testcase_06 AC 16 ms
4,376 KB
testcase_07 AC 14 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 5 ms
4,380 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 5 ms
4,380 KB
testcase_12 AC 15 ms
4,384 KB
testcase_13 AC 8 ms
4,380 KB
testcase_14 AC 9 ms
4,380 KB
testcase_15 AC 13 ms
4,376 KB
testcase_16 AC 10 ms
4,384 KB
testcase_17 AC 15 ms
4,412 KB
testcase_18 AC 4 ms
4,384 KB
testcase_19 AC 11 ms
4,384 KB
testcase_20 AC 18 ms
4,380 KB
testcase_21 AC 19 ms
4,380 KB
testcase_22 AC 5 ms
4,380 KB
testcase_23 AC 9 ms
4,376 KB
testcase_24 AC 4 ms
4,380 KB
testcase_25 AC 3 ms
4,380 KB
testcase_26 AC 7 ms
4,380 KB
testcase_27 AC 5 ms
4,380 KB
testcase_28 AC 11 ms
4,380 KB
testcase_29 AC 21 ms
4,384 KB
testcase_30 AC 11 ms
4,376 KB
testcase_31 AC 7 ms
4,384 KB
testcase_32 AC 12 ms
4,380 KB
testcase_33 AC 1 ms
4,380 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using PII = pair<ll, ll>;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
struct FastIO {FastIO() { cin.tie(0); ios::sync_with_stdio(0); }}fastiofastio;
#ifdef DEBUG_ 
#include "../program_contest_library/memo/dump.hpp"
#else
#define dump(...)
#endif
const ll INF = 1LL<<60;

// a.size() は2べき
// upper: g(S) = \sum_{S \subseteq T} f(T)
// lower: g(S) = \sum_{T \subseteq S} f(T)
template<bool upper>
vector<ll> fzt(vector<ll> a) {
    const int n = log2(a.size());
    REP(i, n) REP(j, 1LL<<n) {
        if(upper && !(j&1LL<<i)) a[j] += a[j|(1LL<<i)];
        else if(!upper && (j&1LL<<i)) a[j] += a[j^(1LL<<i)];
    }
    return a;
}
// a.size() は2べき
// upper: f(S) = \sum_{S \subseteq T} (-1)^(|T\S|) g(T)
// lower: f(S) = \sum_{T \subseteq S} (-1)^(|T\S|) g(T)
template<bool upper>
vector<ll> fmt(vector<ll> a) {
    const int n = log2(a.size());
    REP(i, n) REP(j, 1LL<<n) {
        if(upper && !(j&1LL<<i)) a[j] -= a[j|(1LL<<i)];
        else if(!upper && (j&1LL<<i)) a[j] -= a[j^(1LL<<i)];
    }
    return a;
}

// a.size(),b.size() は2べき
// c_k = \sum_{k = i & j} a_ib_j
vector<ll> convAND(vector<ll> a, vector<ll> b) {
    a = fzt<true>(a);
    b = fzt<true>(b);
    REP(i, a.size()) a[i] *= b[i];
    return fmt<true>(a);
}
// a.size(),b.size() は2べき
// c_k = \sum_{k = i | j} a_ib_j
vector<ll> convOR(vector<ll> a, vector<ll> b) {
    a = fzt<false>(a);
    b = fzt<false>(b);
    REP(i, a.size()) a[i] *= b[i];
    return fmt<false>(a);
}

int main(void) {
    ll n;
    cin >> n;
    vector<ll> a(n);
    REP(i, n) {
        cin >> a[i];
        if(a[i]==-1) a[i] = 0;
    }

    if(n <= 20) {
        auto v = convAND(a, a);
        ll g = 0;
        REP(i, n) g = gcd(g, v[i]);
        cout << (g==0 ? -1 : g) << endl;
    } else {
        ll g = 0;
        REP(i, n) g = gcd(g, a[i]);
        cout << (g==0 ? -1 : g*g) << endl;
    }

    return 0;
}
0