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;
}