Questionnaire

来源:互联网 发布:c语言中的ifdef ifndef 编辑:程序博客网 时间:2024/05/16 10:41

Questionnaire

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 742    Accepted Submission(s): 509
Special Judge

Problem Description
In order to get better results in official ACM/ICPC contests, the team leader comes up with a questionnaire. He asked everyone in the team whether to have more training.



Picture from Wikimedia Commons


Obviously many people don't want more training, so the clever leader didn't write down their words such as ''Yes'' or ''No''. Instead, he let everyone choose a positive integerai to represent his opinion. When finished, the leader will choose a pair of positive intergesm(m>1) and k(0k<m), and regard those people whose number is exactly k modulo m as ''Yes'', while others as ''No''. If the number of ''Yes'' is not less than ''No'', the leader can have chance to offer more training.

Please help the team leader to find such pair of m and k.
 

Input
The first line of the input contains an integer T(1T15), denoting the number of test cases.

In each test case, there is an integer n(3n100000) in the first line, denoting the number of people in the ACM/ICPC team.

In the next line, there are n distinct integers a1,a2,...,an(1ai109), denoting the number that each person chosen.
 

Output
For each test case, print a single line containing two integersm and k, if there are multiple solutions, print any of them.
 

Sample Input
1623 3 18 8 13 9
 

Sample Output
5 3
 

题意:

     给定n个数,找出一对数m,k,使得这n个数模上m等于k的个数大于等于模上m不等于k的个数。若有多组m,k符合题意,仅输出一组m,k即可。


思路:

     一开始想的是找到这n个数中最大的数,遍历从2到这个数,找到符合题意的m,然后再优化,后来发现题目有更简单的做法。关键在于如果有多组m,k符合题意,仅输出一组即可。由题目可得,我们需找到一组m,k,然后把数组分成两部分,一部分是模m等于k的,一部分是模m不等于k的。我们不必求哪一组m,k使得结果最大,只需求一组符合题意的m,k即可。假设我们找到了一个数m,然后我们就要从余数1,2,3…m-1中找出一个符合题意的k,比较余数为k和其他余数所对应的个数是否符合题意,这样就把余数分为两个部分,k,和k之外的。由此我们可以想到,任何数除以2余数都只有两种情况,0或1,再加上此题不求最大,而且只求一组数,所以我们可以得出所有的例子都可以取到m=2,只需要看k=0和k=1时谁对应的数最大即可。由此题目简化。


下面贴上代码:

#include<cstdio>using namespace std;const int maxn=1e5+10;int a[maxn];int main(){    int T,n;    scanf("%d",&T);    while(T--)    {        scanf("%d",&n);        int num=0;        for(int i=0; i<n; i++)        {            scanf("%d",&a[i]);            if(a[i]%2==1)                num++;        }        if(num>=n-num)            printf("2 1\n");        else            printf("2 0\n");    }    return 0;}