1777: 寻找倍数
Time Limit: 1 Sec Memory Limit: 128 MB
Submit: 232 Solved: 137
[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
5
1 2 3 4 1
2
1 2
Sample Output
Case #1: Yes
Case #2: Yes
【分析】用set容器,读入一个数就判断能否整除,不能的话就加上容器里存入的数看能不能整除,若还是不能就把该数对n的余数插入set中。
set容器看这里hiahia
#include<bits/stdc++.h>
using namespace std;
set<int>s;
int main()
{int t,ca=0;scanf("%d",&t);while(t--){s.clear();int n,flag=0;scanf("%d",&n);for(int i=0;i<n;i++){int x;scanf("%d",&x);s.insert(x);if(x%n==0)flag=1;if(!flag){set<int>::iterator j=s.begin();for(;j!=s.end();j++){if((*j+x)%n==0){flag=1;break;}s.insert((*j+x)%n);}}}if(flag)printf("Case #%d: Yes\n",++ca);else printf("Case #%d: No\n",++ca);}
}