codeforces-C. Long Beautiful Integer (string processing)

time limit per test 3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 1imk1≤i≤m−k.

You need to find the smallest beautiful integer yy, such that yxy≥x.

Input

The first line of input contains two integers n,,k (2n200000,1k<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 (a10a1≠0, 0ai90≤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 (b10b1≠0, 0bi90≤bi≤9): digits of yy.

Examples

3 2
353

  output

3
353

  The meaning of problems: that is to give you a size of length n is a decimal number x, and satisfies Xi + K = Xi ( 1≤i≤m-K. , You find a size equal to the number y, where y> = x) , but also to meet the K + Yi = Yi ( 1≤i≤m-K.).

#include<bits/stdc++.h>
using namespace std;
char x[200005];
char y[200005];
int main(void)
{
	int a,k;
	scanf("%d %d",&a,&k);
	cin>>x;
	for(int i=0;i<a;i++){//直接模拟 
		y[i]=x[i%k];
	}
	if(strcmp(y,x)>=0)//y>=x
	{
		cout<<strlen(y)<<endl;
		cout<<y;
		return 0;
	}
	y[k-1]++;
	//如果有进位
	int i=k-1;
	while(y[i]>='9'+1)
	{
		y[i]='0';
		i--;
		y[i]++;//进位 
	}
	cout<<strlen(y)<<endl;
	for(int i=0;i<a;i++)
	y[i]=y[i%k];
	cout<<y; 
	return 0;
}

  

Guess you like

Origin www.cnblogs.com/xuanmaiboy/p/12105242.html