tags:

views:

202

answers:

6

A class went to a school trip. And, as usually, all N kids have got their backpacks stuffed with candy. But soon quarrels started all over the place, as some of the kids had more candies than others. Soon, the teacher realized that he has to step in: "Everybody, listen! Put all the candies you have on this table here!"

Soon, there was quite a large heap of candies on the teacher's table. "Now, I will divide the candies into N equal heaps and everyone will get one of them." announced the teacher.

"Wait, is this really possible?" wondered some of the smarter kids.

Problem specification

You are given the number of candies each child brought. Find out whether the teacher can divide the candies into N exactly equal heaps. (For the purpose of this task, all candies are of the same type.)

Input specification

The first line of the input file contains an integer T specifying the number of test cases. Each test case is preceded by a blank line.

Each test case looks as follows: The first line contains N : the number of children. Each of the next N lines contains the number of candies one child brought.

Output specification

For each of the test cases output a single line with a single word "YES" if the candies can be distributed equally, or "NO" otherwise.

Example

Input:

2

5
5
2
7
3
8

6
7
11
2
7
3
4

Output:

YES
NO

The problem is simple but the case is that SPOJ judges are using very very large inputs. I have used unsigned long long as datatype, yet it shows wc..

Here's my code:

#include<iostream>
using namespace std;
int main()
{
    unsigned long long c=0,n,k,j,testcases,sum=0,i;
    char b[10000][10];
    cin>>testcases;
    while(testcases-->0)
    {
        sum=0;
        cin>>n;
        j=n;
        while(j-->0)
        {
            cin>>k;
            sum+=k;
        }
        if(sum%n==0)
        {
            b[c][0]='Y';b[c][1]='E';b[c][2]='S';b[c][3]='\0';
            c++;
        }
        else
        {
            b[c][0]='N';b[c][1]='O';b[c][2]='\0';
            c++;
        }
    }
    for(i=0;i<c;i++)
        cout<<"\n"<<b[i];
    return 0;
}
+1  A: 

If you have input that is larger than unsigned long long, then they probably want you to implement custom functions for arbitrary-precision arithmetic (or the problem can be solved without using the large integers). If the input fits the largest native integer type, but your algorithm requires larger integer, it's most likely time to think about a different algorithm. :)

Lukáš Lalinský
Given the simple nature of the assignment, it would seem odd to me that it would involve arbitrary-precision arithmetic.
David Thornley
I didn't mean this specific problem in answer. I wanted to cover all cases that might happen in such programming contest problems. This problem falls into the second part => use a different algorithm.
Lukáš Lalinský
+2  A: 

Does a line like this not concern you?

b[c][0]='Y';b[c][1]='E';b[c][2]='S';b[c][3]='\0';

Would it not be simpler to write??

strcpy(b[c], "YES");
abelenky
yet both of them would do the same..
vaibhav
Wouldn't it be easier just to directly output the string? There are only two choices...
Andrew Song
It would be even simpler to write `cout << "YES\n";`
Lukáš Lalinský
we don't have to instatnly give "yes" or "no".. we have to store it for every input and display all at the end.. its necessary to store our answers..
vaibhav
@vaibhav: do you? I wonder. The spec doesn’t say so and it would be quite unorthodox. So I’m inclined to just say no, you’ve misunderstood the specs.
Konrad Rudolph
I'm pretty sure you don't -- so long as you're not outputting other things in between your answers (you shouldn't be), they will appear in order as desired by the spec.Also, are you prohibited from using the std::string type with all its associated functions? That would seem more direct, especially for an easier question like this.
Andrew Song
@konrad: sir its has always been a case in SPOJ problems that we have to take all our inputs and then show the result..
vaibhav
@vaibhav Why not an array of std::string?
Andrew Song
@vaibhav: I just submitted a solution for this problem where I printed YES or NO in each iteration and it was accepted, so yes, you can do that.
Lukáš Lalinský
okk.. thanks for this information sir..
vaibhav
+3  A: 

You can do this question without summing all the candies. Just calculate the remainder off each child's heap (which will be smaller than N). This way, the number won't grow too large and overflow.

I won't write out a solution since this is a contest problem, but if you're stuck I can give some more hints.

Andrew Song
+2  A: 

Easy. Don't add up the number of candies. Instead, keep a count of kids, a count of candies per kid. (CCK), and a count of extra candies (CEC. When you read a new line, CK += 1; CEC += newCandies; if (CEC > CK) CCK += (CEC / CK); CEC %= CK;

MSalters
I don't think you need a count of candies per kid (which might be another source of overflow). Maintaining the number of 'extra' candies after each child is sufficient.
Andrew Song
and what should be the datatypre of ck,cec ?? should it be unsigned long long?
vaibhav
Whatever it needs to be, really. I wouldn't be surprised if it worked with an unsigned int, though.
Andrew Song
Actually, the number of candies per kid is an average. As such, it's never larger than the largest single contribution. So, if no kid brings more than `UINT_MAX` candies, then the average will also be less then `UINT_MAX`.
MSalters
A: 

If you're reading in from cin, you can only read in values that will fit into some sort of integer variable. It's possible that the sum would overflow.

However, you don't have to add the numbers up. You can add the remainders (from dividing by N) up, and then see if the sum of the remainders is N.

David Thornley
A: 

using namespace std;

this is C or C++?

Arabcoder
namespaces are used in c++, they were added a few years back to localize the names of identifiers to avoid name collisions.
vaibhav