HDU-#4112 Break the Chocolate(规律)

来源:互联网 发布:环信java服务端demo 编辑:程序博客网 时间:2024/06/05 07:59

           题目大意:将一块N*M*K的巧克力分解成1*1*1的大小块,按照手工分和用刀切两种方式,问分别最少需要多少步?

           解题思路:通过对案例进行分析,可以得出对于手工,因为只能一次一次地分解,需要n*m*k-1次。对于刀切,可以对于同一个型号进行一切分解,每一次的目标都最终分解成2次幂的形式,因此列举数据分析可以,对于大于1的长度需要切接近2次幂的上限数。解法详见code。

           题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=4112

           code:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define ll __int64ll n,m,k,a,b;int t,cas;ll gao(ll x){    ll cnt=0,tmp=2;    while(x>=tmp){        cnt++;        tmp*=2;        if(x>tmp/2 && x<tmp) ++cnt;    }    return cnt;}int main(){    //freopen("input.txt","r",stdin);    cas=0;    scanf("%d",&t);    while(t--){        a=0;b=0;        scanf("%I64d%I64d%I64d",&n,&m,&k);        a=n*m*k-1;        if(n>1) b+=gao(n);        if(m>1) b+=gao(m);        if(k>1) b+=gao(k);        printf("Case #%d: %I64d %I64d\n",++cas,a,b);    }    return 0;}

0 0