Hdu Girls' Day
2014-8-1 20:23
For example if the girl's No. is 12, and another girl's No. is 210, then you will choose the girl with No. 210. Because 210 = 2 *3 * 5*7 , 12 = 2*2*3. 210 have 4 unique prime factors but 12 just have 2. If there are many results, you will choose the one whose name has minimum lexicographic order.
#include<stdio.h>
#include<string.h>
#define maxn 1000000
int a[maxn]={0,0};
void count()
{
for(int i=2;i*i<maxn;i++)
{
if(!a[i])
{
for(int j=i*i;j<maxn;j+=i)
a[j]=1;
}
}
}
int main()
{
count();
int T,num,k,shu,max,n,q;
char name[22],s[22];
scanf("%d",&T);
while(T--)
{
max=-1;
scanf("%d",&n);
while(n--)
{
shu=0;q=0;
scanf("%s%d",&name,&num);
for(k=2;k<=num;k++)
{
if(a[k]) continue;
else
{
if(num%k==0)
{
if(k!=q) shu++;
q=k;
num=num/k;
}
}
}
if(shu>max)
{
max=shu;
strcpy(s,name);
}
else if(max==shu)
{
if(strcmp(name,s)<0) //name是當前的,s是之前存的,若當前的更小,就把當前的存入s
strcpy(s,name);
}
}
printf("%s\n",s);
}
return 0;
}