2020NYIST个人积分赛第七场-G

题面

原题:CodeForces 1268A

Problem Description
You are given an integer x of n digits a_1_,a_2_,…,a_n_, 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 b_1_,b_2_,…,b_m_ beautiful if b_i_=b_i+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 a_1_,a_2_,…,a_n_ (a_1_≠0, 0≤a_i_≤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 b_1_,b_2_,…,b_m_ (b1≠0, 0≤bi≤9): digits of y.

Sample Input
3 2
353

Sample Output
3
353

简单题意

给你一串数字,对于所有的第i位数字(i<=n-k),第i位和第i+k位数字一样,成为为beautiful数字,给出一个数,求大于等于这个beautiful数的最小值。

思路

因为要求第i位与第i+k位数字相等,所以数的值实际上是取决于前k位的。所以我们要保留前K位然后生成一个新数字b。如果b大于等于原数字a就输出。如果小于a就将b的前k位取出,然后+1,就可以构造出最小的大于等于原数字a的数字b。(如果n个数字全是9 那一定是beautiful的)

代码

#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iterator>
using namespace std;
char a[2020202], b[2020202];
int main()
{
	int n, k;
	cin >> n >> k;
	scanf("%s", a);
	for (int i = 0; i < k; i++)
	{
		for (int j = i; j < n; j += k)
			b[j] = a[i]; //构建新数字b
	}
	if (strcmp(b, a) >= 0) //如果两个字符串相等 即b=a 返回0  如果b>a 返回大于0的值
	{
		cout << n << endl;
		printf("%s\n", b);
	}
	else
	{
		for (int i = k-1; i >= 0; i--)
		{
			if (b[i] == '9')
				b[i] = '0'; //如果b[i]是9,+1就会变成10,所以使当前位变为0,b[i-1]进位+1
			else
			{
				b[i]++; //当前一位加1就大于原数字a,跳出即可
				break;
			}
		}
		for (int i = 0; i < k; i++)
		{
			for (int j = i; j < n; j+=k)
				b[j] = b[i]; //将新数字b构建完整
		}
		cout << n << endl;
		printf("%s\n", b);
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值