HDU 4574Bombs

22 篇文章 0 订阅

Bombs

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12   Accepted Submission(s) : 2
Problem Description
  Terrorists are around everywhere, they always make troubles by detonating bombs. The terrorist have some gunpowder to make bombs, different gunpowder has different damage, every kind of gunpowder can use any times, and the power of one bomb is the product of the gunpowder it consists of. Let’s see how they make a bomb.
  At the beginning they decide to use X parts of gunpowder to make a bomb, and then choose X parts of gunpowder, every time the damage of the gunpowder they choose can’t be smaller than the last time they choose excepting the first time. After choosing X parts gunpowder terrorists get gunpowder[1], gunpowder[2] ... gunpowder[X] ( gunpowder[1] <= gunpowder[2] <= ... <= gunpowder[X]), and then mix the X parts gunpowder to generate a bomb with power of the product of the damage of the gunpowder. Terrorists make bombs in some order, if they make bomb_A before bomb_B one of the following conditions should meet.
(1)Terrorists use less parts gunpowder to make bomb_A than bomb_B.
(2)Terrorists both use X parts of gunpowders to make bomb_A and bomb_B. There exist an integer j(j <=X),for all i < j,gunpowder_A[i] = gunpowder_B[i] and gunpowder_A[j] < gunpowder_B[j].
  Now, the police get the gunpowder by some way, police find that the gunpowder’s damage is in the range of A to B(A, B included), police want to know the K-th bomb with the power in the range of L to R(L, R included).
 

Input
  There are multiple cases, the first line is an integer T denoting the number of the case, for each case has five integers A, B, L, R, K in a line. A, B denote the damage range of the gunpowder. L, R denote the power range of the bomb, K denotes the K-th bomb with the power in the range L to R that police want to know. 2<=A <= B<=10^6 1<=L<=R<=10^9 1<=K<=10^6
 

Output
  For each case output in the format in the first line “Case #x: y” x is the case number start from 1, y is the power of the bomb, and the second line with the gunpowder in the order they choose. If there is no more than K bombs in the range of L to R just output one line “Case #x: -1”.
 

Sample Input
  
  
4 2 2 1 4 1 2 5 1 4 4 73 23642 12 20903 29401 2 50 1 1000000000 815180
 

Sample Output
  
  
Case #1: 2 2 Case #2: 4 2 2 Case #3: -1 Case #4: 59200 4 4 5 20 37 [hint] In the second case we have 4 kinds of gunpowder with damage 2, 3, 4, 5. the first bomb is “2”with power of 2 The second bomb is “3” with power of 3 The third bomb is “4” with power of 4 The fouth bomb is “5” with power of 5 The fifth bomb is “2 2” with power of 2 * 2 = 4 So the 4-th bomb with power in the range of 1 to 4 is “2 2”. [/hint]
 

Source
2013 ACM-ICPC长沙赛区全国邀请赛——题目重现


迷迷糊糊的开始搜索练习,这个是一开始看的题目。

看到的第一个想法,直接暴力搜索,超时!

参考了大神的博客  http://blog.csdn.net/acm_cxlove/article/details/9935667


大致明白了怎么做。。。首先  按题意,解的数的个数在29个之内

计算出每一个长度所对应的解的个数,然后确定题目所求的答案的长度。但枚举最后一层时所花费的时间还是太大。。。继续参考


在再长度n中枚举第n-1的状态,就能计算出每一个n-1所对应的解的个数,从而省去了枚举第n层所花费的时间。。。但还是超时。。。想到剪枝。。不会。。。继续参考


知道当前层所需要乘出的乘积,即可由乘积求出上界和下界,很重要的剪枝!


接下来。。。看代码理解吧


#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>

using namespace std;
typedef long long LL;
const LL inf = 1000000000LL;

int t;
int A,B,L,R,K;
int tt,summ,aa[50];

int Pow(int a,int b)
{
    int ans=1;
    for(int i=1; i<=a; i++)
    {
        ans*=b;
        if(ans>inf||ans<=0)
            return inf+1;
    }
    return ans;
}

int dfs_len(int c,int a,int b,int l,int r,int k)
{
    if(c==0) return l<=r;
    if(c==1)
    {
        if(l>r)  return 0;
        return max(0,min(b,r)-max(a,l)+1);
    }
    int up=min(b,r/Pow(c-1,a));
    int down=max(a,l/Pow(c-1,b));  //求第C位数的取值范围  下限为  down*Pow(c-1,b)  上限  up*Pow(c-1,a)
    int ans=0;
    for(int i=down; i<=up; i++)
    {
        ans+=dfs_len(c-1,i,b,(l+i-1)/i,r/i,k-ans);  //求剩余c-1位数的乘积  最小乘积为 l/i  最大乘积为r/i
        if(ans>k)
            return ans;
    }
    return ans;
}
int ret;
void dfs(int c,int a,int b,int l,int r,int k)
{
    if(c==0)
    {
        return ;
    }
    if (c == 1)
    {
        int num = k + max(l , a) - 1;
        ret = ret * num;
        aa[c] = num;
        return ;
    }
    // 上下界
    int down = max(a , l / Pow( c - 1 , b));
    int up = min (b , r / Pow( c - 1 , a));
    for (int i = down ; i <= up ; i ++)
    {
        int cnt = dfs_len (c - 1 , i , b , (l + i - 1) / i , r / i , k);
        if (k > cnt)
        {
            k -= cnt;
            continue;
        }
        ret = ret * i;
        aa[c] = i;
        dfs (c - 1 , i , b , (l + i - 1) / i , r / i , k);
        return ;
    }
}

int main()
{
    scanf("%d",&t);
    tt=1;
    while(t--)
    {
        scanf("%d%d%d%d%d",&A,&B,&L,&R,&K);
        int i;
        bool flag=false ;
        printf("Case #%d: ",tt++);
        for(i=1; i<=30; i++)
        {
            int len=dfs_len(i,A,B,L,R,K);
            if(K>len)
            {
                K-=len;
                continue ;
            }
            ret=1;
            dfs(i,A,B,L,R,K);
            flag=true ;

            printf("%d\n",ret);
            for(int j=i; j>0; j--)
                if(j==i) printf("%d",aa[j]);
                else printf(" %d",aa[j]);
            printf("\n");
            break;
        }
        if(!flag)
            printf("-1\n");


        /*bool ok = false;
        printf ("Case #%d: " , tt++);
        //枚举长度
        for (int i = 1 ; i <= 30 ; i ++) {
            int cnt = dfs_len (i , A , B , L , R , K);
            if (K > cnt) {
                K -= cnt;
                continue;
            }
            ret = 1;
            dfs (i , A , B , L ,R , K);
            printf ("%d\n" , ret);
            for (int j = i ; j >= 1 ; j --) {
                printf ("%d%c" , aa[j] , j == 1 ? '\n' : ' ');
            }
            ok = true;
            break;
        }
        if (!ok) puts("-1");*/

    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值