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)/~