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 ...
随机推荐
- html07
1.复习js的外部对象,DOM,BOMBOM window -location -Location对象 : href reload() -history -History :back() forwar ...
- CAS机制
##################################################################### 我们知道多线程操作共享资源时,会出现三个问题:可见性.有序性 ...
- ubuntu常用指令
总结一下常用的linux指令. mark一个linux指令学习和速查的网站:http://man.linuxde.net/ (0) su和sudo:得到root权限 su 切换到root用户 sudo ...
- Shell脚本实现检测某ip网络畅通情况,实战用例
Shell脚本实现检测某ip网络畅通情况,实战用例 环境准备,linux shell 发送email 邮件:1.安装sendmailyum -y install sendmail安装好sendmail ...
- sp3485推荐电路(转)
源: sp3485推荐电路 注意:转自电子发烧友 转:485通信自动收发电路 转: RS485收发的3种典型电路-重点-自动收发电路
- Python 自学基础(四)——time模块,random模块,sys模块,os模块,loggin模块,json模块,hashlib模块,configparser模块,pickle模块,正则
时间模块 import time print(time.time()) # 当前时间戳 # time.sleep(1) # 时间延迟1秒 print(time.clock()) # CPU执行时间 p ...
- MFC制作OCX
1.新建工程 注意选择显示时注册,免得后面又去手动注册 2.工程解释,一般ocx是看类视图而不是解决方案 ①.xxxApp:类似整个工程的入口,有xxxApp.h和xxxApp.cpp,工程的初始化, ...
- script 加载顺序问题的延展研究
今天群里有人问为什么会出现脚本的加载顺序与定义脚本顺序不一致的问题,这个问题引起了我的好奇,经过一番调研,有了这篇文章. 这是一个伪命题吗? 首先,W3C 推荐 script 脚本应该被立即加载和执行 ...
- 最大子段和SP1716GSS3 线段树
前言 spoj需要FQ注册,比较麻烦,大家就在luogu评测吧 题目大意: $n$ 个数,$q$ 次操作 操作$0 _ x_ y$把$A_x$ 修改为$y$ 操作$1 _ l _r$询问区间$[l, ...
- 使用CLR Profiler查看C#运行程序的内存占用情况
http://blog.csdn.net/wy3552128/article/details/8158938 https://msdn.microsoft.com/en-us/library/ff65 ...