hdu 3022 Sum of Digits

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=1;i<=9;++i) dp[i][i*i]=1,dp2[i][i*i]=i;
    for(int i=1;i<N;++i)
        for(int j=1;j<M;++j)
            if(dp[i][j] && dp[i][j]!=100)
                for(int k=1;k<=9;++k)
                    if(!dp[i+k][j+k*k] || dp[i+k][j+k*k]>dp[i][j]+1)
                    {
                        dp[i+k][j+k*k]=dp[i][j]+1;
                        dp2[i+k][j+k*k]=k;
                    }
                    else if(dp[i+k][j+k*k]==dp[i][j]+1) 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 0;
}

原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8453836.html

时间: 2024-10-10 06:08:45

hdu 3022 Sum of Digits的相关文章

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

传送门 Description You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the d

Sum of Digits is Prime

Sum of Digits is Prime Daoyi Peng August 19, 2014 For an integer $q\geqslant2$ let $s_q(n)$ denote the $q$-ary sum-of-digits function of a non-negative integer $n$, that is, if $n$ is given by its $q$-ary digits expansion $n=\sum\limits_{k=0}^{r} a_k

hdu 1258 Sum It Up (dfs+路径记录)

Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3953    Accepted Submission(s): 2032 Problem Description Given a specified total t and a list of n integers, find all distinct sums usi

HDU 4704 Sum( 费马小定理 )

HDU 4704 Sum( 费马小定理 ) 理解能力果然拙计,,题目看半天没懂什么意思. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define MOD 1000000007 char str[100010]; LL fast_mod( LL a, int b) { LL res = 1; while( b )

hdu 4704 Sum (费马小定理+快速幂)

//(2^n-1)%mod //费马小定理:a^n ≡ a^(n%(m-1)) * a^(m-1)≡ a^(n%(m-1)) (mod m) # include <stdio.h> # include <algorithm> # include <string.h> # define mod 1000000007 using namespace std; __int64 pow(__int64 n) { __int64 p=1,q=2; while(n) { if(n%

Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base with

CF 489 C Given Length and Sum of Digits... 贪心

题目链接:http://codeforces.com/problemset/problem/489/C 题目大意:给定位数和各个位的和,问满足条件数字的最大值,最小值. 解题思路:模拟即可.主要是细节判断. 代码: 1 const int inf = 0x3f3f3f3f; 2 const int maxn = 1e2 + 5; 3 int m, s; 4 char ans1[maxn], ans2[maxn]; 5 6 void solve(){ 7 memset(ans1, 0, sizeo

HDU 1024Max Sum Plus Plus(最大m字段和)

/* 动态转移方程:dp[i][j]=max(dp[i-1]+a[i], max(dp[t][j-1])+a[i]) (j-1<=t<i) 表示的是前i个数j个字段和的最大值是多少! */ 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define N 10000 5 using namespace std; 6 7 int dp[N][N], num[N]; 8 9 int m

8504. Sum of Digits

Sum of Digits Having watched the last Harry Potter film, little Gerald also decided to practice magic. He found in his father's magical book a spell that turns any number in the sum of its digits. At the moment Gerald learned that, he came across a n