P1679 神奇的四次方数

P1679 神奇的四次方数
用一些什么东西组成一个什么东西,要求什么东西最优,这时候要考虑背包,不过要分析清楚是什么类型的背包。
这题显然是个完全背包。

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<cstring>
#define inf 2147483646
#define For(i,a,b) for(register long long i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.16
using namespace std;
long long n;
long long f[100010];
long long w[20];
void in(long long &x)
{
    long long y=1;
    char c=g();x=0;
    while(c<'0'||c>'9')
    {
    if(c=='-')
    y=-1;
    c=g();
    }
    while(c<='9'&&c>='0')x=x*10+c-'0',c=g();
    x*=y;
}
void o(long long x)
{
    if(x<0)
    {
        p('-');
        x=-x;
    }
    if(x>9)o(x/10);
    p(x%10+'0');
}

long long POW(long long a,long long b)
{
    while(b%2==0)
    {
        a*=a;
        b>>=1;
    }
    long long r=1;
    while(b>0)
    {
        if(b%2==1)
        r*=a;
        a*=a;
        b>>=1;
    }
    return r;
}

int main()
{
    in(n);
    long long cnt;
    for(cnt=1;POW(cnt,4)<=n;cnt++)
    {
        w[cnt]=POW(cnt,4);
    }
    For(i,1,n)
    f[i]=inf;
    f[0]=0;
    For(i,1,cnt)
     For(j,w[i],n) 
       f[j]=min(f[j],f[j-w[i]]+1);
    o(f[n]);
     return 0;
}

 

转载于:https://www.cnblogs.com/war1111/p/7678633.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值