JUDASS's blog

By JUDASS, history, 19 months ago, In English

This is problem link https://cses.fi/problemset/result/5982212/

include <bits/stdc++.h>

using namespace std; const int N=511; vector<vector> dp(N, vector(N, -1));

int find(int a, int b){ if(a == b) return 0; if(dp[a][b] != -1) return dp[a][b]; int A=1e9, B= 1e9; for(int x=1; x<a; ++x) { A=min(A, find(x,b)+find(a-x, b)+1); } for(int y=1; y<b; ++y) { B=min(B, find(a,y)+find(a,b-y)+1); } return dp[a][b]=min(A, B); } int main() { int a, b; cin>>a>>b; cout<<find(a, b);

return 0;

} My code is getting TLE in two cases while it's is under 10^4 iteration

  • Vote: I like it
  • -6
  • Vote: I do not like it

»
19 months ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it
»
19 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Either paste the link for source code or use the "block code" feature properly.

»
19 months ago, # |
  Vote: I like it +1 Vote: I do not like it

Time Complexity of your code is — O(N*N) for recursion and O(2*N) for inner 2 loops.

So overall TC — O(N*N*2N) = O(2N^3) => 2*125*1000000 => 2.5 * 10^8.

That's why, you are getting TLE.