描述
Changlong is a handsome and clever boy and he is admired by many beautiful girls. Every day he received tens of thousands of letters form girls which express their intense desire to be his life companion. You see, Changlong is very tired of this and one day he get an idea to avoid this bother. He declares:
Only those clever girls who can solve the problem below have the qualification to be his wife.
Changlong’s problem can be formulated like this:
There is one interesting sequence of numbers a1, a2,…an, this sequence has a very attractive property that every number appears exactly p times except a particular number t, which occurs q times. How to extract this particular number t from this sequence? In order to let this problem more mysterious, Changlong assumes that the involved number p, q in his problem are prime to each other, i.e., gcd(p, q)=1.
Changlong’s method works very well. After his declaration, the number of received letter has decrease sufficiently. The girls are beginning to think over the problem with continuous strenuous effort, no sooner later they find this problem is beyond their ability. So most of them give up, but there is still a beautiful girl who is persistent in this problem. One day this girl comes across you and she invites you to help her.
输入
At the first line of the input is an integer k (0 < k <= 100), the number of test cases.
For each test case, there will be a line containing three integers: n (0 < n <= 10^7), p, q (1 < p, q < 200, gcd (p, q) =1) and a line containing n elements ai (0 < ai < 10^7) of the sequence.
输出
For each test case, you should first output a line with “Case #:”,where # is the id of the test case, then you should output the particular number t, i.e., the number which occurs q times in the sequence.
样例输入
2
8 3 2
1 2 1 2 1 2 3 3
7 4 3
1 1 1 1 2 2 2
样例输出
Case 1:
3
Case 2:
2
题意:
给你n个数其中有n-1个数出现了p次,一个数出现了q次,你要找出这个只出现了q次的数字
一开始做的时候直接用map标记,超内存,后面才知道原来用数位标记一下这么简单。太菜了
代码:
#include<bits/stdc++.h>
using namespace std;
int a[30];
inline int read()
{
int xx=0,ff=1;
char ch;
ch=getchar();
while(ch>'9'||ch<'0')
{
if(ch=='-') ff=-1;
ch=getchar();
}
while(ch>='0'&&ch<='9')
{
xx=(xx<<3)+(xx<<1)+ch-'0';
ch=getchar();
}
return xx*ff;
}
int main()
{
int t=read();
for(int i=1;i<=t;i++){
memset(a,0,sizeof(a));
int n,p,q,x,sum=0;
n=read(),p=read(),q=read();
for(int j=0;j<n;j++){
x=read();
for(int k=0;x&&k<30;k++){
if(x&1) a[k]++;
x=x>>1;
}
}
int temp=1;
for(int j=0;j<=25;j++){
if(a[j]%p!=0){
sum+=temp;
}
temp*=2;
}
printf("Case %d:\n%d\n",i,sum);
}
return 0;
}