B - Dividing
11215 단어 div
Submit
Status
Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , n6 , where ni is the number of marbles of value i. So, the example from above would be described by the input-line "1 0 1 2 0 0". The maximum total number of marbles will be 20000.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
For each collection, output "Collection #k:", where k is the number of the test case, and then either "Can be divided."or "Can't be divided.".
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0
1 0 0 0 1 1
0 0 0 0 0 0
Sample Output
Collection #1:
Can't be divided.
Collection #2:
Can be divided.
, , ,
1 #include<cstdio>
2 #include<string.h>
3 #include<math.h>
4 using namespace std;
5 int dp[100000];
6 int main()
7 {
8 int a[11];
9 int t=1;
10 int cnt;
11 while(scanf("%d %d %d %d %d %d",&a[1],&a[2],&a[3],&a[4],&a[5],&a[6])!=EOF)
12 {
13 if(a[1]+a[2]+a[3]+a[4]+a[5]+a[6]==0) break;
14 int sum,x;
15 sum=a[1]*1+a[2]*2+a[3]*3+a[4]*4+a[5]*5+a[6]*6;
16 printf("Collection #%d:
",t);
17 t++;
18 if(sum%2)
19 {
20 printf("Can't be divided.
");
21 continue;
22 }
23 x=sum/2;
24 memset(dp,0,sizeof(dp));
25 dp[0]=1;
26 for(int i=1;i<=6;i++)
27 {
28 if(!a[i])
29 continue;
30 for(int j=1;j<=a[i];j*=2)// ,2,4,8,16, dp[3],dp[5],dp[7] ,
31 {
32 cnt=j*i;
33 for(int k=x;k>=cnt;k--)
34 {
35 if(dp[k-cnt])
36 dp[k]=1;
37 }
38 a[i]-=j;//
39 }
40 cnt=a[i]*i;// a[i]
41 if(cnt)// cnt 0
42 {
43 for(int k=x;k>=cnt;k--)
44 {
45 if(dp[k-cnt])
46 dp[k]=1;
47 }
48 }
49 }
50 if(dp[x])// dp[x] 0,
51 printf("Can be divided.
");
52 else printf("Can't be divided.
");
53 printf("
");
54 }
55 return 0;
56 }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
🧙🏼 HTML 구조를 나타내는 요소: 컨텐츠 분할 요소 : 블록 레벨 요소 : 플로우 콘텐츠를 위한 통용 컨테이너 (순수 컨테이너로서 아무것도 표현안함) : 인라인 컨테이너 : 인라인 레벨 요소 🌵 span (인라인 요소) vs div(블록 요소) ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.