/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Accepted 2ms 540.0 KiB
#3 Accepted 3ms 520.0 KiB
#4 Accepted 3ms 584.0 KiB
#5 Accepted 4ms 580.0 KiB
#6 Accepted 4ms 588.0 KiB
#7 Accepted 3ms 584.0 KiB
#8 Accepted 6ms 652.0 KiB
#9 Accepted 12ms 712.0 KiB
#10 Accepted 42ms 912.0 KiB
#11 Accepted 486ms 1.746 MiB
#12 Accepted 773ms 2.277 MiB
#13 Accepted 147ms 1.719 MiB
#14 Accepted 207ms 2.039 MiB
#15 Accepted 162ms 1.918 MiB
#16 Accepted 147ms 1.672 MiB
#17 Accepted 162ms 1.789 MiB
#18 Accepted 188ms 1.809 MiB
#19 Accepted 114ms 1.887 MiB

Code

#include<bits/stdc++.h>
#define endl        '\n'
#define F           first
#define S           second
#define pb          push_back
#define yes         cout<<"YES\n"
#define no          cout<<"NO\n"
#define all(x)      x.begin(),x.end()
#define allr(x)     x.rbegin(),x.rend()
#define error1(x)   cerr<<#x<<" = "<<(x)<<endl
#define error2(a,b) cerr<<"("<<#a<<", "<<#b<<") = ("<<(a)<<", "<<(b)<<")\n";
#define coutall(v)  for(auto &it: v) cout << it << " "; cout << endl;
using namespace std;
using ll = long long;
using ld = long double;

void solve() {
    ll n;
    cin >> n;
    vector<ll> v(n);
    for (int i = 0; i < n; i++) {
        ll x; cin >> x;
        v[i] = x;
    }
    
    vector<vector<map<pair<int, int>, int>>> dp((n / 2) + 2, vector<map<pair<int, int>, int>> (n + 1));

    auto rec = [&](int i, int odd, int even, ll gcd1, ll gcd2, auto&& self) -> int {
        if(i == n) return gcd1 + gcd2;
        auto it = dp[odd][i].find(make_pair(gcd1, gcd2));
        if(it != dp[odd][i].end()) return it->S;
        int mx = 0;
        if(odd > 0) {
            if(gcd1 == -1) mx = max(mx, self(i + 1, odd - 1, even, v[i], gcd2, self));
            else mx = max(mx, self(i + 1, odd - 1, even, __gcd(gcd1, v[i]), gcd2, self));
        }
        if(even > 0) {
            if(gcd2 == -1) mx = max(mx, self(i + 1, odd, even - 1, gcd1, v[i], self));
            else mx = max(mx, self(i + 1, odd, even - 1, gcd1, __gcd(v[i], gcd2), self));
        }
        return dp[odd][i][{gcd1, gcd2}] = mx;
    };

    cout << rec(0, (n + 1) >> 1, n >> 1, -1, -1, rec) << endl;
    return;
}

signed main() {
    ios::sync_with_stdio(false); cin.tie(0);
    int tc = 1;
    cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case " << t << ": ";
        solve();
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1076 Even Odd GCD (Easy Version)
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-23 09:34:51
Judged At
2024-11-11 03:05:47
Judged By
Score
100
Total Time
773ms
Peak Memory
2.277 MiB