题目链接

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

 #include <cstring>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define INF 10000000
int dp[][];
int pre[][];
int s[];
struct node
{
int a,b;
};
void spfa()
{
int i,a,b;
queue<node> que;
node temp,u,v;
for(i = ; i < ; i ++)
{
temp.a = i;
temp.b = i*i;
dp[i][i*i] = ;
pre[i][i*i] = i;
que.push(temp);
}
while(!que.empty())
{
u = que.front();
a = u.a;
b = u.b;
que.pop();
if(a > ) continue;
if(b > ) continue;
for(i = ; i < ; i ++)
{
v.a = i+a;
v.b = i*i+b;
if(i + a > ) continue;
if(i*i + b > ) continue;
if(dp[v.a][v.b] > dp[u.a][u.b] + )
{
pre[v.a][v.b] = i;
dp[v.a][v.b] = dp[u.a][u.b] + ;
que.push(v);
}
}
}
return ;
} int main()
{
int t,n,m,i,j;
int top;
for(i = ; i <= ; i ++)
{
for(j = ; j <= ; j ++)
{
dp[i][j] = INF;
pre[i][j] = INF;
}
}
spfa();
scanf("%d",&t);
top = ;
while(t--)
{
scanf("%d%d",&n,&m);
if(n > ||m > )
{
printf("No solution\n");
continue;
}
else if(dp[n][m] > )
{
printf("No solution\n");
continue;
}
int top = ;
while(n&&m)
{
i = pre[n][m];
s[top++] = i;
n = n - i;
m = m - i*i;
}
for(i = top-;i >= ;i --)
printf("%d",s[i]);
printf("\n");
}
return ;
}

URAL 1658. Sum of Digits(DP)的更多相关文章

  1. URAL 1658 Sum of Digits

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

  2. URAL1658. Sum of Digits(DP)

    链接 这题卡了挺久了 昨天试着用类似dfs的方法直接TLE在第二组 看了下题解,,发现s1,s2的范围是个幌子..100位最大的s1900 s28100 觉得s1s2太大不敢开二维.. 这样就简单了 ...

  3. Hdu3022 Sum of Digits

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

  4. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

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

  6. Sum of Digits / Digital Root

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

  7. ACdreamOJ 1154 Lowbit Sum (数字dp)

    ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:pid=1154" target="_blank" style="color ...

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

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

随机推荐

  1. Avalon学习

    1.认识AvalonAvalon是一个简单易用的迷你的MVVM框架,作者是博客园的司徒正美,去哪儿.搜狐等等都用这个框架.没有任何依赖,兼容性非常好,支持IE6,不到5000行,压缩后不到50KB.官 ...

  2. Delphi字符串的基本操作与常用函数

    参考:http://www.cnblogs.com/pchmonster/archive/2011/12/16/2290034.html 结合这个博客一起学习:http://www.cnblogs.c ...

  3. Delphi的Win32的API调用简单介绍

    1.     介绍Win32 API和Win32系统.还要讨论Win32系统的功能以及它与16位系统在功能上的几个主要区别.只是让对Win32系统有一个基本的了解.当已经基本了解Win32操作后,就可 ...

  4. 取得表中数据的insert语句

    Build Insert Statements for the Existing Data in Tables 下面这个脚本实现了取得一个非空表中的所有insert语句 This script bui ...

  5. [LeetCode] Ugly Number II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  6. PHP+Nginx环境搭配

    一.Nginx安装 nginx可以使用各平台的默认包来安装,本文是介绍使用源码编译安装,包括具体的编译参数信息. 正式开始前,编译环境gcc g++ 开发库之类的需要提前装好,这里默认你已经装好. u ...

  7. JavaScript - 引用类型

    对象在JavaScript中被称为引用类型的值. 引用类型和传统面向对象设计中的类相似,但实现不同. Object是一个基础类型,其他所有类型都从Object继承了基本的行为. Array类型,Dat ...

  8. css 全局 兼容性问题

    css 笔记 第一种:常用的全局CSS属性设置 //参考大型网站,如凤凰网 (1)清除所有的标记的内外边距 body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,p ...

  9. android 入门-Service实时向Activity通过BroadcastReceiver传递数据

    引文: http://www.cnblogs.com/linjiqin/p/3147764.html <RelativeLayout xmlns:android="http://sch ...

  10. linux使用读写锁pthread_rwlock_t

    转自:http://blog.csdn.net/onlyou930/article/details/6755593 使用读写锁 配置读写锁的属性之后,即可初始化读写锁.以下函数用于初始化或销毁读写锁. ...