题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1059
题目大意:价值为1, 2, 3, 4, 5 , 6 的大理石以及分别有x1, x2, x3, x4, x5, x6个,问两个人是否能平分这些大理石, 要求价值相等?
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <cstdlib> 6 #include <cmath> 7 #include <set> 8 #include <map> 9 #include <vector> 10 using namespace std; 11 12 const int MAX = 120010; 13 int a[MAX], b[MAX]; 14 int main() 15 { 16 int x[10], i, j, k; 17 int t = 0; 18 while(~scanf("%d %d %d %d %d %d", &x[1], &x[2], &x[3], &x[4], &x[5], &x[6])) 19 { 20 t++; 21 if(x[1] + x[2] + x[3] + x[4] + x[5] + x[6] == 0) 22 break; 23 //下面这步取mo是关键,不然会超时 24 int sum = x[1] % 30 + (x[2] % 30) * 2 + (x[3] % 30) * 3 + (x[4] % 30) * 4 + (x[5] % 30) * 5 + (x[6] % 30) * 6; 25 if(sum % 2 == 1) 26 { 27 printf("Collection #%d: ", t); 28 printf("Can't be divided. "); 29 continue; 30 } 31 memset(a, 0, sizeof(a)); 32 memset(b, 0, sizeof(b)); 33 for(i = 0; i <= x[1]; i++) 34 { 35 a[i] = 1; 36 } 37 for(i = 2; i < 7; i++) 38 { 39 int s; 40 for(j = 0; j <= sum / 2; j++) 41 { 42 for(s = 0, k = 0; s <= x[i] && k + j <= sum / 2; k += i, s++) 43 { 44 b[k + j] += a[j]; 45 } 46 } 47 for(j = 0; j <= sum / 2; j++) 48 { 49 a[j] = b[j]; 50 b[j] = 0; 51 } 52 } 53 if(a[sum / 2] == 0) 54 { 55 printf("Collection #%d: ", t); 56 printf("Can't be divided. "); 57 } 58 else 59 { 60 printf("Collection #%d: ", t); 61 printf("Can be divided. "); 62 } 63 } 64 return 0; 65 }