14行代码AC_Break the Chocolate HDU-4112(数学推导+解析)

励志用少的代码做高效表达


Problem describe

Benjamin is going to host a party for his big promotion coming up.
Every party needs candies, chocolates and beer, and of course Benjamin has prepared some of those. But as everyone likes to party, many more people showed up than he expected. The good news is that candies are enough. And for the beer, he only needs to buy some extra cups. The only problem is the chocolate.
As Benjamin is only a ‘small court officer’ with poor salary even after his promotion, he can not afford to buy extra chocolate. So he decides to break the chocolate cubes into smaller pieces so that everyone can have some.
He have two methods to break the chocolate. He can pick one piece of chocolate and break it into two pieces with bare hand, or put some pieces of chocolate together on the table and cut them with a knife at one time. You can assume that the knife is long enough to cut as many pieces of chocolate as he want.
The party is coming really soon and breaking the chocolate is not an easy job. He wants to know what is the minimum number of steps to break the chocolate into unit-size pieces (cubes of size 1 × 1 × 1). He is not sure whether he can find a knife or not, so he wants to know the answer for both situations.

Input

The first line contains an integer T(1<= T <=10000), indicating the number of test cases.
Each test case contains one line with three integers N,M,K(1 <=N,M,K <=2000), meaning the chocolate is a cube of size N ×M × K.

Output

For each test case in the input, print one line: “Case #X: A B”, where X is the test case number (starting with 1) , A and B are the minimum numbers of steps to break the chocolate into N × M × K unit-size pieces with bare hands and knife respectively.

Sample Input

2
1 1 3
2 2 2

Sample Output

Case #1: 2 2
Case #2: 7 3


分析

题意:给定一个N*M*K的巧克力块,问用手掰或用刀切各需多少步才能把巧克力块变成N*M*K1*1*1的单元块。 用手掰一次只能掰一块,用刀切可以切一行或者一列。

用手掰:很好算: N*M*K-1即可。

用刀切:注意:切的过程中方块是可以改变位置 ,举例:切割长宽高为1*4*1的方块

这样,一共两刀即可将所有方块分割。

分析到这里,不难看出规律:

首先将长方体分为长宽高三个参数,分别考虑

每个参数分别切割的次数一定与2的幂次有关,即: 2^n<=长或宽或高, n取最大值。 如:4就切两刀,8就切三刀,若在4-8之间,则切3刀。(画一画就明白了)

贴上最后的代码

#include<bits/stdc++.h>
using namespace std;
int main() {
    
    
	int T; cin>>T; 
	for(int i = 1; i <= T; i++) {
    
    
		long long l, w, h;  cin>>l>>w>>h;
		long long sum2=0;
		sum2 += ceil(1.0*log(l)/log(2));
		sum2 += ceil(1.0*log(w)/log(2));
		sum2 += ceil(1.0*log(h)/log(2));
		
		printf("Case #%d: %lld %lld\n", i, (l*w*h-1), sum2);
	}
return 0; } 

猜你喜欢

转载自blog.csdn.net/weixin_43899069/article/details/108307241
今日推荐