URAL 1658 Sum of Digits
思路:
dp+记录路径
状态:dp[i][j]表示s1为i,s2为j的最小位数
初始状态:dp[0][0]=0
状态转移:dp[i][j]=min(dp[i-k][j-k*k]+1,dp[i][j])(0<=k<10)
在状态转移时用一个数组v[i][j]记录选的k
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a)) const int INF=0x3f3f3f3f;
int dp[][];
int v[][];
vector<int>ans;
int main(){
ios::sync_with_stdio(false);
cin.tie();
mem(dp,INF);
dp[][]=;
for(int i=;i<=;i++){
for(int j=;j<=;j++){
for(int k=;k<=;k++){
if(i-k>=&&j-k*k>=){
if(dp[i-k][j-k*k]+<dp[i][j]){
dp[i][j]=dp[i-k][j-k*k]+;
v[i][j]=k;
}
}
}
}
}
int T,s1,s2;
cin>>T;
while(T--){
cin>>s1>>s2;
if(s1>||s2>||dp[s1][s2]>){
cout<<"No solution"<<endl;
continue;
}
ans.clear();
while(true){
if(s1==&&s2==)break;
ans.pb(v[s1][s2]);
int t=v[s1][s2];
s1-=t;
s2-=t*t;
}
sort(ans.begin(),ans.end());
for(int i=;i<ans.size();i++)cout<<ans[i];
cout<<endl;
}
return ;
}
URAL 1658 Sum of Digits的更多相关文章
- URAL 1658. Sum of Digits(DP)
题目链接 隔了一年零三个月,重新刷URAL,这题挺麻烦的输出路径.输出路径挺扯的,乱写了写乱改改就A了...我本来想用很靠谱,记录每一条路径的,然后输出最小的,结果Tle,然后我使劲水水又过了一组,发 ...
- CodeForces 489C Given Length and Sum of Digits... (贪心)
Given Length and Sum of Digits... 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/F Descr ...
- Sum of Digits / Digital Root
Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...
- Maximum Sum of Digits(CodeForces 1060B)
Description You are given a positive integer nn. Let S(x) be sum of digits in base 10 representation ...
- Codeforces Round #277.5 (Div. 2)C——Given Length and Sum of Digits...
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- CodeForces 1060 B Maximum Sum of Digits
Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...
- Hdu3022 Sum of Digits
Sum of Digits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- codeforces#277.5 C. Given Length and Sum of Digits
C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...
- cf#513 B. Maximum Sum of Digits
B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
随机推荐
- 倒计时60s
- Linux 安装gcc、gcc-c++编译器
安装环境 Red Hat Enterprise Linux Server release 7.3 (Maipo) 方式一:yum安装 使用ISO制作yum源:Linux 使用系统ISO制作yum源 y ...
- Linux基础命令---ziinfo
zipinfo 在不解压的情况下,获取zip压缩文件的的详细信息.zipinfo列出了ZIP档案中有关文件的技术信息,最常见的是在MS-DOS系统上.这些信息包括文件访问权限.加密状态.压缩类型.版本 ...
- linux基础命令---umask
umask 指定创建文件时所需要的权限掩码,掩码的执行权限对于文件没有效果.如果模式以数字开头,则解释为八进制数字:否则解释为符号模式掩码,类似于chmod(1)所接受的模式掩码.如果省略模式,则打印 ...
- Qt 学习之路 2(55):数据库操作
Qt 提供了 QtSql 模块来提供平台独立的基于 SQL 的数据库操作.这里我们所说的“平台独立”,既包括操作系统平台,又包括各个数据库平台.另外,我们强调了“基于 SQL”,因为 NoSQL 数据 ...
- log4j.properties配置详解与实例
log4j.properties配置详解与实例 第一步:加入log4j-1.x.x.jar到lib下. 第二步:在工程的src下下建立log4j.properties.内容如下: #OFF,syste ...
- windows tomcat web应用以及eclipse console乱码解决方法
在windows下,如果vm文件名为UTF-8格式,则显示乱码(velocity写出的不乱码): 改回GBK,则不再乱码.
- 更换 nodejs npm 镜像为 淘宝 镜像
淘宝npm镜像官方介绍文档:https://npm.taobao.org/ ,使用命令在这个官方文档里查询. 安装工具cnpm: $ npm install -g cnpm --registry=ht ...
- 20145127 《Java程序设计》第四次实验报告
在本周,我们进行了Andirod部分的学习,这一次实验是使用Andirod Studio来运行简单的Andirod小程序,并在自己的手机虚拟机上显示自己的学号,为了达到这一效果,我在Andirod S ...
- 20145206邹京儒 web安全基础实践
20145206邹京儒 web安全基础实践 一.实践过程记录 关于WebGoat 1.我们在命令行里执行:java -jar webgoat-container-7.0.1-war-exec.jar运 ...