跳到正文

题解

U592640 小石子&大木棍 理论非打表正解

Tanlun_np约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年3月13日
收录
2026年8月27日
更新
2026年11月16日
篇幅
约 6 分钟
获赞
1

版本

  1. v1当前

此题其实在出的时候出题者本人除了打表也不会做

但是感谢cenzhiy提供的代码,也是终于给了这道题一个正解,具体算法可以去标签里看。

还有就是快去做我的题单!!!!!!!!!!!!!!!!! 小猫奇遇寄

C++
#include<bits/stdc++.h>
using namespace std;
const int cost[10][2]={{4,1},{2,2},{3,0},{4,1},{3,1},{3,1},{5,1},{2,0},{4,2},{4,1}};
const int MAX_LEN=10;
const int MAX_STICK=100;
const int MAX_STONE=20;
bool dp[MAX_LEN+1][MAX_STICK+1][MAX_STONE+1];
void initDP()
{
    dp[0][0][0]=true;
    for(int i=1;i<=MAX_LEN;i++)
	{
        for(int r=0;r<=MAX_STICK;r++)
		{
            for(int s=0;s<=MAX_STONE;s++)
			{
                if (!dp[i-1][r][s]) continue;
                for(int d=0;d<10;d++)
				{
                    int nr=r+cost[d][0];
                    int ns=s+cost[d][1];
                    if(nr<=MAX_STICK && ns<=MAX_STONE)
					{
                        dp[i][nr][ns]=true;
                    }
                }
            }
        }
    }
}
int constructMin(int len,int needR,int needS)
{
    int r=needR,s=needS;
    long long val=0;
    for(int pos=0;pos<len;pos++)
	{
        int start=(pos==0)?1:0;
        int chosen=-1;
        for(int d=start;d<10;d++)
		{
            int rd=cost[d][0],sd=cost[d][1];
            if (r>=rd && s>=sd && dp[len-pos-1][r-rd][s-sd])
			{
                chosen=d;
                r-=rd;
                s-=sd;
                val=val*10+d;
                break;
            }
        }
        if(chosen==-1) return -1;
    }
    if(val>1000000000) return -1;
    return (int)val;
}
int constructMax(int len,int needR,int needS)
{
    int r=needR,s=needS;
    long long val=0;
    for(int pos=0;pos<len;pos++)
	{
        int chosen=-1;
        for(int d=9;d>=0;d--)
		{
            if(pos==0 && d==0) continue;
            int rd=cost[d][0],sd=cost[d][1];
            if(r>=rd && s>=sd && dp[len-pos-1][r-rd][s-sd])
			{
                chosen=d;
                r-=rd;
                s-=sd;
                val=val*10+d;
                break;
            }
        }
        if(chosen==-1) return -1;
    }
    if(val>1000000000) return -1;
    return (int)val;
}
pair<int,int> solve(long long x,int y)
{
    if(x>MAX_STONE) return {-1,-1};
    int minAns=-1,maxAns=-1;
    for(int len=1;len<=MAX_LEN;len++)
	{
        if(dp[len][y][x])
		{
            int cand=constructMin(len,y,x);
            if(cand!=-1)
			{
                minAns=cand;
                break;
            }
        }
    }
    for(int len=MAX_LEN;len>=1;len--)
	{
        if(dp[len][y][x])
		{
            int cand=constructMax(len,y,x);
            if(cand!=-1)
			{
                maxAns=cand;
                break;
            }
        }
    }
    return {minAns,maxAns};
}
int main()
{
    initDP();
    long long x1,x2;
    int y1,y2;
    cin>>x1>>y1>>x2>>y2;
    auto ans1=solve(x1,y1);
    auto ans2=solve(x2,y2);
    cout<<ans1.first<<" "<<ans1.second<<endl;
    cout<<ans2.first<<" "<<ans2.second<<endl;
    return 0;
}

小猫快急死了,你快帮他做吧!

原文