http://acm.hdu.edu.cn/showproblem.php?pid=3022

题意:

最多不超过10000组数据,每组数据给定两个数n,m,求一个最小的数,使得该数每一位之和等于n,每一位的平方和等于m。
若无解或者答案超过100位,输出no solution。
 
n最大=900,m最大=8100
dp[i][j]表示和为i,平方和为j的最少位数
dp2[i][j]表示和为i,平方和为j的最小首位数
竟然一直在思考有前导0怎么办,最优解肯定不能出现0啊啊啊
 
#include<cstdio>
#include<algorithm> using namespace std; #define N 901
#define M 8101 int dp[N][M],dp2[N][M]; void pre()
{
for(int i=;i<=;++i) dp[i][i*i]=,dp2[i][i*i]=i;
for(int i=;i<N;++i)
for(int j=;j<M;++j)
if(dp[i][j] && dp[i][j]!=)
for(int k=;k<=;++k)
if(!dp[i+k][j+k*k] || dp[i+k][j+k*k]>dp[i][j]+)
{
dp[i+k][j+k*k]=dp[i][j]+;
dp2[i+k][j+k*k]=k;
}
else if(dp[i+k][j+k*k]==dp[i][j]+) dp2[i+k][j+k*k]=min(dp2[i+k][j+k*k],k);
} int main()
{
pre();
int T,n,m,d;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
if(n>=N || m>=M || !dp[n][m]) printf("No solution\n");
else
{
while(n)
{
printf("%d",d=dp2[n][m]);
n-=d;
m-=d*d;
}
putchar('\n');
}
}
return ;
}

hdu 3022 Sum of Digits的更多相关文章

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

  2. Sum of Digits / Digital Root

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

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

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

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

  6. Hdu3022 Sum of Digits

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

  7. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  8. codeforces#277.5 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. cf#513 B. Maximum Sum of Digits

    B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...

随机推荐

  1. sudo apt-get update 去除设置的代理

    今天想装个软件(wine),使用 sudo apt-get update 命令时,发现给出很多Ign 语句,总出现 Connecting to proxy.http://10.0.126.1:1312 ...

  2. Linux/Mac 挂载远程服务器目录到本地

    1. 安装 sudo apt-get installsshfs 2. 创建SSHFS 挂载目录 sudo mkdir/mnt/siyuan 3.使用SSHFS 挂载远程的文件系统 sudo sshfs ...

  3. PAT甲题题解-1017. Queueing at Bank (25)-模拟

    有n个客户和k个窗口,给出n个客户的到达时间和需要的时长有空闲的窗口就去办理,没有的话就需要等待,求客户的平均时长.如果在8点前来的,就需要等到8点.如果17点以后来的,则不会被服务,无需考虑. 按客 ...

  4. 团队作业Week5

    每个团队开一个讨论会,协商讨论团队贡献分的分配方式.每个团队的团队贡献分为50分/人.每个人分数不能相同,请详细说明分数的分配规则. 可参考这个博客. 截止时间:2014-10-27

  5. Java实验报告(实验五)

    课程:Java程序设计                         班级:1351            姓名:王玮怡      学号:20135116 成绩:             指导教师: ...

  6. 作业五:分析system_call中断处理过程

    分析system_call中断处理过程 一.MesuSO增加getpid和getpid-asm 二.使用GDB跟踪系统调用内核函数sys_getpid 分析system_call中断处理过程 使用gd ...

  7. python 安装多个包/pip用法

    列出已安装的包 pip freeze or pip list 导出requirements.txt pip freeze > <目录>/requirements.txt 安装包 在线 ...

  8. Android开发环境的发展演变调研

    Android开发环境的发展演变调研 前几年比较多的方法是用JDK+eclipse+ADT,该方法除了要配置JDK的路径之外, 还要在eclipse里面打开SDK Manage进行相应的操作.不过近两 ...

  9. WPF和js交互 调用窗体中的方法

    public partial class WebTest: Window { private void Window_ContentRendered(object sender, EventArgs ...

  10. 基于 Redis 做分布式锁

    基于 REDIS 的 SETNX().EXPIRE() 方法做分布式锁 setnx() setnx 的含义就是 SET if Not Exists,其主要有两个参数 setnx(key, value) ...