/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 344.0 KiB
#2 Accepted 92ms 536.0 KiB
#3 Time Exceeded ≥2083ms ≥532.0 KiB
#4 Time Exceeded ≥2081ms ≥8.316 MiB

Code

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

ll gcd_(ll a,ll b){
    return b==0? a: gcd_(b,a%b);
}

ll find_min(ll pos,ll n,ll k,vector<ll>&arr){
    vector<ll> dp(n+1,-1);
    if(pos==n)return 0;
    if(dp[pos]!=-1){
        return dp[pos];
    }

    ll min_cost=LONG_LONG_MAX;

    if(pos+1<=n){
        min_cost=min(min_cost,k+find_min(pos+1,n,k,arr));
    }

    for(ll j=pos+1; j<=n; j++){
        ll cost=arr[pos-1]/gcd_(arr[pos-1],arr[j-1]);
        min_cost=min(min_cost,cost+find_min(j,n,k,arr));
    }

    return dp[pos]=min_cost;
}
 
void solve(){
    ll n,k;
    cin>>n>>k;
    vector<ll> arr(n);
    for(ll i=0; i<n; i++)cin>>arr[i];

    ll res=find_min(1,n,k,arr);
    cout<<res<<endl;
}
int main(){
    int t;
    cin>>t;
    while(t--){
        solve();
    }
return 0;
}

Information

Submit By
Type
Submission
Problem
P1099 Which way to go
Contest
Brain Booster #6
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-03 16:59:29
Judged At
2024-12-17 11:33:37
Judged By
Score
10
Total Time
≥2083ms
Peak Memory
≥8.316 MiB