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. 使用C#创建WCF服务控制台应用程序

    本文属于原创,转载请注明出处,谢谢! 一.开发环境 操作系统:Windows 10 开发环境:VS2015 编程语言:C# IIS版本:10.0.0.0 二.添加WCF服务.Internet Info ...

  2. [穷尽]ADO.NET连接字符串

    微软提供的四种数据库连接方式: System.Data.OleDb.OleDbConnection System.Data.SqlClient.SqlConnection System.Data.Od ...

  3. git笔记:通过给grunt-inline打tag看tag操作

    晚上review了下grunt-inline的issues,看到有个兄弟pull request,修正了0.3.0版本的一个bug.于是就merge了下,然后发布了0.3.1版本(这里). npm p ...

  4. Asp.Net_的传值跟存储值操作

    页面传值是学习asp.net初期都会面临的一个问题,总的来说有页面传值.存储对象传值.ajax.类.model.表单等.但是一般来说,常用的较简单有QueryString,Session,Cookie ...

  5. LintCode——数字统计

    数字统计:计算数字k在0到n中的出现的次数,k可能是0~9的一个值 样例:例如n=12,k=1,在 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],我们发现1出现 ...

  6. 再探Redux Middleware

    前言 在初步了解Redux中间件演变过程之后,继续研究Redux如何将中间件结合.上次将中间件与redux硬结合在一起确实有些难看,现在就一起看看Redux如何加持中间件. 中间件执行过程 希望借助图 ...

  7. Linux 第五周 实验: 分析system_call中断处理过程

    姬梦馨 原创博客 <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 使用gdb跟踪分析一个系统调用内核函数 ...

  8. 20170929php

    这是之前学习PHP类使用的代码 <?phpclass animal{ var $name="1"; var $sex="2"; public static ...

  9. 贝叶斯先验解释l1正则和l2正则区别

    这里讨论机器学习中L1正则和L2正则的区别. 在线性回归中我们最终的loss function如下: 那么如果我们为w增加一个高斯先验,假设这个先验分布是协方差为 的零均值高斯先验.我们在进行最大似然 ...

  10. PSP(3.16——3.22)以及周记录

    3.17 13:30 14:45 15 60 讨论班 A Y min 14:50 17:05 5 130 得到设备 Cordova 蓝牙连接 A Y min 23:15 23:45 5 25 英语百词 ...