题目链接*:cf-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≥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 x
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.
Examples
Input
3 2
353
Output
353
Input
4 2
1234
Output
4
1313
题意:
输入n,k;再输入数字t;
好数的定义:一个数字由s[0],s[1]…s[n]组成,如果s[i]==s[i+k] 即为好数。让你输出大于t,并且最小的好数。
思路:
截取 t 的前 k 位数字,然后进行复制(s[i]==s[i+k]&&i<n),如果s小于t,对前k位数字进行字符串的模拟加1,再复制比较。
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,k;
string s;
scanf("%d%d",&n,&k);
cin>>s;
string now=s.substr(0,k);
string ans;
while(1){
for(int i=0;i<n/k;i++){
ans=ans+now;
}
int len=n-k*(n/k);
ans=ans+now.substr(0,len);
if(ans>=s){
cout<<ans.length()<<endl;
cout<<ans<<endl;
break;
}else{
ans.clear();
int tot;
bool mark=false;
for(int i=k-1;i>=0;i--){
if(now[i]=='9'){
now[i]='0';
int j=i-1;
while(1){
if(now[j]!='9'){
now[j]=now[j]+1;
mark=true;
break;
}else{
now[j]='0';
j--;
}
}
if(mark)break;
}else{
now[i]=now[i]+1;
// printf("--- %c \n",s[i]);
break;
}
}
// cout<<"@@@@ "+now<<endl;
}
}
}