Sum of Digits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 810    Accepted Submission(s): 220

Problem Description
Petka thought of a positive integer n and reported to Chapayev the sum of its digits and the sum of its squared digits. Chapayev scratched his head and said: "Well, Petka, I won't find just your number, but I can find the smallest fitting number." Can you do the same?
Input
The first line contains the number of test cases t (no more than 10000). In each of the following t lines there are numbers s1 and s2 (1 ≤ s1, s2 ≤ 10000) separated by a space. They are the sum of digits and the sum of squared digits of the number n.
Output
For each test case, output in a separate line the smallest fitting number n, or "No solution" if there is no such number or if it contains more than 100 digits.
Sample Input
4
9 81
12 9
6 10
7 9
Sample Output
9
No solution
1122
111112
Source
题目大意:求一个数字,使得这个数字每个数位上的数字和为s1,平方和为s2,输出最小的满足这个要求的数字,如果不存在,则输出No solution
分析:好题!
   显然是一个dp.状态的每一维都很好确定,但它具体表示什么呢? 这就比较头疼了.令f[i][j]表示和为i,平方和为j的数的最小位数. g[i][j]表示和为i,平方和为j,最小位数为f[i][j]的最小首位数. 如果能求得这两个数组,每次输出答案的时候先输出g[s1][s2],然后s1 -= g[s1][s2],s2 -= g[s1][s2],直到s1和s2中有一个等于0.
   怎么转移呢?f的转移非常简单,g的定义涉及到f,不好单独处理.  一个比较好的方法是把f和g放在一起处理. 每当f能转移的时候,就转移g.比如f[i][j]转移到f[i + k][j + k * k],那么和为i + k,j + k * k的最小位数在这个时候肯定是确定的,就是f[i + k][j + k * k],因为k是从小到大枚举的,所以g[i + k][j + k * k]也可以转移.g[j + k][j + k * k] = k. 如果f[i + k][j + k * k] == f[i][j] + 1, g的条件是满足了,但是最小首位数不一定是k,因为之前求出了f[i+k][j + k * k]是从其它的状态转移过去的,这个时候取个min.
   这道题的状态表示真的挺神奇的. 状态表示的东西必须要能够得到答案和转移,并且还要满足题目的要求(最小). 考虑如何使得数最小,先是数位最少,再是首位最小.根据这两个最小就可以定义得到状态了.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; int T,s1,s2,f[][],g[][]; void solve()
{
for (int i = ; i <= ; i++)
f[i][i * i] = ,g[i][i * i] = i;
for (int i = ; i <= ; i++)
for (int j = ; j <= ; j++)
if (f[i][j])
{
for (int k = ; k <= ; k++)
{
if (!f[i + k][j + k * k] || f[i + k][j + k * k] > f[i][j] + )
{
f[i + k][j + k * k] = f[i][j] + ;
g[i + k][j + k * k] = k;
}
else if (f[i + k][j + k * k] == f[i][j] + )
g[i + k][j + k * k] = min(g[i + k][j + k * k],k);
}
}
} int main()
{
solve();
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&s1,&s2);
if (s1 > || s2 > || !f[s1][s2] || f[s1][s2] > )
printf("No solution\n");
else
{
while (s1 && s2)
{
printf("%d",g[s1][s2]);
int t = g[s1][s2];
s1 -= t;
s2 -= t * t;
}
printf("\n");
}
} return ;
}
 

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

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

  8. CodeForces 489C Given Length and Sum of Digits... (dfs)

    C. Given Length and Sum of Digits... time limit per test 1 second memory limit per test 256 megabyte ...

  9. Codeforces Round #277.5 (Div. 2)-C. Given Length and Sum of Digits...

    http://codeforces.com/problemset/problem/489/C C. Given Length and Sum of Digits... time limit per t ...

随机推荐

  1. NO.07--我跟“ 币乎 ”的那些事

    文章开头给大家安利一款app吧,就是我标题提到的,‘币乎’,一个近似于虚拟货币的论坛吧,大家可以下载试试,发文章点赞赚钱,... 好了,开始说一说今天的正题吧: 这些事情说起来其实挺惭愧的,但也不是什 ...

  2. DNS递归查询与迭代查询

    注:一般TCP/IP的应用层或者OSI的会话.表示.应用层把数据称为数据或者信息,到了传输层把数据称为报文,到了最底层就是比特流了也就是字节流 DNS递归查询与迭代查询   基础知识 1.域名系统 2 ...

  3. JAVA学习笔记--初识容器类库

    一.前言 JAVA中一切皆为对象,因而,持有对象显得尤为重要. 在JAVA中,我们可以通过创建一个对象的引用的方式来持有对象: HoldingObject holding; 也可以创建一个对象数组来持 ...

  4. LeetCode 148——排序链表

    1. 题目 2. 解答 2.1 快速排序 可参考 快速排序和归并排序 中的第一种快速排序思想,与在数组中排序有两点不同. 第一,我们需要取最后一个元素作为主元,在数组中可以直接访问到最后一个元素,但在 ...

  5. 性能度量RMSE

    回归问题的典型性能度量是均方根误差(RMSE:Root Mean Square Error).如下公式. m为是你计算RMSE的数据集中instance的数量. x(i)是第i个实例的特征值向量 ,y ...

  6. 第五章—if语句

    5-1 条件测试 :编写一系列条件测试:将每个测试以及你对其结果的预测和实际结果都打印出来.你编写的代码应类似于下面这样: car = 'subaru' print("Is car == ' ...

  7. Github上的一些高分Qt开源项目【多图】

    游戏2D地图编辑器: 著名的TileMap编辑器,做2D游戏开发的一定不会陌生. Go 语言的IDE: Go语言的集成开发环境. Clementine Music Player: 功能很完善且跨平台支 ...

  8. 数据时代的的企业管理 记SAP商业同略会

    [PConline 资讯]在2012 SAP中国商业同略会城市论坛深圳站上,自SAP中国的萧洁云总裁和张志琦先生,对SAP中国的战略.SAP的技术战略,以及SAP对于行业趋势分析与媒体进行了沟通,对数 ...

  9. KETTLE元数据表

    表名 说明 R_CLUSTER R_CLUSTER_SLAVE R_CONDITION R_DATABASE 数据库连接信息 R_DATABASE_ATTRIBUTE 数据库属性 R_DATABASE ...

  10. sprint2 团队贡献分

    团队名:在考虑 团队贡献分: 102 杨晶晶:17 106 邹育萍:18 114 纪焓:16 116 黄敏鹏:28 117 郑培轩:26 138 曾昱霖:15 最新项目的github地址: https ...