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

本文介绍了一种通过贪心算法构造给定长度内的最大和最小数值的方法,利用9的倍数特性快速形成解决方案,并通过代码实现展示了具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

贪心

先构造最大的,将s拆成9的倍数和余数,前面排列9的倍数个9再加上一个余数,最后排0

然后把最大的放余数位置的数减去1,然后后面填上0,最后填上1,再做一次翻转,得到最小的


#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>


using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;

#define CLR(x,y) memset(x,y,sizeof(x))
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))
#define MID(x,y) (x+((y-x)>>1))
#define eps 1e-9
#define INF 0x3f3f3f3f
#define LLINF 1LL<<62

template<class T>
inline bool read(T &n)
{
    T x = 0, tmp = 1; char c = getchar();
    while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();
    if(c == EOF) return false;
    if(c == '-') c = getchar(), tmp = -1;
    while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();
    n = x*tmp;
    return true;
}
template <class T>
inline void write(T n)
{
    if(n < 0)
    {
        putchar('-');
        n = -n;
    }
    int len = 0,data[20];
    while(n)
    {
        data[len++] = n%10;
        n /= 10;
    }
    if(!len) data[len++] = 0;
    while(len--) putchar(data[len]+48);
}
//-----------------------------------

int main()
{
    int n,s;
    read(n),read(s);
    if(n==1 && !s){cout<<0<<' '<<0;return 0;}
    if(n*9<s || !s){cout<<-1<<' '<<-1;return 0;}
    string st;
    while(s>0)
    {
        st+=min(9,s)+'0';
        s-=min(9,s);
    }
    string st1=st;
    while(st1.size()!=n)
        st1+='0';
    if(st.size()!=n)
    {
        st[st.size()-1]--;
        for(int i=n-st.size(); i>=2; i--)
            st+='0';
        st+='1';
    }
    reverse(st.begin(),st.end());
    cout<<st<<' '<<st1;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值