小言_互联网的博客

P1679 神奇的四次方数 完全背包

434人阅读  评论(0)

传送门
思路:先打表预处理,将1-20所有数的四次方全部列举,然后将n做为容量,每个数的四次方作为价值。
完全背包:dp[i]是当容量是i时,当前背包中物体的个数。因为是要求最小,所以先预处理,将个数初始化最大。

/**
* From:
* Qingdao Agricultural University
* Created by XiangwangAcmer
* Date : 2019-10-16-19.59.14
* Talk is cheap.Show me your code.
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<queue>
#include<cmath>
#include<cctype>
#include<stack>
#include<map>
#include<string>
#include<cstdlib>
#define ll long long
using namespace std;
const ll maxn = 1e6 + 5;
const ll minn = 1e9 + 5;
const ll mod = 1000000007;
const int INF = 0x3f3f3f3f;
const long long LIMIT = 4294967295LL;
vector<int>v[maxn];
int dp[maxn];
vector<int>G[maxn];
bool row[maxn], col[maxn];
bool flag = 0;
queue<int>q;
int w[maxn];
int n;
int vis[maxn];
int Count = 100100;
int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    memset(dp,0xf,sizeof dp);
    dp[0]=0;
    for(int i = 1; i <= 20; i++)
        w[i] = i*i*i*i;
    for(int i=1;i<=20;i++)
        for(int j=w[i];j<=n;j++)
        dp[j]=min(dp[j],dp[j-w[i]]+1);
    cout << dp[n] << endl;
    return 0;
}


转载:https://blog.csdn.net/weixin_43960370/article/details/102595630
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场