Long Beautiful Integer CodeForces - 1268A(贪心构造)

You are given an integer x of n digits a1,a2,…,an, which make up its decimal notation in order from left to right.

Also, you are given a positive integer k<n.

Let’s call integer b1,b2,…,bm beautiful if bi=bi+k for each i, such that 1≤i≤m−k.

You need to find the smallest beautiful integer y, such that y≥x.

Input
The first line of input contains two integers n,k (2≤n≤200000,1≤k<n): the number of digits in x and k.

The next line of input contains n digits a1,a2,…,an (a1≠0, 0≤ai≤9): digits of x.

Output
In the first line print one integer m: the number of digits in y.

In the next line print m digits b1,b2,…,bm (b1≠0, 0≤bi≤9): digits of y.

Examples
Input
3 2
353
Output
3
353
Input
4 2
1234
Output
4
1313
思路:这个题目其实只要前k位确定了,那么这个数字就确定了。因为任意长度的全是9组成的数字一定符合题意,这样m一定等于n。那么我们就取给定数字的前k位,剩下的根据这k位去构造。如果大于等于给定的就直接输出,否则就将前k位的数字+1,然后再根据这k位数字构造。
代码如下:

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

string s;
int n,k;

int main()
{
	char a='a';
	scanf("%d%d",&n,&k);
	cin>>s;
	string t;
	for(int i=0;i<k;i++) t+=s[i];
	for(int i=k;i<n;i++) t+=t[i-k];
	if(t>=s) cout<<n<<endl<<t<<endl;
	else
	{
		for(int i=k-1;i>=0;i--)
		{
			if(t[i]!='9')
			{
				t[i]+=1;
				break;
			}
			else t[i]='0';
		}
		for(int i=k;i<n;i++) t[i]=t[i-k];
		cout<<n<<endl<<t<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值