Ural 1658 Sum of Digits

求最小的,各个数位的和为 S1 ,各个数位的平方和为 S2 的数


一眼看下来只会求方案数啊GG,最小的该怎么办呢

然后可以想到如果一个数的位数少,他肯定是短的

所以我们要找到最短位的那个数应该就是答案了

dp的时候顺便维护一下是用哪个数字转移过来的,回溯过去记录答案

最后再把拥有的数从小到大排一下,应该就是最优的了


但是这样空间复杂度是1e10啊QAQ,还是要GG啊

你或许注意到了,要是数位大于100,也是无解

也就是只有 900×8100 的状态,然后就可以很开心的做了


最后一个对空间的优化是不用记录上一次的坐标,而是通过转移过来的数字来计算坐标

#include<bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;
const int maxn = 1000;
const int maxm = 9000;
int dp[maxn][maxm],v[maxn][maxm];
vector<int> ans;

int main(){
    memset(dp,0x3f,sizeof(dp));
    dp[0][0] = 0;
    for(int i=1;i<=900;i++){
        for(int j=1;j<=8100;j++){
            for(int k=1;k<=9;k++){
                int x = i-k,y = j-k*k;
                if(x>=0 && y>=0){
                    if(dp[i][j] > dp[x][y]+1){
                        dp[i][j] = dp[x][y]+1;
                        v[i][j] = k;
                    }
                }
            }
        }
    }
    int s1,s2;
    int T;
    scanf("%d",&T);
    while(T-- && ~scanf("%d %d",&s1,&s2)){
        if(s1>900 || s2>8100 || dp[s1][s2]>100){
            puts("No solution");
        }
        else{
            ans.clear();
            int x = s1,y = s2;
            while(x && y){
                ans.push_back(v[x][y]);
                int t = v[x][y];
                x -= t,y -= t*t;
            }
            sort(ans.begin(),ans.end());
            for(auto x : ans){
                printf("%d",x);
            }
            puts("");
        }
    }
    return 0;
}

ps:无解的情况判断错误,不应该是这个地方没有被dp扫到,而是dp[s1][s2] > 100

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值