URAL 1658. Sum of Digits(DP)
隔了一年零三个月,重新刷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)的更多相关文章
- 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 ...
- URAL1658. Sum of Digits(DP)
链接 这题卡了挺久了 昨天试着用类似dfs的方法直接TLE在第二组 看了下题解,,发现s1,s2的范围是个幌子..100位最大的s1900 s28100 觉得s1s2太大不敢开二维.. 这样就简单了 ...
- Hdu3022 Sum of Digits
Sum of Digits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- 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 ...
- ACdreamOJ 1154 Lowbit Sum (数字dp)
ACdreamOJ 1154 Lowbit Sum (数位dp) ACM 题目地址:pid=1154" target="_blank" style="color ...
- 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 ...
随机推荐
- 设计模式学习之组合模式(Composite,结构型模式)(10)
转载地址:http://www.cnblogs.com/zhili/p/CompositePattern.html 一.引言 在软件开发过程中,我们经常会遇到处理简单对象和复合对象的情况,例如对操作系 ...
- ExcelReport第三篇:扩展元素格式化器
导航 目 录:基于NPOI的报表引擎——ExcelReport 上一篇:ExcelReport源码解析 概述 上篇中已介绍了ExcelRepor的架构,本篇将通过例子讲述如何扩展元素格式化器以满足 ...
- c++ 左值右值 函数模板
1.先看一段代码,这就是一种函数模板的用法,但是红色的部分如果把a写成a++或者写成一个常量比如1,都是编译不过的,因为如果是a++的话,实际上首先是取得a的 值0,而0作为一个常量没有地址.写成1也 ...
- Java后端WebSocket的Tomcat实现
转自:http://blog.chenzuhuang.com/archive/28.html 文章摘要随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5 ...
- BZOJ 1503: [NOI2004]郁闷的出纳员 splay
1503: [NOI2004]郁闷的出纳员 Description OIER公司是一家大型专业化软件公司,有着数以万计的员工.作为一名出纳员,我的任务之一便是统计每位员工的工资.这本来是一份不错的工作 ...
- wpf window set window的owner
[DllImport("user32.dll")] public static extern IntPtr GetAncestor(IntPtr hWnd, int f ...
- barabasilab-networkScience学习笔记6-evolving networks
第一次接触复杂性科学是在一本叫think complexity的书上,Allen博士很好的讲述了数据结构与复杂性科学,barabasi是一个知名的复杂性网络科学家,barabasilab则是他所主导的 ...
- [Windows驱动开发](四)内存管理
一.内存管理概念 1. 物理内存概念(Physical Memory Address) PC上有三条总线,分别是数据总线.地址总线和控制总线.32位CPU的寻址能力为4GB(2的32次方)个字 ...
- 如何在MFC中添加对话栏
- OpenGL大作业
GLfloat light0_position[] = { 15.0,15.0,15.0,10.0 };//定义光源位置 103glLightfv(GL_LIGHT0, GL_POSITION, li ...