cf-D.Long Beautiful Integer-#609-div2

题目链接*:cf-Long Beautiful Integer

题面
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 1≤i≤m−k1≤i≤m−k.You need to find the smallest beautiful integer yy, such that y≥x

Input
The first line of input contains two integers n,kn,k (2≤n≤200000,1≤k<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 (a1≠0a1≠0, 0≤ai≤90≤ai≤9): digits of x

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 (b1≠0b1≠0, 0≤bi≤90≤bi≤9): digits of yy.

Examples
Input
3 2
353
Output
353
Input
4 2
1234
Output
4
1313

题意
输入n,k;再输入数字t;
好数的定义:一个数字由s[0],s[1]…s[n]组成,如果s[i]==s[i+k] 即为好数。让你输出大于t,并且最小的好数。

思路
截取 t 的前 k 位数字,然后进行复制(s[i]==s[i+k]&&i<n),如果s小于t,对前k位数字进行字符串的模拟加1,再复制比较。

#include<bits/stdc++.h>
using namespace std;
int main(){
 	int n,k;
 	string s;
 	scanf("%d%d",&n,&k);
 	cin>>s;
 	string now=s.substr(0,k);
 	string ans;
 	while(1){
  		for(int i=0;i<n/k;i++){
   			ans=ans+now;
  		}
  		int len=n-k*(n/k);
  		ans=ans+now.substr(0,len);
  		if(ans>=s){
   			cout<<ans.length()<<endl; 
   			cout<<ans<<endl;
   			break;
  		}else{
   			ans.clear();
   			int tot;
   			bool mark=false;
   			for(int i=k-1;i>=0;i--){
    				if(now[i]=='9'){
     				now[i]='0';
     				int j=i-1;
     				while(1){
      					if(now[j]!='9'){
       						now[j]=now[j]+1;
       						mark=true;
       						break;
      					}else{
       						now[j]='0';
       						j--;
      					}
     				}
     				if(mark)break;
    				}else{
     				now[i]=now[i]+1;
    //  printf("---  %c   \n",s[i]);
     				break;
    				}
   			}
  // cout<<"@@@@        "+now<<endl;
  		}
 	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值