您的位置:首页 > 其它

ACM POJ 2356

2013-08-08 09:59 176 查看
Findamultiple

TimeLimit:1000MS
MemoryLimit:65536KB
64bitIOFormat:%I64d&%I64u

[Submit][GoBack][Status]

Description

TheinputcontainsNnatural(i.e.positiveinteger)numbers(N<=10000).Eachofthatnumbersisnotgreaterthan15000.Thisnumbersarenotnecessarilydifferent(soitmayhappenthattwoormoreofthemwillbeequal).Yourtaskistochooseafewofgivennumbers(1<=few<=N)sothatthesumofchosennumbersismultipleforN(i.e.N*k=(sumofchosennumbers)forsomenaturalnumberk).

Input

ThefirstlineoftheinputcontainsthesinglenumberN.EachofnextNlinescontainsonenumberfromthegivenset.

Output

Incaseyourprogramdecidesthatthetargetsetofnumberscannotbefounditshouldprinttotheoutputthesinglenumber0.Otherwiseitshouldprintthenumberofthechosennumbersinthefirstlinefollowedbythechosennumbersthemselves(onaseparatelineeach)inarbitraryorder.
Iftherearemorethanonesetofnumberswithrequiredpropertiesyoushouldprinttotheoutputonlyone(preferablyyourfavorite)ofthem.

SampleInput

5
1
2
3
4
1

SampleOutput

2
2
3

Source

UralCollegiateProgrammingContest1999

抽屉原理,开始高明白,安叔一点拨就清楚了,Orz,和mod后相同的两个数就可以

给代码了

#include<cstdio>

[code]#include<cstring>
constintN=10001;

inta
,sum
,visit
;


intn;


voidprint(inti,intj)

{

printf("%d\n",j-i+1);

intk;

for(k=i;k<=j;k++)

{

printf("%d\n",a[k]);

}

return;

}


intmain()

{


scanf("%d",&n);

sum[0]=0;

memset(visit,0,sizeof(visit));

for(inti=1;i<=n;i++)

{

scanf("%d",&a[i]);

sum[i]=(a[i]+sum[i-1])%n;

}

//for(inti=1;i<=n;i++)

//{

//printf("%d\n",sum[i]);

//}

for(inti=1;i<=n;i++)

{

if(sum[i]==0)

{

print(1,i);

return0;

}

else

{

if(visit[sum[i]]!=0)

{

print((visit[sum[i]]+1),i);

return0;

}

else

{

visit[sum[i]]=i;

//printf("%d\n",visit[i]);

}

}


}

}

[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: