链接

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

这样就简单了 类似背包 dp[s1][s2]表示组成s2s2最少的位数 其实就是装进去多少个数字 正好把s1s2装满

把DP部分预处理之后放在外面  不然会超时

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define INF 0xfffffff
int s1,s2;
int dp[][];
int path[],flag;
void dfs(int ss1,int ss2,int u,int v)
{
path[u] = v;
if(flag) return ;
int i;
if(u==&&ss1==&&ss2==)
{
flag = ;
for(i = dp[s1][s2] ; i>=; i--)
printf("%d",path[i]);
puts("");
return ;
}
if(ss1<=||ss2<=)
return ;
u--;
for(i = ; i <= ; i++)
{
if(ss1-i<||ss2-i*i<)
continue;
if(dp[ss1-i][ss2-i*i]+==dp[ss1][ss2])
dfs(ss1-i,ss2-i*i,u,i);
if(flag) break;
}
}
int main()
{
int i,j,g,t;
scanf("%d",&t);
for(i = ; i <= ; i++)
for(j = ; j <= ; j++)
dp[i][j] = INF;
dp[][] = ;
for(i = ; i <= ; i++)
for(j = i ; j <= ; j++)
for(g = i*i ; g <= ; g++)
dp[j][g] = min(dp[j][g],dp[j-i][g-i*i]+);
while(t--)
{
scanf("%d%d",&s1,&s2);
if(s1>||s2>)
{
printf("No solution\n");
continue;
}
flag = ;
if(dp[s1][s2]==INF||dp[s1][s2]>)
{
printf("No solution\n");
}
else
{
for(i =; i <= ; i++)
{
if(s1-i<||s2-i*i<)
continue;
if(dp[s1-i][s2-i*i]+==dp[s1][s2])
dfs(s1-i,s2-i*i,dp[s1][s2],i);
if(flag) break;
}
}
}
return ;
}

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

  1. URAL 1658. Sum of Digits(DP)

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

  2. Hdu3022 Sum of Digits

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

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

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

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

  5. Sum of Digits / Digital Root

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

  6. ACdreamOJ 1154 Lowbit Sum (数字dp)

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

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

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

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

随机推荐

  1. Css颜色定义的方法汇总color属性设置方式

    颜色的定义方式用rgb()里面带上十进制的数字来定义. color:rgb(211,123,135); 用预定义的颜色名称. color:red; rgba()最后一个参数是不透明度. color:r ...

  2. OpenJudge 2756 二叉树

    1.链接地址: http://bailian.openjudge.cn/practice/2756/ 2.题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 如 上图所示,由正整数1 ...

  3. Nginx+keepalived实现负载均衡

    Nginx的优点是: 1.工作在网络的7层之上,可以针对http应用做一些分流的策略,比如针对域名.目录结构,它的正则规则比HAProxy更为强大和灵活,这也是它目前广泛流行的主要原因之一,Nginx ...

  4. javascript学习笔记2

    二.下列哪些变量是不正确的  说明原因 var a1; var b1 = 3; c1 ='good'; var d1 = c1 = e1;   ×  连等必须赋值 var g1 = 'hei' goo ...

  5. opengl Test

    LIBS += -lGL -lGLU -lglut -lGLEW #include <GL/glut.h> #include <iostream> using namespac ...

  6. codeforces 615D - Multipliers

    Multipliers 题意:给定一个2e5范围内的整数m,之后输入m个2e5内的素数(当然可以重复了),问把这些输入的素数全部乘起来所得的数的约数的乘积mod(1e9+7)等于多少? 思路:对题目样 ...

  7. 2016031901 - U盘安装ubuntu系统

    使用U盘安装ubuntu系统 01.进入u盘安装 个人使用的是闪迪U盘安装 02. 个人重装ubuntu 03.进入ubuntu安装界面 04.进入语言界面 05.准备安装ubuntu 06.ubun ...

  8. uCGUI窗口的创建过程分析

    一.相关结构体和变量 窗口管理结构体 /* 窗口管理结构体 共30个字节 */ struct WM_Obj { GUI_RECT Rect; //窗口尺寸(x0,y0,x1,y1) 8个字节 GUI_ ...

  9. Google Code Jam 2014 Qualification 题解

    拿下 ABD, 顺利晋级, 预赛的时候C没有仔细想,推荐C题,一个非常不错的构造题目! A Magic Trick 简单的题目来取得集合的交并 1: #include <iostream> ...

  10. android:Faild to install,你的主机中的软件终止了一个连接错误解决

    当在用真机调试android程序时出现Faild to install,你的主机中的软件终止了一个连接错误时可以这样解决: 在手机开启usb调试和安装未知来源软件的情况下: 1:先查进入任务管理器查看 ...