TWOGRS - Editorial

Will you please elaborate more simply.
Please.!

The tester’s solution is the clearest.

What is the logic behind while(a>=30) a-=2; (same for b and c) in testers solution

6 Likes

Can anyone please suggest a test-case where this solution is wrong. It is checking if the values of a,b,c are odd or even.

 `int main(){
    	int t,a,b,c;
    	int d,e,f;
    	cin>>t;
    	while(t--){
    		cin>>a>>b>>c;
    		d=a%2;
    		e=b%2;
    		f=c%2;
    		if((a+(b*2)+(c*3))%2!=0)
    			cout<<"NO"<<"\n";
    		else if( (d && e && f) || (!d && !e && !f ) )
    			cout<<"YES"<<"\n";
    		else if( !d && !f && e && a>0)
    			cout<<"YES"<<"\n";
    		else if( d && !e && f && b>0)
    			cout<<"YES"<<"\n";
    		else cout<<"NO"<<"\n";
    	}
    	return 0;
    }`

can someone explain in tester solution why he did while(a>=30)a=a-2; ??

4 Likes

This is the easiest I can explain guys -

1] If the sum(1*a+2*b+3*c==even) of all numbers is even, we check for something , else (Answer–>No).
(If sum is even, then only a set can be divided into 2-equal sets, agreed?)

2]If sum is even, and all a,b,c are positive. Answer is always Yes(you can try it yourself).

3)If sum is even and a=0 and c=0, just check the value of 'b'.

4)If sum is even, a=1 and c=odd number and b=0, answer is No.

5)If sum is even, b=1 and c=even number and a=0, answer is No.

6)For all others, answer is yes.

16 Likes

#include<bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t–)
{
int a,b,c,to,ans=0;
cin>>a>>b>>c;
to=a+2b+3c;
if(to&1)
ans=0;
else if((a+b+c)==1)
ans=0;
else if(a==0&&c==0&&(b&1))
ans=0;
else
{
if(a!=0)
{ to=to/2;
to=to-3min(c,to/3);
to=to-2
min(b,to/2);
if(to<=a)
ans=1;
else
ans=0;
}
else
{
to=to/2;
to=to-3*min(c,to/3);
if(to/2<=b)
ans=1;
else
ans=0;
}
}
if(ans==0)
cout<<“NO”<<endl;
else
cout<<“YES”<<endl;

}

}
Why i am not getting ac??? Pls suggest me.

Why is this approach wrong?

#include <bits/stdc++.h>
using namespace std;

//sums an array of size 3
int sum_arr(int arr[]){
return arr[0] + (2arr[1]) + (3arr[2]);
}

int main(){
int T,A,B,C;
cin>>T;
while(T–){
cin>>A>>B>>C;
int arr_1[3]{0};
int arr_2[3]{0};
arr_1[0]=max(A/2,A-A/2);
arr_1[1]=max(B/2,B-B/2);
arr_1[2]=max(C/2,C-C/2);
arr_2[0]=min(A/2,A-A/2);
arr_2[1]=min(B/2,B-B/2);
arr_2[2]=min(C/2,C-C/2);
int sum_1=sum_arr(arr_1);
int sum_2=sum_arr(arr_2);
int diff=sum_1 - sum_2;
if(diff%2==1) cout<<“NO\n”;
else{
if(diff==0)cout<<“YES\n”;
else if(diff==2) {
if(arr_1[0]>=1)cout<<“YES\n”;
else cout<<“NO\n”;
}
else if(diff==4){
if(arr_1[0]>=2 || arr_1[1]>=1)cout<<“YES\n”;
else cout<<“NO\n”;
}
else if(diff==6) cout<<“YES\n”;
else cout<<“NO\n”;
}
A=B=C=0;
}
return 0;

}

Sum is even ? Lol. I wish you best of luck with basic +/- in maths :slight_smile:
(1+2x5+3x4====odd)

my bad i thought u were saying sum of a b c

1 Like

here is my approach ,i check whether i can make the nearerst even or not
https://www.codechef.com/viewsolution/26747330

Here’s my approach :

The problem can be broken down into 8 cases depending on whether a, b, c are even or odd.

It is easy to see that if exactly one of a or c is odd, then the sum of all elements in the multiset will be odd. So, in these cases (4 of them), the answer would be NO.

Also, if all of a, b and c are even, the answer would be YES because we will always be able to take a/2, b/2 and c/2 elements in one subset forming equal sums.

The three cases that remain are :

[s := a + (2*b) + (3*c)]

  1. a is odd, b is odd, c is odd.
    Let a = 2k + 1,\ \ b = 2l + 1,\ \ c = 2m + 1
    s = 2k + 1 + 4l + 2 + 6m + 3
    s = 2k + 4l + 6m + 6
    \therefore\ \ s/2 = k + 2l + 3m + 3
    \therefore\ \ s/2 = \lfloor{a/2}\rfloor + (2*\lfloor{b/2}\rfloor) + (3*\lfloor{c/2}\rfloor )+ 3
    We only need to make sure that the additional 3 at the end can be formed.
    a' := a - \lfloor{a/2}\rfloor, \ \ b' := b - \lfloor{b/2}\rfloor, \ \ c' := c - \lfloor{c/2}\rfloor \ \
    3 can be formed iff a' \geq 3 \ \ || \ \ (a' \geq 1 \ \ \&\& \ \ b' \geq 1) \ \ || \ \ c' \geq 1.
    So, the answer would be YES if this is true. Else, the answer would be NO.

  2. a is odd, b is even, c is odd.
    Let a = 2k + 1,\ \ b = 2l,\ \ c = 2m + 1
    s = 2k + 1 + 4l + 6m + 3
    s = 2k + 4l + 6m + 4
    \therefore\ \ s/2 = k + 2l + 3m + 2
    \therefore\ \ s/2 = \lfloor{a/2}\rfloor + (2*\lfloor{b/2}\rfloor) + (3*\lfloor{c/2}\rfloor )+ 2
    We only need to make sure that the additional 2 at the end can be formed.
    a' := a - \lfloor{a/2}\rfloor, \ \ b' := b - \lfloor{b/2}\rfloor, \ \ c' := c - \lfloor{c/2}\rfloor \ \
    2 can be formed iff a' \geq 2 \ \ || \ \ b' \geq 1.
    So, the answer would be YES if this is true. Else, the answer would be NO.

  3. a is even, b is odd, c is even.
    Let a = 2k,\ \ b = 2l + 1,\ \ c = 2m
    s = 2k + 4l + 2 + 6m
    s = 2k + 4l + 6m + 2
    \therefore\ \ s/2 = k + 2l + 3m + 1
    \therefore\ \ s/2 = \lfloor{a/2}\rfloor + (2*\lfloor{b/2}\rfloor) + (3*\lfloor{c/2}\rfloor )+ 1
    We only need to make sure that the additional 1 at the end can be formed.
    a' := a - \lfloor{a/2}\rfloor, \ \ b' := b - \lfloor{b/2}\rfloor, \ \ c' := c - \lfloor{c/2}\rfloor \ \
    1 can be formed iff a' \geq 1
    So, the answer would be YES if this is true. Else, the answer would be NO.

Here’s the link to my code.
However, this received WA. Can someone help me figure out the problem here?

3 Likes

Look at my simple explanation.
@nihaljain

I think the approaches are equivalent. Yet I cannot find a test case that would fail my code.

You should first of all check, if the sum is even or not, if you don’t do that, you are bound to mess up few cases, as it was seen by so many Was during the contest :slight_smile:

The sum would be odd only in the first 4 cases (i.e. when exactly one of a or c is odd) that I reject immediately.

Can anyone pls explain the TESTER’s solution?

2 Likes

Can anyone please suggest me the mistake in my program. As, the testcases I am able to think gives the correct output, but instead it is showing wrong answer.
I will be very much thankful to the one who helps me…

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main(){
int t;
cin>>t;
while(t–){
ll a, b, c;
cin>>a>>b>>c;
ll sum = a1 + b2 + c*3;
if(sum%2!=0){
cout<<“NO”<<endl;
}
else if(a==0&&b==0&&c==0){
cout<<“NO”<<endl;
}
else if(c%2==0){
if(a%2!=0){
cout<<“NO”<<endl;
}else{
if(b%2!=0){
if(a<2){
cout<<“NO”<<endl;
}else{
cout<<“YES”<<endl;
}
}else{
cout<<“YES”<<endl;
}
}
}else{
if(a%2==0){
cout<<“NO”<<endl;
}else{
if(b%2==0){
if(a>=3){
cout<<“YES”<<endl;
}else{
cout<<“NO”<<endl;
}
}else{
cout<<“YES”<<endl;
}
}
}
}

return 0;

}

Also please tell me the case for this it doesn’t work correctly…

Check my editorial for this problem here :- Unofficial editorial-TWOGRS

2 Likes

can anyone please tell me what is the mistake in my code.
#include<stdio.h>
int main()
{
int t,T;
long int A,B,C;
long long int sum1,sum2;
scanf("%d",&T);
for(t=0;t<T;t++)
{
scanf("%ld %ld %ld",&A,&B,&C);
sum1=sum2=A1+B2+C3;
if(sum1%2!=0)
printf(“NO\n”);
else
{
sum1=sum1/2;
while(sum1>0)
{
if(sum1==1 && A>0)
{
sum1-=1;
A-=1;
}
else if(sum1==2 && B>0)
{
sum1-=2;
B-=1;
}
else if(sum1==3 && C>0)
{
sum1-=3;
C-=1;
}
else
{
if(A>0)
{
sum1-=1;
A-=1;
}
else if(B>0)
{
sum1-=2;
B-=1;
}
else if(C>0)
{
sum1-=3;
C-=1;
}
else
break;
}
}
sum2=sum2/2;
if(sum1==0)
{
if((1
A+2B+3C) == sum2)
printf(“YES\n”);
else
printf(“NO\n”);
}
else
printf(“NO\n”);
}
}
return 0;
}