Codeforces Gym 101164I Cubes
来源:互联网 发布:找淘宝客服工作 编辑:程序博客网 时间:2024/06/06 04:39
Problem I
Cubes
Input File: I.inOutput File: standard output
Time Limit: 0.5 seconds (C/C++)
Memory Limit: 256 megabytes
Write a program that takes a natural number N and decomposes it as a sum of the minimum number of
exact natural cubes. The program should find m1,m2, …,mk, such that eachmi is a natural number,
m1
3
+ m23+… + mk3= N, andk is minimal.
Input
The only line of the input file contains the numberN (1 ≤N ≤ 44,777,444).
Output
Your program should write exactly two lines. The first line contains the numberk - the minimum
number of natural cubes. The second line contains kspace-separated natural numbers - that
raised to the power of 3 sum to N.
Sample inputSample output42 7
2 2 2 2 2 1 143 3
3 2 2
题目大意:给定一个数n,要求将n分成k个数的立方和,使得k最小。
思路:该题有多种做法,可以采用BFS的方法,也可以采用DFS的方法。
BFS:
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>#include<cmath>#include<cstdlib>#include<queue>using namespace std;const int MAXN=50000010;int dis[MAXN];int ans[1000];queue <int> q;bool p;int work(int n,int required,int MAX){if (dis[n]<1000 && !p){return dis[n];}if (!n){return 0;}if (required<=2 && dis[n]>required){return 3;}int ret=required+1;for (int i=MAX;i>=1;i--){int m=n-i*i*i;if (m<0){continue;}int res=work(m,required-1,i)+1;if (res<ret){ans[required-1]=i;ret=res;}required=ret-1;} return ret;}int main(){int n;scanf("%d",&n);for (int i=0;i<MAXN;i++){dis[i]=1000;}q.push(0);dis[0]=0;while (!q.empty()){int x=q.front();if (dis[x]==3){break;}q.pop();for (int i=1;x+i*i*i<MAXN;i++){int y=x+i*i*i;if (dis[y]>dis[x]+1){dis[y]=dis[x]+1;q.push(y);}}}int num=work(n,100,400);p=true;work(n,num,400);printf("%d\n",num);for (int i=0;i<num;i++){printf("%d ",ans[i]);} return 0;}
DFS:
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<iostream>#include<cmath>#include<cstdlib>#include<ctime>using namespace std;const int MAXN=500;int n,tot;int p[MAXN];int num[MAXN];void dfs(int n,int i,int rem){if (n==0){printf("%d\n",tot);for (int i=0;i<tot;i++){printf("%d ",num[i]);}exit(0);}if (rem<=0){return ;}int id=upper_bound(p+1,p+MAXN,n)-(p+1);i=min(i,id);if (n>p[i]*rem){return ;}tot++;while (i>=1){num[tot-1]=i;dfs(n-p[i],i,rem-1);i--;}tot--;}int main(){for (int i=1;i<MAXN;i++){p[i]=i*i*i;}scanf("%d",&n);for (int i=1;;i++){dfs(n,MAXN-1,i);} return 0;}
阅读全文
0 0
- Codeforces Gym 101164I Cubes
- CodeForces Gym 100735I
- codeforces Gym 101097 I Sticks
- 【Codeforces Gym 100228 - I】Graph Dp
- Codeforces Gym 100338I TV Show DFS
- codeforces Gym 101334 I IP Networks
- codeforces Gym 101341 I Matrix God
- Gym 101341G I love Codeforces 模拟
- Codeforces Gym 100623I Problem I. Important Wires
- Codeforces Gym 101164 F. Letter
- Codeforces Gym 101164 G. Pokemons
- Codeforces GYM 100651 D I Conduit! (水计算几何)
- Codeforces Gym 100339I Sum vs Product 剪枝搜索
- Codeforces Gym 100340I Longest Common Subpair 字符串DP
- Codeforces Gym 100341I Hungry Queen 2 Set水题
- Codeforces Gym 100341I Hungry Queen 2 Set+模拟
- 【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)
- [交互 杂题] Codeforces Gym 100307 NEERC 13 I. Interactive Interception
- 怎样安装最新版的Appium
- CUDA编程实践(一)
- 并查集之POJ--1308--Is It A Tree?
- CSS基础(一)
- MemSQL Start[c]UP 3.0
- Codeforces Gym 101164I Cubes
- HDU 5974 A Simple Math Problem
- BZOJ 3527 快速傅里叶变换
- 数据库视频总结(二)
- 有序列表,无序列表,以及框架集
- 并查集
- POJ-2391 Ombrophobic Bovines (二分答案+Floyd+拆点+最大流)
- 关于const的相关知识
- javascript中==与===的区别