Can anyone please figure out why i am getting WA on Test 2 for the following code:

Problem Link- Problem - A - Codeforces

Solution - Submission #77786339 - Codeforces

This was Yesterday’s Codeforces Contest (Div.2)

Can anyone please figure out why i am getting WA on Test 2 for the following code:

Problem Link- Problem - A - Codeforces

Solution - Submission #77786339 - Codeforces

This was Yesterday’s Codeforces Contest (Div.2)

Thanks for the reply @aaditya_singh , but if i removed the equality sign

Then this condition will get false whichc was given in the question as follows:

c-d<= (sum of all x[i]) <= c+d

suppose the value came c-d then according to my code answer will be No but acc. to question its Yes.

And if i iterated that means i am checking all possible answers so why i gotb WA

Consider

n=10,a=2,b=1,c=15, d=3

1 Like

My Output is No but i know that output should be Yes` (8*2 + 2*1).`

Yes. It’s actually very easy to check

```
void solve(){
int n, a, b, c, d;
cin>>n>>a>>b>>c>>d;
int maxgrain=n*(a+b);
int mingrain=n*(a-b);
int maxval=c+d;
int minval=c-d;
if(maxgrain < minval || mingrain>maxval){
cout<<"No\n";
}
else{
cout<<"Yes\n";
}
}
```

2 Likes

oh so sorry i thought it was like mineYour if condition is wrong i guess,the question doesn’t says that all the n grains will be of the same weight so you maybe missing some of the cases which gives answer no instead of yes.try thinking negatively

1 Like

Thank you So much @everule1 for your kind reply!!

But suppose if i want to check for every possible condition how can i do that ?

For ex- `10 2 1 15 3`

Then how should i code to get every possible case like` (8*2)+(2*1)`

It is probably too slow for the constraints, but you can check all values from n*(a-b) to n*(a+b)

1 Like

Thanks @aaditya_singh for your reply.

Yes,but i want to know how can i code to compute every possible case instead of doing this?

yes i know its too slow but i am not saying this @everule1 .

I was asking if we can check to give different weights to every grain and then check the condition.

like your test case: `n=10,a=2,b=1,c=15,d=3`

weight of 10 grains= 2+2+2+2+2=2+2+2+1+1

Do you know numbers in base x? Like 12 is 22 in base 5.

1 Like

You can iterate from 0 to (2b+1)^n, and the ith digit can represent the difference of the weight of the ith grain from a-b.

1 Like

Can you explain it with an example and it would be grateful if you would explain what you said earlier too.

(“Do you know numbers in base x? Like 12 is 22 in base 5.”)

@everule1

Isn’t this overcomplicating a simple problem?

1 Like

12 = 2*(5^1) + 2*(5^0) = 22 in base 5

1 Like

wa because you are checking only boundary values . however, for a case where

n*(a-b) > (c-d) is false ,but the condition could be made true by adding a few valid grams ,this logic would fail. same for n*(a+b)<(c+d) , we can deduce a few grams.

```
int n,a,b,c,d;
cin>>n>>a>>b>>c>>d;
if((n*(a-b))>(c+d) || (n*(a+b))<(c-d)){
cout<<"No"<<"\n";
}
else{
cout<<"Yes"<<"\n";
}
```

1 Like