标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 947 Accepted Submission(s): 538
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define maxn 30 4 int min, sum,n; 5 int a[maxn], b[maxn],c[maxn]; 6 int Min(int a, int b) 7 { 8 return a> b ? b:a; 9 } 10 int dfs(int m) 11 { 12 if (m == n + 1) 13 { 14 int temp = sum; 15 int length = 0; 16 for (int j = 1; j <=n; j++) 17 { 18 if (temp == 0) 19 break; 20 if (c[j] == 0) 21 { 22 temp %= a[j]; 23 length++; 24 } 25 } 26 if (temp==0) 27 min = Min(min, length); 28 return 0; 29 }
//这里利用选择为0,未选为1,方便代码调试时按照二进制运算来看,比较直观 30 c[m] = 0;//用来标记哪些被选了 31 dfs(m + 1); 32 c[m] = 1;//标记未被选 33 dfs(m + 1); 34 } 35 int cmp(const void*a, const void*b) 36 { 37 return *(int *)b - *(int*)a; 38 } 39 int main() 40 { 41 int num; 42 scanf("%d", &num); 43 while (num--) 44 { 45 min= 1000000; 46 scanf("%d%d", &n, &sum); 47 for (int i = 1; i <= n; i++) 48 scanf("%d", &a[i]); 49 qsort(&a[1], n, sizeof(a[1]), cmp); 50 dfs(1); 51 if (min!=1000000) 52 printf("%d\n", min); 53 else printf("-1\n"); 54 } 55 }
CodeForce Round#49 untitled (Hdu 5339)
标签:
原文地址:http://www.cnblogs.com/Cc1231/p/4705728.html