A. Long Beautiful Integer

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

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

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

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

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

The next line of input contains nn digits a1,a2,…,ana1,a2,…,an (a1≠0a1≠0, 0≤ai≤90≤ai≤9): digits of xx.Output

In the first line print one integer mm: the number of digits in yy.

In the next line print mm digits b1,b2,…,bmb1,b2,…,bm (b1≠0b1≠0, 0≤bi≤90≤bi≤9): digits of yy.ExamplesinputCopy

3 2
353

outputCopy

3
353

inputCopy

4 2
1234

outputCopy

4
1313

思路:贪心循环构造

找前面的要求的k个然后不断循环放置,如果这样出现了比原串大的时候,就把循环小串从后往前进位,在0-8的加一位,9的进位。更新完后循环串再构造整串

启示:出现了可以类似平移的状态可以考虑循环构造

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=3e6+1000;
typedef long long LL;
string a,b,c;
LL n,k;
void solve()
{
	LL t=k-1;
	while(1)
	{
		if(b[t]>='0'&&b[t]<='8')
		{
			b[t]++;return;
		}
		else
		{
			b[t]='0';
			t--;
		}
	}
}
int main(void)
{
  
  cin>>n>>k;
  cin>>a;
  LL l=n%k;
  cout<<n<<endl;
  for(LL i=0;i<k;i++) b+=a[i];
  for(LL i=0;i<n/k;i++) c+=b;
  for(LL i=0;i<l;i++) c+=b[i];
  if(c>=a) {
  	cout<<c<<endl;
  }
  else
  {
  	 solve();c.clear();
  	 for(LL i=0;i<n/k;i++) c+=b;
  	 for(LL i=0;i<l;i++) c+=b[i];
  	 cout<<c<<endl;
  }
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值