Madara__Uchiha__'s blog

By Madara__Uchiha__, history, 4 years ago, In English
  • Vote: I like it
  • +4
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

you can solve it with a one dimension dp array like this

first set the whole array to -inf execpt dp[0] = 0
for(int i = 2; i < 10001;++i)
for(int x : v) // v is the vector that hold prime numbers and primatic numbers
{
if(i — x < 0)continue;
dp[i] = min(dp[i], dp[i — x] + 1);
}
answer = dp[input]

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

With N = 9973 (the largest prime below 10 000), your code prints 0.

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

You aren't using last prime number v[m - 1] in your dp loop, because you are going from 1..m-1 (for i), but then you are using v[i - 1] in calculation, so in reality you are using 0..m-2 prime numbers.

Second problem is array overflow, because in your for(;l<v.size();l++) for number like 10000 you will end up with l == m, which doesn't with your dp declaration.

To fix it change int dp[m][10001] to int dp[m+1][10001] and also your dp loop from for(int i=0;i<m;i++) to for(int i=0;i<=m;i++). That being said, you don't need 2 dimensional array, as pointed out by Mohamed_Saad62