URAL 1658

思路:

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的更多相关文章

  1. URAL 1658. Sum of Digits(DP)

    题目链接 隔了一年零三个月,重新刷URAL,这题挺麻烦的输出路径.输出路径挺扯的,乱写了写乱改改就A了...我本来想用很靠谱,记录每一条路径的,然后输出最小的,结果Tle,然后我使劲水水又过了一组,发 ...

  2. 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 ...

  3. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Hdu3022 Sum of Digits

    Sum of Digits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Javascript-涨工资案例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. tfs代码上传到server并下载到新位置

    1.svn与git代码管理原理基本一致,基于文档管理,能看到文件代码,通过设置文件的只读属性来控制代码. 而tfs是基于sqlserver及lock来管理,看不见代码文件 2.tfs没有自己的用户管理 ...

  3. PKU2503_map应用

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  4. 原生 ajax

    1.创建XMLHttpRequest对象 var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Ope ...

  5. Linux基础命令---yes

    yes 反复的输出指定的字符串,直到手动停止.此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora.   1.语法      yes [STR ...

  6. redis 哨兵机制环境搭建

    Redis哨兵机制,一主二从 注:Redis哨兵切换,建议一主多从 一.一主二从 教程步骤:https://www.cnblogs.com/zwcry/p/9046207.html 二.哨兵配置(se ...

  7. EXKMP

    (我和lesphere,reverse研究了这个东西一上午)QAQ kmp是求字符串S的任意前缀与字符串T的最长的相同的前缀和后缀 exkmp第求字符串S的任意后缀与字符串T的最长公共前缀 与kmp相 ...

  8. 怎么归档老日志的shell脚本

    本脚本来自有学习阿铭的博文学习:工作中,需要用到日志切割logrotate,按照各自的需要切割.定义保留日志.提示:本文中的S全部都$符,不要问为什么,马云爸爸的社区就这样. #用途:日志切割归档.按 ...

  9. 编写第一个微信小程序界面

    编写第一个微信小程序界面 不忘初心,方得始终:初心易得,始终难守. 传统的 web 结构 小程序文件目录结构 小程序页面层级结构 编写第一个小程序 1. 创建小程序目录结构 2. 编写代码 welco ...

  10. 20145305 《网络对抗》Web基础

    实践过程及结果截图 Apache 简单的网页编写 javascript相关 PHP测试 MySQL基础 php+mysql 登录成功信息: 登录失败信息: SQL注入 XSS攻击 基础问题回答 (1) ...