why the vector work and map doesn't? [question]

Revision en1, by ErenZ, 2023-12-27 15:45:32

I was trying this question

and for some reason that i dont understand my code doesn't work that is following.

int rec(int n, vector<int>&v){
    if(n == 0){
        return 1;
    }
    
    if(v[n] != -1){
        return v[n];
    }
    return v[n] = rec(floor(n/2),v) + rec(floor(n/3),v);
}

void solve() {
    read(n);
    vector<int>v(n+1,-1);
    cout<<rec(n,v)<<endl;
  
}

but the following code which has the same logic works.

map<ll, ll> mp;
ll calc(ll x) {
    if (x == 0)return 1;
    if (mp.find(x) != mp.end())return mp[x];
    ll res = calc(x / 2) + calc(x / 3);
    return mp[x] = res;
}
void solve() {
    ll n; cin >> n;
    cout << calc(n) << "\n";
}
Tags maps, vector

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English ErenZ 2023-12-27 15:46:18 18
en1 English ErenZ 2023-12-27 15:45:32 855 Initial revision (published)