开发者

Is there any trick to handle very very large inputs in C++?

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;
}


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;


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");


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.


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. :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜