hdu 4574 Bombs (dfs+剪枝)

來源:互聯網
上載者:User
Bombs

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 326    Accepted Submission(s): 45Problem 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

42 2 1 4 12 5 1 4 473 23642 12 20903 294012 50 1 1000000000 815180
 

Sample Output

Case #1: 22Case #2: 42 2Case #3: -1Case #4: 592004 4 5 20 37HintIn 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 3The third bomb is “4” with power of 4The fouth bomb is “5” with power of 5The fifth bomb is “2 2” with power of 2 * 2 = 4So the 4-th bomb with power in the range of 1 to 4 is “2 2”. 
 

Source2013
ACM-ICPC長沙賽區全國邀請賽——題目重現 題意:給一個區間[A,B],表示有B - A + 1種火藥,火藥i的威力值為i,現在要用這些火藥做炸彈,每次先按原料個數再按原料的字典序做炸彈。每個炸彈的威力值是每種原料威力值的乘積。現在給一個區間[L,R]表示炸彈的威力值範圍,求第K個炸彈的配方。

思路:因為炸彈的威力值是原料威力值的乘積,R不超過10^9,A至少為2,所以極端情況下炸彈至少要29種原料,所以直接搜就可以了。

剪枝:當搜到第i中火藥的時候,假設剩下的全部用威力值最大的B種火藥,如果還不能達到炸彈威力值下界L,退出;假設剩下的全部用威力值最小的A種火藥,已經超過炸彈威力值上界R,退出。加了這個剪枝還是TLE呀,搜到倒數第二層的時候統計個數就險過了。

ps:開始想的以為是對的剪枝錯了,所以想剪枝時一定要合理。
思路來源於:http://blog.csdn.net/ophunter/article/details/9980921
代碼:

#include <iostream>#include <cstdio>#include <cstring>#define maxn 35using namespace std;int n,m,ans,depth,flag;long long s[maxn];long long a,b,l,r,k,temp,cnt;long long dl,dr,tl,tr;long long pow(int cc,int num)  // 計算cc^num{    int i;    if(num==0) return 1;    long long ppt=cc;    for(i=1;i<num;i++)    {        ppt*=cc;    }    return ppt;}void dfs(int p,int pos,long long val){    if(flag) return ;    int i,j;    long long le,ri;    if(pos==depth)                // 對倒數第二層處理    {        long long rrr=r/val;        long long lll=l/val;        if(l%val!=0) lll++;       // 注意'/'會取下界 要處理掉        if(rrr>b) rrr=b;          // 注意和邊界比較        if(lll<p) lll=p;        if(rrr>=lll&&cnt+rrr-lll+1<k)        {            cnt+=(rrr-lll+1);            return ;        }        else if(rrr<lll) return ;    }    if(pos==depth+1)    {        if(val>=l&&val<=r) cnt++;        if(cnt==k)        {            flag=1;            printf("%I64d\n",val);            for(i=1;i<depth;i++)            {                printf("%I64d ",s[i]);            }            printf("%I64d\n",s[i]);        }        return ;    }    le=(long long)(l/(val*pow(b,depth-pos)));    ri=(long long)(r/(val*pow(a,depth-pos)))+1;    if(le<p) le=p;    if(ri>tr) ri=tr;    for(i=le;i<=ri;i++)    {        s[pos]=i;        if(val*i*pow(i,depth-pos)>r) return ;        dfs(i,pos+1,val*i);    }}int main(){    int i,j,t,test=0;    scanf("%d",&t);    while(t--)    {        scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&l,&r,&k);        cnt=min(b,r)-max(a,l)+1;        printf("Case #%d: ",++test);        if(r<a)        {            printf("-1\n");            continue ;        }        if(cnt>=k)        {            long long tmd=a>l?a:l;       // 特判一定要注意a、l的大小!            printf("%I64d\n%I64d\n",tmd+k-1,tmd+k-1);            continue ;        }        if(cnt<0) cnt=0;        flag=0;        dl=dr=2;           // 計算上下深度        temp=b*b;        while(temp<l)        {            dl++;            temp*=b;        }        temp=a*a;        while(temp<=r)        {            dr++;            temp*=a;        }        for(i=dl;i<dr;i++)        {            depth=i;            tl=(long long)(l*1.0/pow(b,i-1));            if(tl<a) tl=a;            tr=(long long)(r*1.0/pow(a,i-1))+1;            if(tr>b) tr=b;            for(j=tl;j<=tr;j++)            {                s[1]=j;                if(j*pow(j,i-1)>r) break ;                dfs(j,2,j);                if(flag) break ;            }            if(flag) break ;        }        if(!flag) printf("-1\n");    }    return 0;}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.