首页 > 其他 > 详细

codeforces-C. Long Beautiful Integer(字符串处理)

时间:2019-12-27 00:45:50      阅读:137      评论:0      收藏:0      [点我收藏+]
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

  题意:就是给你一个长度为n的大小为十进制数x,且满足Xi=Xi+k(1≤i≤m−k.),要你找一个大小等于y的数,其中y>=x,也要满足Yi=Yi+k(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;
}

  

codeforces-C. Long Beautiful Integer(字符串处理)

原文:https://www.cnblogs.com/xuanmaiboy/p/12105242.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!