Hello Codeforces, I came across an interesting error while solving this problem : https://mirror.codeforces.com/contest/1950/problem/G which I am not able to understand as to why this is happening?
https://mirror.codeforces.com/contest/1950/submission/255101098 (This solution did not get accepted but gave runtime error) https://mirror.codeforces.com/contest/1950/submission/255101528 (This solution got accepted)
Note there is very little difference between the two
In the first solution I have initialized vector as : vector<vector<int>>dp(n,vector<int>((1<<n),-1));
In the second solution I have initialized vector as : vector<vector<int>>dp((1<<n),vector<int>(n,-1));
I really don't understand what is causing this error. Any help would be appreciated. Thank you !
even GPT knows about it
Why would it give out of bounds error? Please see the code properly
vector<vector>dp(n,vector(**(**1<<n,-1**)**)); remove those brackets
Actually, like what the above commenter has said, you declare the vector in the first sol as
vector<vector<int>>dp(n,vector<int>((1<<n,-1)));
, instead ofvector<vector<int>>dp(n,vector<int>((1<<n),-1));
. While the difference here is subtle, it is still a fatal error, as you have accidentally declaredvector<int>((1<<n,-1));
which makes the compiler very confused and essentially terminate themselves. Why? Because the first argument you put is not an integer (in fact, it is not any STL data structure at all).Moral of the story: watch out for bracket placement, as they are the reason some codes basically gives WA or RTE.
There is nothing confusing compiler. This line is valid and compiled. The only issue is that (1 << n, -1) is evaluated as two expressions separated by comma, from left to right (check out comma operator). Then the result of the first expression 1 << n is discarded since it's not used. The result of the second expression is used which is -1 so this means vector is initialized with size of -1. There is noting wrong to create a vector with size -1 actually you can see it's complied successfully. But obviously accessing a negative size vector throws run time exception.
well, the more i know i suppose, thx m8 <3