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
https://cses.fi/paste/ba9b8b98433cf5055cb26d/
this ac
Either paste the link for source code or use the "block code" feature properly.
https://ideone.com/0IGOww
enjoy
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.