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. 牛客网暑期ACM多校训练营(第一场):E-Removal(DP)

    链接:E-Removal 题意:给出序列 s1, s2, ..., sn ,1<=s[i]<=10.问删除m个数后,有多少种不同的序列. 题解:定义dp[i][j]代表长度为i,最末尾的数 ...

  2. loadrunner处理https请求

    录制到的脚本如下: login() { lr_think_time(10); web_url("verifycode.jsp", "URL=https://192.168 ...

  3. 互评Alpha版本——基于spec评论作品

    组名:可以低头,但没必要 组长:付佳 组员:张俊余  李文涛  孙赛佳  田良  于洋  刘欣  段晓睿 一.二次元梦之队----I DO 在测评该项目时,我们组索要了该组的apk程序,安装之后我就开 ...

  4. 汉诺塔python实现

    下载汉诺塔ppt def move(n,A,B,C): if n == 1: print(A,'->',C) else: move(n-1,A,C,B) print(A,'->',C) m ...

  5. MacOS下搭建python环境

    1. 安装须知 Mac OS自身其实已经带有Python,版本为2.7.X,这个Python主要用于支持系统文件和XCode,所以我们在安装新的Python版本时候最好不要影响这部分. 这里就会出现一 ...

  6. Codeforces Round #287 (Div. 2) E. Breaking Good 最短路

    题目链接: http://codeforces.com/problemset/problem/507/E E. Breaking Good time limit per test2 secondsme ...

  7. OC创建对象并访问成员变量

    1.创建一个对象 Car *car =[Car new] 只要用new操作符定义的实体,就会在堆内存中开辟一个新空间 [Car new]在内存中 干了三件事 1)在堆中开辟一段存储空间 2)初始化成员 ...

  8. TCP系列42—拥塞控制—5、Linux中的慢启动和拥塞避免(二)

    在本篇中我们继续上一篇文章wireshark的示例讲解,上一篇介绍了一个综合示例后,本篇介绍一些简单的示例,在读本篇前建议先把上一篇读完,为了节省篇幅,本篇只针对一些特殊的场景点报文进行讲解,不会像上 ...

  9. nginx 简介  http://nginx.org

    Nginx(一) 官方技术文档网站:http://nginx.org Nginx的特性 1:各功能基于模块化设计,扩展性好   2:支持平滑重启,实现应用不下线部署   3:在多并发请求模型下,内存消 ...

  10. JS扫雷原理性代码

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...