您的位置:首页 > 其它

ZCMU—1777

2016-12-19 10:05 405 查看

1777: 寻找倍数

Time Limit: 1 Sec  Memory Limit: 128 MB

[Submit][Status][Web
Board]

Description

给出n(n<=10000)个正整数,每个数xi<=15000.可以在这个n个数中选择一些数出来,至少选择一个,是否存在一种选择方案使得选择出
来的数的和是n的整数倍

Input

第一行一个T(T<=500),第二行一个数n,接下来n个正整数

Output

Case #x: y,其中x是测试编号,从1开始,y表示答案,如果存在y为Yes,否则为No

Sample Input

2



1 2 3 4 1

2

1 2

Sample Output

Case #1: Yes

Case #2: Yes

【分析】

砝码称重...经典题,数据只有10000个数,所以可以用队列直接做,遍历一遍数组,每次对当前这个数,去加上前面已经出现过的所有数,得到新的数,然后判重,没出现过的就加进队列,然后做完n个数,中途加个判断,如果某一次得到的数已经是n的倍数了那么就直接跳出循环
【代码】
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
vector<int>a;
int main()
{
int n,x;
int f[10100];
int pp;scanf("%d",&pp);
for (int p=1;p<=pp;p++)
{
int tt=0;
memset(f,0,sizeof(f));
scanf("%d",&n);
scanf("%d",&x);
a.push_back(x);
for (int i=1;i<n;i++)
{
scanf("%d",&x);
if (x%n==0) tt=1;
if (!tt)
{
for (int j=0;j<a.size();j++)
{
int xx=(a[j]+x)%n;
if (xx==0) {tt=1;break;}
if (!f[xx])
{
f[xx]=1;
a.push_back(xx);
}
}
}
}
printf("Case #%d: %s\n",p,tt==0?"No":"Yes");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: