Codeforces Round #609 (Div. 2) C.Long Beautiful Integer
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个数即可,找到k个数中最后一个不为9的数字的位置,把它加一,后面的9全部置0即可:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
string s1,s2="";
int n,k;
cin>>n>>k>>s1;
for(int i=0;i<n;i++){
if(i<k) s2+=s1[i];
else s2+=s2[i-k];
}
if(s2>=s1) cout<<n<<endl<<s2<<endl;
else{
int u=0;
for(int i=k-1;i>=0;i--){
if(s1[i]!='9') {s1[i]=s1[i]+1;u=i;break;}
}
for(int i=u+1;i<k;i++){
s1[i]='0';
}
string ans="";
for(int i=0;i<n;i++){
if(i<k) ans+=s1[i];
else ans+=ans[i-k];
}
cout<<n<<endl<<ans<<endl;
}
}