Codeforces 1219C 【字符串构造】

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;
		}
	}
}

Source

Codeforces 1219C Periodic integer number

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值