题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4422
Problem Description
It's yet another festival season in Gensokyo. Little girl Alice planned to pick mushrooms in five mountains. She brought five bags with her and used different bags to collect mushrooms from different mountains. Each bag has a capacity of 2012 grams. Alice has
finished picking mushrooms in 0 ≤ n ≤ 5 mountains. In the i-th mountain, she picked 0 ≤ wi ≤ 2012 grams of mushrooms. Now she is moving forward to the remained mountains. After finishing picking mushrooms in all the five mountains, she want to bring
as much mushrooms as possible home to cook a delicious soup.
Alice lives in the forest of magic. At the entry of the forest of magic, live three mischievous fairies, Sunny, Lunar and Star. On Alice's way back home, to enter the forest, she must give them exactly three bags of mushrooms whose total weight must be of integral kilograms. If she cannot do so, she must leave all the five bags and enter the forest with no mushrooms.
Somewhere in the forest of magic near Alice's house, lives a magician, Marisa. Marisa will steal 1 kilogram of mushrooms repeatedly from Alice until she has no more than 1 kilogram mushrooms in total. So when Alice gets home, what's the maximum possible amount of mushrooms Alice has?
Alice lives in the forest of magic. At the entry of the forest of magic, live three mischievous fairies, Sunny, Lunar and Star. On Alice's way back home, to enter the forest, she must give them exactly three bags of mushrooms whose total weight must be of integral kilograms. If she cannot do so, she must leave all the five bags and enter the forest with no mushrooms.
Somewhere in the forest of magic near Alice's house, lives a magician, Marisa. Marisa will steal 1 kilogram of mushrooms repeatedly from Alice until she has no more than 1 kilogram mushrooms in total. So when Alice gets home, what's the maximum possible amount of mushrooms Alice has?
Remember that our common sense doesn't always hold in Gensokyo. People in Gensokyo believe that 1 kilogram is equal to 1024 grams.
Input
There are about 8192 test cases. Proceed to the end of file.
The first line of each test case contains an integer 0 ≤ n ≤ 5, the number of mountains where Alice has picked mushrooms. The second line contains n integers 0 ≤ wi ≤ 2012, the amount of mushrooms picked in each mountain.
The first line of each test case contains an integer 0 ≤ n ≤ 5, the number of mountains where Alice has picked mushrooms. The second line contains n integers 0 ≤ wi ≤ 2012, the amount of mushrooms picked in each mountain.
Output
For each test case, output the maximum possible amount of mushrooms Alice can bring home, modulo 20121014 (this is NOT a prime).
Sample Input
1 9 4 512 512 512 512 5 100 200 300 400 500 5 208 308 508 708 1108
Sample Output
1024 1024 0 792HintIn the second sample, if Alice doesn't pick any mushrooms from the 5-th mountain. She can give (512+512+0) =1024 grams of mushrooms to Sunny, Lunar and Star. Marisa won't steal any mushrooms from her as she has exactly 1 kilogram of mushrooms in total. In the third sample, there are no three bags whose total weight is of integral kilograms. So Alice must leave all the five bags and enter the forest with no mushrooms. In the last sample: 1.Giving Sunny, Lunar and Star: (208+308+508)=1024 2.Stolen by Marisa: ((708+1108)-1024)=792
Source
题意:
一共同拥有5座山。每座山有非常多的蘑菇,给出一个 n 表示已经採了 n 座山上的蘑菇,求最多带回去多少蘑菇。。
当採完5座山后,有三个人拿走三个袋子里的蘑菇。这三个的袋子的蘑菇数量为1024的倍数;
假设没有三个袋子的蘑菇数目满足情况,则拿走5个袋子,即不能带走不论什么蘑菇。
而且回家的路上有人偷蘑菇。一直偷,直到总蘑菇数不大于1024。
PS:假设n小于等于3,那么一定能拿回家1024的蘑菇!
假设n大于3,则分类讨论!
代码例如以下:
#include <cstdio> #include <cstring> int main() { int a[7]; int t; int n; while(~scanf("%d",&n)) { int sum = 0; for(int i = 0; i < n; i++) { scanf("%d",&a[i]); sum+=a[i]; } if(n <= 3) { printf("1024 "); continue; } int maxx = 0, tt = 0; if(n == 4) { int flag = 0; for(int i = 0; i < 4; i++) { for(int j = i+1; j < 4; j++) { for(int k = j+1; k < 4; k++) { if((a[i]+a[j]+a[k])%1024 == 0) { flag = 1; } } } } if(flag) { printf("1024 "); continue; } for(int i = 0; i < 4; i++) { for(int j = i+1; j < 4; j++) { tt= a[i]+a[j]; while(tt > 1024) tt-=1024; if(tt > maxx) { maxx = tt; } } } printf("%d ",maxx); continue; } if(n == 5) { for(int i = 0; i < 5; i++) { for(int j = i+1; j < 5; j++) { for(int k = j+1; k < 5; k++) { int ss = a[i]+a[j]+a[k]; if(ss%1024==0) { tt = sum - ss; while(tt >1024) tt-=1024; if(tt > maxx) { maxx = tt; } } } } } printf("%d ",maxx); } } return 0; }