网址:https://www.luogu.com.cn/problem/P1679ios
在你的帮助下,v神终于帮同窗找到了最合适的大学,接下来就要通知同窗了。在班级里负责联络网的是dm同窗,因而v神便找到了dm同窗,可dm同窗正在忙于研究一道有趣的数学题,为了请dm出山,v神只好请你帮忙解决这道题了。spa
题目描述:将一个整数m分解为n个四次方数的和的形式,要求n最小。例如,m = 706, 706 = \(5^4\) + \(3^4\), 则n = 2。code
一行,一个整数m。get
一行,一个整数n。数学
706
2
数据范围:对于100%的数据,m<=100,000string
这道题估计一下,最大的数不会比18大。所以,预处理18以前数的四次方。接下来彻底背包。注意预处理。io
代码以下:class
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; const int c[17] = {1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000, 14641, 20736, 28561, 38416, 50625, 65536, 83521}; int m, dp[100000]; int main() { scanf("%d", &m); memset(dp, 0x3f, sizeof(dp)); dp[0] = 0; for(int i = 1; i <= m; ++ i) { for(int j = 0; j < 17; ++ j) { if(i >= c[j]) dp[i] = min(dp[i], dp[i - c[j]] + 1); } } printf("%d\n", dp[m]); return 0; }