C. Periodic integer number
Alice became interested in periods of integer numbers. We say positive X integer number is periodic with length L if there exists positive integer number P with L digits such that X can be written as PPPP…P. For example:
X=123123123 is periodic number with length L=3 and L=9
X=42424242 is periodic number with length L=2,L=4 and L=8
X=12345 is periodic number with length L=5
For given positive period length L and positive integer number A, Alice wants to find smallest integer number X strictly greater than A that is periodic with length L.
Input
First line contains one positive integer number L (1≤L≤105) representing length of the period. Second line contains one positive integer number A (1≤A≤10100000).
Output
One positive integer number representing smallest positive number that is periodic with length L and is greater than A.
Examples
input
3
123456
output
124124
input
3
12345
output
100100
Note
In first example 124124 is the smallest number greater than 123456 that can be written with period L = 3 (P = 124).
In the second example 100100 is the smallest number greater than 12345 with period L = 3 (P=100)
Solution
给出一个数,求出严格大于该数并且循环节为n的数是多少。首先需要判断n是否能被给出数的长度整除,如果不能整除,则是1配上(n-1)个0,然后循环len/n+1次。能被整除则需要考虑多种特殊情况,如果n==len,那么实际上就是给出的数+1,但是需要考虑是否进位。然后后就是如果前面n位都是相同时,后面能否遵循严格大于,如不能则+1,考虑进位。只要能进到最高位或者比原先多一位,则是最高位配上(n-1)个0,循环n次,如果最高位为1则多循环一次。eg:3 999999999
疯狂特判,玄学过题。
Code
#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+5;
typedef long long ll;
int l,res[maxn];
string str;
int main() {
cin >> l >> str;
if(str.length() % l) {
int cnt = str.length() / l;
cnt++;
string ans = "1";
ans += string(l-1,'0');
for(int i=0; i<cnt; i++) {
cout << ans;
}
cout << endl;
} else {
int cnt = str.length() / l;
for(int i=0; i<l; i++) {
res[l-1-i] = str[i] - '0';
}
bool flag = false;
if(l == str.length()) {
res[0] == str[l-1] - '0';
res[0]++;
for(int i=0; i<l; i++) {
if(res[i] == 10) {
res[i] = 0;
res[i+1]++;
if(i == l-1) {
flag = true;
}
} else {
break;
}
}
} else {
for(int i=l; i<str.length(); i++) {
if((str[i] - '0' > res[l-i%l-1]) || i == str.length()-1 && str[i] - '0' == res[l-i%l-1]) {
res[0]++;
for(int i=0; i<l; i++) {
if(res[i] == 10) {
res[i] = 0;
res[i+1]++;
if(i == l-1) {
flag = true;
}
} else {
break;
}
}
break;
}
if(str[i] - '0' < res[l-i%l-1]) {
break;
}
}
}
if(flag) {
if(res[l] == 1) {
cnt++;
}
for(int i=0; i<cnt; i++) {
cout << res[l];
for(int j=1; j<l; j++) {
cout << "0";
}
}
cout << endl;
} else {
for(int i=0; i<cnt; i++) {
for(int j=l-1; j>=0; j--) {
cout << res[j];
}
}
cout << endl;
}
}
}