/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Accepted 1ms 540.0 KiB
#3 Accepted 1ms 372.0 KiB
#4 Accepted 1ms 540.0 KiB
#5 Accepted 1ms 772.0 KiB
#6 Wrong Answer 1ms 540.0 KiB
#7 Accepted 1ms 540.0 KiB
#8 Accepted 1ms 540.0 KiB
#9 Accepted 1ms 540.0 KiB
#10 Accepted 1ms 540.0 KiB
#11 Accepted 1ms 568.0 KiB
#12 Wrong Answer 1ms 540.0 KiB

Code

#include <bits/stdc++.h>
using namespace std;
 
using ll = long long;
const ll INF = 1LL << 60; // a large number
// We'll set T_max a bit higher than needed; max recursion depth ~ 20.
const int T_max = 22;
 
// Precompute prime factors for numbers in [X, X+T_max].
vector<vector<int>> precomputeFactors(ll X) {
    vector<vector<int>> factors(T_max+1);
    for (int t = 0; t <= T_max; t++) {
        ll Y = X + t;
        ll temp = Y;
        for (ll p = 2; p*p <= temp; p++) {
            if(temp % p == 0) {
                factors[t].push_back(p);
                while(temp % p == 0) temp /= p;
            }
        }
        if(temp > 1) {
            factors[t].push_back((int)temp);
        }
    }
    return factors;
}
 
// For a given a and offset t, compute cost1(a, X+t).
ll cost1(ll a, int t, const vector<vector<int>> &factors) {
    ll ans = INF;
    for (int p : factors[t]) {
        ll r = a % p;
        ll add = (r == 0 ? 0 : p - r);
        ans = min(ans, add);
    }
    return ans;
}
 
// Structure for our game tree node.
struct Node {
    int L, R;
    Node *left = nullptr, *right = nullptr;
    // dp[t]: minimum operations to make the segment (with parameter X+t)-ordinary.
    // cost[t]: cost to fix every element in the segment so that gcd(element, X+t) > 1.
    array<ll, T_max+1> dp;
    array<ll, T_max+1> cost;
};
 
// Global array and factors (for base cost computation).
vector<ll> A;
 
// Build the tree using the fixed splitting rule for indices [L, R] (0-indexed).
Node* buildTree(int L, int R, const vector<vector<int>> &factors, ll X) {
    Node *node = new Node();
    node->L = L; node->R = R;
    int n = R - L + 1;
    if(n == 1) {
        // Leaf node: for each t compute cost1(A[L], X+t)
        for (int t = 0; t <= T_max; t++) {
            ll c = cost1(A[L], t, factors);
            node->dp[t] = c;
            node->cost[t] = c;
        }
        return node;
    }
    // Determine the split:
    int k = n / 2; // first half size = floor(n/2)
    int mid = L + k; // left: [L, mid-1], right: [mid, R]
    node->left = buildTree(L, mid - 1, factors, X);
    node->right = buildTree(mid, R, factors, X);
    // Combine children:
    for (int t = 0; t <= T_max; t++) {
        // The cost to fix the whole segment for parameter X+t is the sum over children.
        node->cost[t] = node->left->cost[t] + node->right->cost[t];
    }
    // For dp[t], we need dp[t+1] from children.
    for (int t = 0; t <= T_max; t++) {
        if(t == T_max) {
            // Not enough parameter "budget" for recursive call.
            node->dp[t] = INF;
        } else {
            ll option1 = node->left->dp[t+1] + node->right->cost[t];
            ll option2 = node->right->dp[t+1] + node->left->cost[t];
            node->dp[t] = min(option1, option2);
        }
    }
    return node;
}
 
// Free the allocated tree.
void freeTree(Node *node) {
    if(!node) return;
    freeTree(node->left);
    freeTree(node->right);
    delete node;
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N; 
    ll X;
    cin >> N >> X;
    A.resize(N);
    for (int i = 0; i < N; i++){
        cin >> A[i];
    }
    
    // Precompute factors for X, X+1, ..., X+T_max.
    vector<vector<int>> factors = precomputeFactors(X);
    
    // Build the game tree for the entire array [0, N-1].
    Node *root = buildTree(0, N-1, factors, X);
    
    // The answer is the minimum number of operations to make the whole array X-ordinary,
    // which is stored in root->dp[0].
    ll answer = root->dp[0];
    cout << answer << "\n";
    
    freeTree(root);
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1168 F. x ordinary array
Language
C++17 (G++ 13.2.0)
Submit At
2025-03-27 20:39:40
Judged At
2025-03-27 20:39:40
Judged By
Score
10
Total Time
1ms
Peak Memory
772.0 KiB