题目大意:求出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. grouth:全栈工程师指南

    语言学习 curl -v baidu.com 域名请求DNS服务器得到网站服务器的IP,http是80端口,https是443端口.查到ip拿到HTML,JS,CSS后就开始渲染这个页面了. 1.Pa ...

  2. 博客换了,比较少用这个博客(http://loid.cf)

    新博客地址: http://loid.cf/

  3. This manual page is part of Xcode Tools version 5.0

    https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/xcodebuild.1.ht ...

  4. Mac Dock 效果及原理(勾股定理)

    这个是苹果机上的 Dock 效果,Windows 上也有一款专门的模拟软件——RocketDock. 代码如下: <!doctype html> <html> <head ...

  5. (3)选择元素——(15)总结(Summary)

    With the techniques that we have covered in this chapter, we should now be able to locate sets of el ...

  6. FusionChart实现金字塔分布图

    1.XML提供数据源 Pyramid.xml: <?xml version="1.0" encoding="UTF-8"?> <chart m ...

  7. 命令行运行android模拟器

    创建模拟器 android create avd --name avd_4.1 --target "android-16" --abi armeabi-v7a Android 4. ...

  8. REST、SOA、SOAP、RPC、ICE、ESB、BPM知识汇总及理解

    转载自处blog.csdn.net/tantexian. SOA: 维基百科解释:SOA:面向服务的软件架构(Service Oriented Architecture),是一种计算机软件的设计模式, ...

  9. CentOS7 安装chrome浏览器

    本篇文章主要记录如何在CentOS7.0上安装chrome. 1.配置yum下载源: 在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo, 并且在该文件中添加 ...

  10. SVN服务器的本地搭建和使用

    用VisualSVN server 服务端和 TortoiseSVN客户端搭配使用. 详细步骤如下 http://www.2cto.com/os/201412/361931.html