Codeforces Round #609 (Div. 2) C. Long Beautiful Integer

Topic Link
meaning of the title is a given length n integers x, then you need to find not less than n and i-th position this number is equal to the smallest number i + k.
Obviously, by the k bits before the current number x, as the loop section, to see whether the number is greater than the number x is formed, is less than, the cycle simply adding section 1 can be formed to ensure that the number is greater than x, the output is less than on the line, is to be noted here is the time to end 9, a carry must, of course, does not recycle all sections 9 plus a case, since the number of all 9 formed will not less than x.
java large numbers made easy, direct plus a (manual nugget)
the AC Code

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
 
public class Main {
	static StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(System.in));
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static PrintWriter pr = new PrintWriter(new BufferedOutputStream(System.out));
	static Scanner sc = new Scanner(System.in);
 
	public static void main(String[] args) {
//		long tic = System.currentTimeMillis();
		int n = sc.nextInt();
		int k = sc.nextInt();
		String s = sc.next();
		String s2 = s.substring(0,k);
		boolean flag = true;
		for(int i = k;i<n;i++) {
			if(s.charAt(i)<s2.charAt((i)%k)) {
				break;
			}else if(s.charAt(i)>s2.charAt((i)%k)) {
				flag = false;
				break;
			}
		}
		StringBuffer sb = new StringBuffer();
		if(flag) {
			for(int i = 0;i<n;i++) {
				sb.append(s2.charAt(i%k));
			}
		}else{
			String s3 = new BigInteger(s2).add(new BigInteger("1")).toString();
			for(int i = 0;i<n;i++) {
				sb.append(s3.charAt(i%k));
			}
		}
		System.out.println(sb.length());
		System.out.println(sb);
		
//		long toc = System.currentTimeMillis();
//		System.out.println("Elapsed time: " + (toc - tic) + " ms");
	}
	private static long A(long n, long k, long mod) {
		long jcn = n;
		for (int i = 1; i < k; i++) {
			jcn *= (n - i);
			jcn %= mod;
		}
		return jcn;
	}
 
	private static long C(long n, long k, long mod) {
		long jcn = n;
		long jck = 1;
		for (int i = 1; i < k; i++) {
			jcn *= (n - i);
			jcn %= mod;
			jck *= i;
			jck %= mod;
		}
		jck *= k;
		jck %= mod;
		return jcn * pow(jck, mod - 2, mod) % mod;
	}
 
	static long pow(long a, long b, long mod) {
		long result = 1;
		while (b > 0) {
			if (b % 2 == 1) {
				result = (result * a) % mod;
			}
			a = (a * a) % mod;
			b /= 2;
		}
		return result;
	}
 
	static int nextInt() {
		try {
			st.nextToken();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return (int) st.nval;
	}
 
	static double nextDouble() {
		try {
			st.nextToken();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return st.nval;
	}
 
	static String next() {
		try {
			st.nextToken();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return st.sval;
	}
 
	static long nextLong() {
		try {
			st.nextToken();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return (long) st.nval;
	}
}
Published 50 original articles · won praise 32 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_40827780/article/details/103653997