Tomya is a girl. She loves Chef Ciel very much.
Tomya like a positive integer p, and now she wants to get a receipt of Ciel‘s restaurant whose total price is exactly p.
The current menus of Ciel‘s restaurant are shown the following table.
Name of Menu | price |
eel flavored water | 1 |
deep-fried eel bones | 2 |
clear soup made with eel livers | 4 |
grilled eel livers served with grated radish | 8 |
savory egg custard with eel | 16 |
eel fried rice (S) | 32 |
eel fried rice (L) | 64 |
grilled eel wrapped in cooked egg | 128 |
eel curry rice | 256 |
grilled eel over rice | 512 |
deluxe grilled eel over rice | 1024 |
eel full-course | 2048 |
Note that the i-th menu has the price 2i-1 (1 ≤ i ≤ 12).
Since Tomya is a pretty girl, she cannot eat a lot.
So please find the minimum number of menus whose total price is exactly p.
Note that if she orders the same menu twice, then it is considered as two menus are ordered. (SeeExplanations for details)
The first line contains an integer T, the number of test cases.
Then T test cases follow.
Each test case contains an integer p.
For each test case, print the minimum number of menus whose total price is exactly p.
1 ≤ T ≤ 5
1 ≤ p ≤ 100000 (105)
There exists combinations of menus whose total price is exactly p.
4 10 256 255 4096
2 1 8 2
本题由于数据特殊,原来可以使用贪心法的。
时间效率可以达到O(1),因为最后有个1的数组能被任何整数整除,所以是必然有解的。
#include <stdio.h> int CielandReceiptCal() { const static int MENUS_NUM = 12; int T; scanf("%d", &T); while (T--) { int P; scanf("%d", &P); int largest = 2048, ans = 0; for (int i = MENUS_NUM - 1; i >= 0 ; i--) { ans += P / largest; P %= largest; largest >>= 1; } printf("%d\n", ans); } return 0; }
int CielandReceiptDP() { const static int MENUS_NUM = 12; const static int MENUS[MENUS_NUM] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048}; int T; scanf("%d", &T); while (T--) { int P; scanf("%d", &P); int *tbl = new int[P+1];//这里的[]写成()错误 tbl[0] = 0; for (int i = 1; i <= MENUS_NUM; i++) { for (int j = MENUS[i-1]; j <= P; j++) { tbl[j] = tbl[j-MENUS[i-1]] + 1; }//处理4096000上百万个数据也能秒杀 } printf("%d\n", tbl[P]); delete [] tbl; } return 0; }
codechef Ciel and Receipt题解,布布扣,bubuko.com
原文:http://blog.csdn.net/kenden23/article/details/24972001