why the vectormap work and mapvector doesn't? [question]
Difference between en1 and en2, changed 18 character(s)
I was trying this [question](https://atcoder.jp/contests/abc275/tasks/abc275_d)↵

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";↵
}↵
~~~~~↵



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)