Ural 1658 Sum of Digits
来源:互联网 发布:mac子弹头色号 编辑:程序博客网 时间:2024/03/28 22:20
求最小的,各个数位的和为
一眼看下来只会求方案数啊GG,最小的该怎么办呢
然后可以想到如果一个数的位数少,他肯定是短的
所以我们要找到最短位的那个数应该就是答案了
dp的时候顺便维护一下是用哪个数字转移过来的,回溯过去记录答案
最后再把拥有的数从小到大排一下,应该就是最优的了
但是这样空间复杂度是1e10啊QAQ,还是要GG啊
你或许注意到了,要是数位大于100,也是无解
也就是只有
最后一个对空间的优化是不用记录上一次的坐标,而是通过转移过来的数字来计算坐标
#include<bits/stdc++.h>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 1000;const int maxm = 9000;int dp[maxn][maxm],v[maxn][maxm];vector<int> ans;int main(){ memset(dp,0x3f,sizeof(dp)); dp[0][0] = 0; for(int i=1;i<=900;i++){ for(int j=1;j<=8100;j++){ for(int k=1;k<=9;k++){ int x = i-k,y = j-k*k; if(x>=0 && y>=0){ if(dp[i][j] > dp[x][y]+1){ dp[i][j] = dp[x][y]+1; v[i][j] = k; } } } } } int s1,s2; int T; scanf("%d",&T); while(T-- && ~scanf("%d %d",&s1,&s2)){ if(s1>900 || s2>8100 || dp[s1][s2]>100){ puts("No solution"); } else{ ans.clear(); int x = s1,y = s2; while(x && y){ ans.push_back(v[x][y]); int t = v[x][y]; x -= t,y -= t*t; } sort(ans.begin(),ans.end()); for(auto x : ans){ printf("%d",x); } puts(""); } } return 0;}
ps:无解的情况判断错误,不应该是这个地方没有被dp扫到,而是dp[s1][s2] > 100
0 0
- Ural 1658 Sum of Digits
- Ural 1658 Sum of Digits
- ural 1658. Sum of Digits
- URAL 1658. Sum of Digits
- URAL 1658 Sum of Digits (DP) #by Plato
- 【URAL 1658】1658. Sum of Digits(DP)
- URAL 1658. Sum of Digits(简单dp)
- URAL 1206 Sum of Digits of the Sum of Numbers
- ural 1014 Product of Digits
- URAL 1014 Product of Digits
- Timus Online Judge 1658 Sum of Digits
- HDU3022:Sum of Digits
- HDU3022:Sum of Digits
- B. Sum of Digits
- Sum of Digits
- Lucky Sum of Digits
- timus 1658. Sum of Digits URAL 解题报告 DP 数字位数和……
- Ural-1014. Product of Digits(简单题)
- git-canal介绍
- Hibernate分页查询的实现
- Java中线程局部变量ThreadLocal使用教程及源码分析
- sql的临时表使用小结
- STL之set和multiset总结
- Ural 1658 Sum of Digits
- jsp中的9个内置对象
- 【CodeForces】96B - Lucky Numbers (easy)(模拟,字典序全排列)
- 项目33-教师兼干部类
- java字符串处理--String,StringBuffer,StringBuilder类
- nyoj 44 最大的连续子串的和(不输出位置)
- Eclipse调整Java和xml代码字体大小
- 图片加载库Glide——解决图片错乱+无法设置tag
- 软考二进宫-总结篇