/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 10ms 532.0 KiB
#3 Accepted 8ms 648.0 KiB
#4 Accepted 21ms 696.0 KiB
#5 Accepted 19ms 700.0 KiB
#6 Accepted 18ms 720.0 KiB
#7 Accepted 18ms 704.0 KiB
#8 Accepted 19ms 712.0 KiB
#9 Accepted 14ms 532.0 KiB
#10 Accepted 3ms 532.0 KiB
#11 Accepted 4ms 532.0 KiB
#12 Accepted 18ms 692.0 KiB
#13 Accepted 2ms 532.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

int n;
vector<int> v;
unordered_map<long long, int> memo;

long long encodeKey(int index, int count, int current_gcd)
{
    return ((long long)index * 1000000LL + (long long)count * 1000LL + current_gcd);
}

int fun(int index, int count, int current_gcd, const vector<int> &A, int N)
{
    if (count == 0)
    {
        return current_gcd;
    }
    if (index == N)
    {
        return -1;
    }
    long long key = encodeKey(index, count, current_gcd);
    if (memo.find(key) != memo.end())
    {
        return memo[key];
    }
    int option1 = fun(index + 1, count, current_gcd, A, N);
    int new_gcd;
    if (current_gcd == 0)
    {
        new_gcd = A[index];
    }
    else
    {
        new_gcd = gcd(current_gcd, A[index]);
    }
    int option2 = fun(index + 1, count - 1, new_gcd, A, N);
    int ans = max(option1, option2);
    memo[key] = ans;
    return ans;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> n;
    v.resize(n);
    for (auto &x : v)
    {
        cin >> x;
    }
    vector<int> result(n + 1, 0);
    for (int k = 1; k <= n; k++)
    {
        memo.clear();
        int cc = fun(0, k, 0, v, n);
        cout << max(0, cc) << " ";
    }
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1151 Max gcd group
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 15:05:10
Judged At
2025-01-02 15:05:10
Judged By
Score
100
Total Time
21ms
Peak Memory
720.0 KiB