Author: Praveen Dhinwa Difficulty:Cakewalk PreRequisites:None Problem StatementThere are $C$ cats and $D$ dogs, and $L$ legs touching the ground. Some of the cats can ride on dogs, but every dog can't have more than 2 cats on his back. Can this be true? ExplanationLet's make some obvious observations:
If we have $X$ cats and $Y$ dogs staying on the ground then, the number of legs in the barn equal $4 * (X+Y)$. Therefore if $L$ not divisible by 4, the answer is "no". Subtask 1 and 2Constraints are chosen in such way that solutions with complexity $O(D+C)$ per test case can pass. Iterate over possible numbers of the cats on Chef's dogs back $G$($G$ must be in the range between $0$ and $2*D$ due to the condition of the dog and 2 cats on his back, and not more than the total number of cats). Hence in the barn $4*(CG+D)$ legs on the ground, if $4*(CG+D) = L$ for some $G$, then the answer is "yes", and "no" otherwise. Subtask 3There is possible to solve problem with $O(1)$ solution per test case. Let $G$ number of the cats on the backs of the dogs, $0 ≤ G ≤ min(C,2*D)$ $4*(CG)+4*D = L $, there are $CG$ cats on the ground, therefore total number of legs = $4*(CG)$+$4*D$ $CG+D = L/4 $, divide both parts of the equation by $4$ $C+DL/4 = G $, add $GL/4$ to both parts of the equation if $G$ will be in the range between $0$ and $2*D$ answer is "yes", and "no" otherwise. The overall time complexity of this approach is $O(1)$ per test case. Solution:Setter's solution can be found here Please feel free to post comments if anything is not clear to you.
This question is marked "community wiki".
asked 18 Jan '17, 04:02

can somebody upvote my answer I need to ask a question and needed atleast 3 karma for that? help is appreciated.thanks answered 21 Feb '17, 17:02

import java.util.Scanner; / * @author Ashutosh / class CATSDOGS {
} Can anyone please check what's wrong in this code. I got 0 marks, but i am not getting where I am wrong. Can anyone please guide me. answered 25 Jan '17, 13:22
You've calculated wrong min, learn to debug your code. ll mnl=d4; if(c>d2) mnl=(ll)((ll)(c(ll)(d2))4 + mnl);
(25 Jan '17, 19:06)

Please try to insert comments as such to tell what exactly you are doing in the code. Here, check my code in C https://www.codechef.com/viewsolution/12439335
I am unable to get why you used "String[] answer = new String[nTestCases];" Was it to store and print answer? Here is the quick algo I used. First we check the number of cats and dogs. Now we need to find which is less.
If number of cats is less than 2number of dogs (i.e. all cats can still ride on top of dogs) we Follow as in step 1. However, if cats are more than 2dogs, we find minimum number of cats which will remain on ground first (assuming all dogs carry 2 cats[we are finding minimum]). This is clearly cats2 dogs. Eg Dogs 4, Cats =10. Then Dogs can carry 8 cats, and 108=2 will remain. Now the MINIMUM number of legs is 4number of Dogs + minimum number of cats, the maximum again is number of cats+dogs. The number L should be divisible by 4. NOW THE TRICKY PART The number of C, L and D in final subtast are REALLY LARGE. DO NOT...I REPEAT DO NOT USE INT TO STORE THESE. YOU WILL GET AN OVERFLOW!!! I used long long int in C. Feel free to ask doubts/get back to me. :) answered 25 Jan '17, 13:42

import java.util.Scanner; public class Main {
} See this is also, giving error. answered 25 Jan '17, 16:12

Firstly, I want to ask that did you try running it in "Code, Cmpile and Run" Feature of codechef? If not, then I strongly advise you to! Cause online IDEs are very much different in handling exceptions and etc. Also, I run your code, and the first glaring error found was that "while (nTestCases != 0)" PS, its NOT same as doing it in for loop. Here it first REDUCES the value and THEN compares. So if the number of test cases are 3, then it will execute loop as (3) !=0 ==> 2!=0 Then 1!=0 in next iteration Then in final iteration 0!=0 will be false. Note that your program iterated only twice, instead of 3 as required by the test cases. Suggested fixes : 1. Use a for loop. OR 2. try (t !=0) or simply (t) since when it reaches 0, loop assumes false. Secondly, check for large numbers. In Code, Compile and Run feature, check by inputting the max input (10^9 ) and CHECK the values of C, L and D by asking program to print. In my case, due to overflow I got some weird negative value like 168734 &etc. That's the second catch in this problem. Edit Thirdly, if as I suspect, your code suffers from  "Exception in thread "main" java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at Main.main(Main.java:9) " Then do check here http://stackoverflow.com/questions/7209110/javautilnosuchelementexceptionnolinefound The error is due to, (as quoted by them), You're calling nextLine() and it's throwing an exception when there's no line, exactly as the javadoc describes. It will never return null My suggestion I always used int a=sc.nextInt(); etc. Try this one, I am sure it will work ^^ Try the three of these and let me know. If error still occurs, we will have a closer look. :) Cheers, have a nice day ^^ answered 25 Jan '17, 16:27

its in this weird way because you pasted it beside "enter your code" . What you have to do is, copy your code, select "enter your code" and then paste (overwriting "enter your code" with your code) and it'd be properly formatted. As for your code Why its failing Firstly, please give a submission link if possible, or give more details. You are NOT suffering from wrong answer, you are suffering from TIME OUT (program is taking TOO long) and its obvious why. Look dear, test cases can vary from 1 to 10,000. And number of cats and dogs till 10^9. Problem with your code is that, while it accurately tells about number of cats on dogs etc, its not required. You simply need to tell if the counting of legs is possible or not, and NOT the configuration (like, how many dogs were on cats and stuff). Analyse your while loop which calculates cats on dogs. lets say, cats are 25000, and dogs are 10,000. Now, until you get ((4 * (Cats  catsOnDogs) + (4 * Dogs)) == Legs) true, (which in worst case, will be false), your loop will execute. In worse case, it will execute 20,000 times. If numbers are bigger, it may have to execute 10, 100 or thousand times more!! Now repeat this for all 10,000 test cases. Automatically, number of loop executions increase so much, that the program takes more than allotted time to run. How to fix? The problem is possible by direct computation. First, find minimum number of legs possible (assume 2 cats on dog) and maximum (4*(cats+dogs)) Minimum Legs might get tricky if cats are more than dogs. Eg cats=10, dogs=2, legs =36 4 cats may climb on dog. ==>cats=6, dogs=2, total 8. Minimum Legs=8*4=32. Now if 1 cat is NOT on dog, we get 36. If 2 cats and 2 dogs, minimum is obviously 2 (number of dogs assuming all cats on dogs). Minimum legs = 2*4=8 Now we have maximum and minimum. Now see, for EVERY CAT NOT ON DOG, we can add 4 to minimum and get new count of legs. What I mean to say is that, by this statement we can clearly see that legs = minimum +4*k (where k is an integer) [incase you still didn't understand. In minimum we assumed all cats on dogs. Lets say 1 cat is not on dog, leg count increases by 4. If 2 cats not on 4, leg count increases by 8 and so on. Meaning, there is a gap of 4 between successive valid leg counts. Also, since min and max legs are divisible by 4, the leg count is divisible by 4) Now, how to determine if leg count is correct or not? Firstly, it must lie between minimum and maximum (BOTH INCLUSIVE) Then, it MUST be divisible by 4. (eg, min32 and max =56. Then all leg counts like 32,36,40,44..56 are valid.) In case you face any problem, do get back to me! Hope it helps! Cheers ^_^ answered 27 Jan '17, 11:27

int main() {
scanf("%d",&t); for(i=1;i<=t;i++) {
} } answered 27 Jan '17, 19:45

Pls check your print statements. The question asked to print "yes" or "no" and your program is printing "Yes" and "No" (Y and N in capital!!). It will result in the answer being marked wrong by the online judge. I hope it helped! ^_^ answered 27 Jan '17, 19:54

include<stdio.h>int main() { long unsigned c,d,l,t,max,min,flag,i; scanf("%lu",&t); while(t>0) { flag=0; scanf("%lu %lu %lu",&c,&d,&l); max=4c+4d; min=4*d; for(i=min;i<=max;i+=4) { if (l==i) { flag=1; } } if(flag==1) { printf("yes"); printf("\n"); } else { printf("no"); printf("\n"); } t; }
} 1. List item want to know what is wrong with this code ...just it is passing only subtask 2. IS it related with time complexity... answered 19 Feb '17, 12:17

include<stdio.h>int main() { long unsigned c,d,l,t,max,min,flag,i; scanf("%lu",&t); while(t>0) { flag=0; scanf("%lu %lu %lu",&c,&d,&l); max=4c+4d; min=4*d; for(i=min;i<=max;i+=4) { if (l==i) { flag=1; } } if(flag==1) { printf("yes"); printf("\n"); } else { printf("no"); printf("\n"); } t; }
} 1. List item want to know what is wrong with this code ...just it is passing only subtask 2. IS it related with time complexity... answered 19 Feb '17, 12:18

please can some one of you tell me where is the bug because it works for me but when I subbmit the answer is wrong. the code is in C:
} main(){ int i; int T; test * t; scanf("%d",&T); t= (test )malloc(Tsizeof(test)); for(i=0;i<T;i++){ scanf("%d %d %d",&t[i].c,&t[i].d,&t[i].l); } for(i=0;i<T;i++){ stats(t[i]); } } int i; int T; test * t; scanf("%d",&T); t= (test )malloc(Tsizeof(test)); for(i=0;i<T;i++){ scanf("%d %d %d",&t[i].c,&t[i].d,&t[i].l); } for(i=0;i<T;i++){ stats(t[i]); } } answered 21 Feb '17, 16:22

@magime The constraints must be stored in long long int. In int, they will overflow and you will fail in final sub task. Check your minimm calculation, I wasn't able to check it thoroughly in that code you posted , so please do a self check and see if its equal to what is given in editorial (PS min and overflow were 2 main culprits behind this problem having low accuracy rate despite easy nature) answered 21 Feb '17, 18:06

import java.util.*; class Catsdogs{
} } This code works fine on other compiler giving required output but code chef is giving error. Please help !! answered 21 Mar '17, 13:40

@mihir1440 Your code throws out of index error whenever T=1
I strongly suspect that it is due to wrong allocation of memory to cnt[][]. Please have a look at it. answered 21 Mar '17, 17:51

What I'm doing is this:
To my surprise, it doesn't work. It gives a straight WA. Where did I go wrong?
} Any help? PS: It works fine with the given test case! answered 08 Apr '17, 17:43

Your calculation of min is only partially correct. Its correct if number of cats are way more than number of dogs, but what if number of cats are less than number of dogs? In a scenario when all cats can be on top of dogs, the min is not 4x(cd), but its simply 4xd. Visualise it, when all cats on top of dogs, minimum legs is equal to number legs of dogs. Your case fails at this test case
Your min here is 4x(13) = 8. While practically we can see that min should be 4*3=12. answered 08 Apr '17, 18:34

https://ideone.com/VWl7gD Can someone tell why i am failing my testcases answered 08 Nov '17, 13:24
