program_contest_library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ferin-15/program_contest_library

:heavy_check_mark: test/yuki983.test.cpp

Back to top page

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/983"
#include "../memo/macro.hpp"
#include "../math/fzt_fmt.cpp"

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;
}

#line 1 "test/yuki983.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/983"
#line 1 "memo/macro.hpp"
#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;
const ll INF = 1LL<<60;
#line 1 "math/fzt_fmt.cpp"
// BEGIN CUT
// 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);
}
// END CUT
#line 4 "test/yuki983.test.cpp"

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;
}

Back to top page