题目大意:求出T个最小的满足各个位的和为S1,平方和为S2的数。按顺序输出。数的位数大于100或者不存在这样一个数时,输出:No solution。

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
数据规模:T<=10000,1<=S1,S2<=10000。
理论基础:无。
题目分析:用dp[i][j]表示各个位的和为i,平方和为j的最小的数的位数。print[i][j]存储dp[i][j]时最高位选择的数字,用于最后输出答案。
可以肯定的是:S1与S2必然同奇偶。而且:S1<=900,S<=8100,否则无解。
证明同奇偶:观察S1^2的展开式中除了平方项,其余项的系数均为2为偶数(将S1,表示为各个位和的形式展开),所以其余项的和偶数。而剩下的项的和即为S2,所以:S2与S1^2同奇偶。又因为S1与S1^2同奇偶,所以S1与S2同奇偶得证。
所以dp转移方程为:min(dp[i][j],dp[i-k][j-k*k](k<=i,k*k<=j))。
同时,因为使用的判断条件为dp[i][j]<dp[i-k][j-k*k]且k是以升序枚举,所以得出的解保证了最小性。
代码如下:
#include<iostream>
#include<cstring>
#include<string>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<ctime>
#include<vector>
using namespace std;
typedef double db;
#define DBG 0
#define maa (1<<31)
#define mii ((1<<31)-1)
#define ast(b) if(DBG && !(b)) { printf("%d!!|\n", __LINE__); while(1) getchar(); } //调试
#define dout DBG && cout << __LINE__ << ">>| "
#define pr(x) #x"=" << (x) << " | "
#define mk(x) DBG && cout << __LINE__ << "**| "#x << endl
#define pra(arr, a, b) if(DBG) {\
dout<<#arr"[] |" <<endl; \
for(int i=a,i_b=b;i<=i_b;i++) cout<<"["<<i<<"]="<<arr[i]<<" |"<<((i-(a)+1)%8?" ":"\n"); \
if((b-a+1)%8) puts("");\
}
template<class T> inline bool updateMin(T& a, T b) { return a>b? a=b, true: false; }
template<class T> inline bool updateMax(T& a, T b) { return a<b? a=b, true: false; }
typedef long long LL;
typedef long unsigned int LU;
typedef long long unsigned int LLU;
#define M 910
#define N 8110
int m,n,T;
char dp[M][N],print[M][N];
void init()
{
for(int i=0;i<=900;i++)
{
for(int j=0;j<=8100;j++)dp[i][j]=101;
}
dp[0][0]=0;
for(int i=1;i<=900;i++)
{
for(int j=i;j<=8100&&j<=i*i;j++)
{
for(int k=1;k<=9&&k<=i&&k*k<=j;k++)
{
if(i-k>j-k*k)break;
if(dp[i][j]>dp[i-k][j-k*k]+1)
{
print[i][j]=k;
dp[i][j]=dp[i-k][j-k*k]+1;
}
}
}
}
}
int main()
{
init();
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&m,&n);
if(m>n||m>900||n>8100||(m^n)&1||dp[m][n]>100)
{
printf("No solution\n");
continue;
}
while(m&&n)
{
int t=print[m][n];
printf("%d",t);
m-=t;
n-=t*t;
}
puts("");
}
return 0;
}

其中,因为有多个测例,所以选择预处理出答案,效率可能会更高一些。




URAL 1658的更多相关文章

  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. URAL 1658. Sum of Digits(DP)

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

  3. 后缀数组 POJ 3974 Palindrome && URAL 1297 Palindrome

    题目链接 题意:求给定的字符串的最长回文子串 分析:做法是构造一个新的字符串是原字符串+反转后的原字符串(这样方便求两边回文的后缀的最长前缀),即newS = S + '$' + revS,枚举回文串 ...

  4. ural 2071. Juice Cocktails

    2071. Juice Cocktails Time limit: 1.0 secondMemory limit: 64 MB Once n Denchiks come to the bar and ...

  5. ural 2073. Log Files

    2073. Log Files Time limit: 1.0 secondMemory limit: 64 MB Nikolay has decided to become the best pro ...

  6. ural 2070. Interesting Numbers

    2070. Interesting Numbers Time limit: 2.0 secondMemory limit: 64 MB Nikolay and Asya investigate int ...

  7. ural 2069. Hard Rock

    2069. Hard Rock Time limit: 1.0 secondMemory limit: 64 MB Ilya is a frontman of the most famous rock ...

  8. ural 2068. Game of Nuts

    2068. Game of Nuts Time limit: 1.0 secondMemory limit: 64 MB The war for Westeros is still in proces ...

  9. ural 2067. Friends and Berries

    2067. Friends and Berries Time limit: 2.0 secondMemory limit: 64 MB There is a group of n children. ...

随机推荐

  1. Javascript的模块化编程

    随着网站逐渐变成"互联网应用程序",嵌入网页的Javascript代码越来越庞大,越来越复杂. 网页越来越像桌面程序,需要一个团队分工协作.进度管理.单元测试等等......开发者 ...

  2. [原创作品]一个实用的js倒计时器 postby:zhutty.cnblogs.com

    今天做了一个手机短信发送倒计时,额,就是每隔多长时间可以重新发送的功能.贡献出来给园有吐槽点评. //倒计时,time:时长(秒),scb:每秒回调,cb:计时完成回调 var timing = fu ...

  3. 互联网程序设计c++

    地址:ftp.sist.stdu.edu.cn用户名:lzh_hlw20133密码:lzhstdftp端口:2014

  4. monkeyrunner总结

    device=MonkeyRunner.waitForConnection()   //手机连接 result = device.takeSnapshot()    //截图 result.write ...

  5. ViewDragHelper详解

    2013年谷歌i/o大会上介绍了两个新的layout: SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用,其实研究他们的源码你会发现这两个类都运用了ViewDra ...

  6. MVC 5 下,应用log4net收集异常信息

    1.安装log4net. 首先在VS中通过nuget安装logenet,我用的是VS2013,截屏如下:

  7. DataGrid 简单数据绑定实例1

    1.默认数据显示(自动显示列) 后台绑定 //DataGrid 数据绑定 dataGridOne.ItemsSource = _Context.Info.ToList(); 前台定义 <Data ...

  8. myEclipse笔记(1):优化配置

    一.设置字体 Window->Preferences->General->Appearance->Colors and Fonts 在右侧找到”Aa Test Font”双击或 ...

  9. Java系列--第七篇 基于Maven的Android开发实战项目

    本篇是基于<Android应用案例开发大全,吴亚峰等著>的项目开发实例源码,其中有些图片,我做了一些修改,用于个人学习,请勿用于商业. 1, 日程管理专家 mvn archetype:ge ...

  10. 使用HTML5中的Canves标签制作时钟特效

    <!DOCTYPE html > <html> <head> </head> <body> <canvas id="cloc ...