https://codeforces.com/contest/1269/problem/C
You are given an integer x of n digits a1,a2,…,an, 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 b1,b2,…,bm beautiful if bi=bi+k for each i, such that 1≤i≤m?k.
You need to find the smallest beautiful integer y, such that y≥x.
记录前k个,模拟比大小,要修改值的时候从后往前选第一个不是9的字符修改,同时把9修改为0
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;
int n, k;
char num[MAXN], ans[MAXN];
bool Check()
{
for (int i = k;i < n;i++)
{
if (num[i] < ans[i%k])
return true;
if (num[i] > ans[i%k])
return false;
}
return true;
}
int main()
{
scanf("%d%d", &n, &k);
scanf("%s", num);
for (int i = 0;i < k;i++)
ans[i] = num[i];
ans[k] = 0;
if (Check())
{
printf("%d\n", n);
for (int i = 0;i < n;i++)
printf("%c", ans[i%k]);
printf("\n");
return 0;
}
for (int i = k-1;i >= 0;i--)
{
if (ans[i] != ‘9‘)
{
ans[i]++;
break;
}
else
ans[i] = ‘0‘;
}
printf("%d\n", n);
for (int i = 0;i < n;i++)
printf("%c", ans[i%k]);
printf("\n");
return 0;
}
Codeforces Round #609 (Div. 2) C. Long Beautiful Integer
原文:https://www.cnblogs.com/YDDDD/p/12082608.html