Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5.  
  6.  
  7. public class Main  {
  8.     final static int forNine = 45;
  9.     final static int forNineNine = 900;
  10.     final static int forNineNineNine = 13500;
  11.  
  12.     public static void main(String[] args) throws IOException {
  13.  
  14.         File a = new File("book.in");
  15.         Scanner sc = new Scanner(a);
  16.         FileWriter writer = new FileWriter("book.out");
  17.  
  18.         int n = sc.nextInt(), k = sc.nextInt();
  19.  
  20.         int sum = analyzeNumber(n);
  21.  
  22.         for(int i = 0; i < k; i++){
  23.             sum -= getSum(n - sc.nextInt() + 1);
  24.         }
  25.  
  26.         writer.append(sum + "");
  27.         writer.flush();
  28.         writer.close();
  29.     }
  30.  
  31.     public static int getSum(int x){
  32.         if(x < 10)
  33.             return x;
  34.         else if(x < 100)
  35.             return  x / 10 + (x - x / 10 * 10);
  36.         else if (x < 1000)
  37.             return x / 100 + (x / 10 - x / 100 * 10) + (x - x / 100 * 100 - (x / 10 - x / 100 * 10) * 10);
  38.         else {
  39.             char[] xs = Integer.toString(x).toCharArray();
  40.             int sum = 0;
  41.             for(char j: xs)
  42.                 sum += Integer.parseInt(j + "");
  43.  
  44.             return sum;
  45.         }
  46.     }
  47.  
  48.     public static int getSumFor(int x){
  49.         if(x == 0)
  50.             return 0;
  51.  
  52.         int sum = 0;
  53.         for(int i = 1; i <= x; i++)
  54.             sum += i;
  55.  
  56.         return sum;
  57.     }
  58.  
  59.     public static int for100(int x){
  60.         return (x / 10) * forNine + getSumFor(x / 10 - 1) * 10 + (x / 10) * (x % 10 + 1) + getSumFor(x % 10);
  61.     }
  62.  
  63.     public static int for1000(int x){
  64.         return (x / 100) * forNineNine + for100(x - x / 100 * 100) + getSumFor(x / 100 - 1) * 100 + (x / 100) * (x % 100 + 1);
  65.     }
  66.  
  67.     public static int for10000(int x){
  68.         return (x / 1000) * forNineNineNine + for1000(x - x / 1000 * 1000) + getSumFor(x / 1000 - 1) * 1000 + (x / 1000) * (x % 1000 + 1);
  69.     }
  70.  
  71.     public static int analyzeNumber(int x){
  72.         if(x < 10)
  73.             return getSumFor(x);
  74.         if(x < 100)
  75.             return for100(x);
  76.         if(x < 1000)
  77.             return for1000(x);
  78.         else return for10000(x);
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement