Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class Main {
- final static int forNine = 45;
- final static int forNineNine = 900;
- final static int forNineNineNine = 13500;
- public static void main(String[] args) throws IOException {
- File a = new File("book.in");
- Scanner sc = new Scanner(a);
- FileWriter writer = new FileWriter("book.out");
- int n = sc.nextInt(), k = sc.nextInt();
- int sum = analyzeNumber(n);
- for(int i = 0; i < k; i++){
- sum -= getSum(n - sc.nextInt() + 1);
- }
- writer.append(sum + "");
- writer.flush();
- writer.close();
- }
- public static int getSum(int x){
- if(x < 10)
- return x;
- else if(x < 100)
- return x / 10 + (x - x / 10 * 10);
- else if (x < 1000)
- return x / 100 + (x / 10 - x / 100 * 10) + (x - x / 100 * 100 - (x / 10 - x / 100 * 10) * 10);
- else {
- char[] xs = Integer.toString(x).toCharArray();
- int sum = 0;
- for(char j: xs)
- sum += Integer.parseInt(j + "");
- return sum;
- }
- }
- public static int getSumFor(int x){
- if(x == 0)
- return 0;
- int sum = 0;
- for(int i = 1; i <= x; i++)
- sum += i;
- return sum;
- }
- public static int for100(int x){
- return (x / 10) * forNine + getSumFor(x / 10 - 1) * 10 + (x / 10) * (x % 10 + 1) + getSumFor(x % 10);
- }
- public static int for1000(int x){
- return (x / 100) * forNineNine + for100(x - x / 100 * 100) + getSumFor(x / 100 - 1) * 100 + (x / 100) * (x % 100 + 1);
- }
- public static int for10000(int x){
- return (x / 1000) * forNineNineNine + for1000(x - x / 1000 * 1000) + getSumFor(x / 1000 - 1) * 1000 + (x / 1000) * (x % 1000 + 1);
- }
- public static int analyzeNumber(int x){
- if(x < 10)
- return getSumFor(x);
- if(x < 100)
- return for100(x);
- if(x < 1000)
- return for1000(x);
- else return for10000(x);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement