luogu P1731 [NOI1999] birthday cake | enumeration violence

Topic background

July 17 is the birthday of Mr.W, ACM-THU do this to make a volume of M layer Nπ

Birthday cake, each layer is a cylinder.

Provided from the bottom up of the number i (1 <= i <= M) layer cake with a radius Ri, a cylindrical height Hi. When i <M, the required Ri> Ri + 1
due to the cake with wiping, to save money as much as possible, we want (lowermost layer under the bottom surface excluding) the area of the outer surface of the cake Q minimal.

So that Q = Sπ

Please programmed M and N is given, find the cake recipe program (the appropriate value of Ri and Hi), the minimum of S.

(Q addition, all the above data are all positive integer)

Title Description

Input Format

There are two rows, the first row N (N <= 20000), it represents the volume of the cake to be made of n [pi]; second row M (M <= 15), indicates the number of layers of cake M.

Output Format

Only one row is a positive integer S (if no solutions are S = 0).


#include<iostream>
using namespace std;
int n,m;int maxn=(1<<31)-1;int f1[1001],f2[1001];
inline void dfs(int dep,int lastr,int lasth,int s,int v){
    if(dep==0){if(s<maxn&&n==v)maxn=s;return;}
    if(s+2*(n-v)/lastr>maxn)return;
    if(f1[dep]+v>n)return;
    if(f2[dep]+s>maxn)return;
    for(int r=min(n-v,lastr-1);r>=dep;r--)
    for(int h=min((n-v)/(r*r),lasth-1);h>=dep;h--)
    dfs(dep-1,r,h,2*r*h+s,v+r*r*h);
    
}
int main(){
    cin>>n>>m;
    for(int i=1;i<m;i++){f1[i]=i*i*i+f1[i-1];f2[i]=2*i*i+f2[i-1];}
    for(int r=n;r>=m;r--)
    for(int h=n/(r*r);h>=m;h--)
    dfs(m-1,r,h,r*r+h*2*r,r*r*h);
    cout<<maxn;
}

Guess you like

Origin www.cnblogs.com/naruto-mzx/p/11847799.html