src
stringlengths
95
64.6k
complexity
stringclasses
7 values
problem
stringlengths
6
50
from
stringclasses
1 value
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author kessido */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); BTheHat solver = new BTheHat(); solver.solve(1, in, out); out.close(); } static class BTheHat { PrintWriter out; InputReader in; int n; public void solve(int testNumber, InputReader in, PrintWriter out) { this.out = out; this.in = in; n = in.NextInt(); int desiredPair = -1; int result = query(1); if (result != 0) { int l = 2, r = 1 + n / 2; while (l < r) { int m = (l + r) / 2; int mRes = query(m); if (mRes == 0) { desiredPair = m; break; } else if (mRes == result) { l = m + 1; } else { r = m; } } } else { desiredPair = 1; } out.println("! " + desiredPair); } private int query(int i) { int iV = queryValue(i); int iN2V = queryValue(i + n / 2); if (iV < iN2V) { return -1; } else if (iV > iN2V) { return 1; } return 0; } private int queryValue(int i) { out.println("? " + i); out.flush(); return in.NextInt(); } } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,"); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int NextInt() { return Integer.parseInt(next()); } } }
logn
1019_B. The hat
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { FastReader in; PrintWriter out; int n; public void solve(int testNumber, FastReader in, PrintWriter out) { this.in = in; this.out = out; n = in.nextInt(); if (n % 4 != 0) { out.println("! -1"); return; } int low = 0; int high = n >> 1; int fSign = Integer.signum(BValue(low)); if (fSign == 0) { out.println("! " + (low + 1)); return; } while (high - low > 1) { int mid = (high + low) >> 1; int mSign = Integer.signum(BValue(mid)); if (mSign == 0) { out.println("! " + (mid + 1)); return; } if (mSign == -fSign) { high = mid; } else { low = mid; } } out.println("! -1"); } public int BValue(int index) { out.println("? " + (index + 1)); out.flush(); int f = in.nextInt(); out.println("? " + (index + 1 + (n >> 1))); out.flush(); int s = in.nextInt(); return f - s; } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
logn
1019_B. The hat
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class B { static PrintWriter out = new PrintWriter(System.out); static FS in; static int N; static final boolean debug = false; static int inp[] = new int[] {1,2,3,2,1,0}; public static void main(String[] args) { in = new FS(); if(!debug) N = in.nextInt(); else N = inp.length; int x = solve(0, N/2-1, N/2, N-1); out.println("! "+(x+1)); out.flush(); out.close(); } static int solve(int l1, int r1, int l2, int r2) { int sz = r1-l1+1; if(sz <= 0) return -2; int a1 = query(l1); int a2 = query(l2); if(a1 == a2) return l1; if(sz == 1) return -2; int b1 = query(l1+sz/2); int b2 = query(l2+sz/2); if(b1 == b2) return l1 + sz/2; if(sz == 2) return -2; int d1 = a2-a1; int d2 = b2-b1; if((d1 < 0 && d2 > 0) || (d1 > 0 && d2 < 0)) { return solve(l1+1, l1 + sz/2 - 1, l2+1, l2 + sz/2 - 1); } else { return solve(l1 + sz/2 + 1, r1, l2 + sz/2 + 1, r2); } } static int query(int a) { out.println("? "+(a+1)); out.flush(); if(debug) return inp[a]; else return in.nextInt(); } static class FS{ BufferedReader br; StringTokenizer st; public FS() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while(st == null || !st.hasMoreElements()) { try {st = new StringTokenizer(br.readLine());} catch(Exception e) { throw null;} } return st.nextToken(); } int nextInt() { return Integer.parseInt(next());} double nextDouble() { return Double.parseDouble(next());} long nextLong() { return Long.parseLong(next());} int[] NIA(int n) { int r[] = new int[n]; for(int i = 0; i < n; i++) r[i] = nextInt(); return r; } long[] NLA(int n) { long r[] = new long[n]; for(int i = 0; i < n; i++) r[i] = nextLong(); return r; } char[][] grid(int r, int c){ char res[][] = new char[r][c]; for(int i = 0; i < r; i++) { char l[] = next().toCharArray(); for(int j = 0; j < c; j++) { res[i][j] = l[j]; } } return res; } } }
logn
1019_B. The hat
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class B { public static void main(String[] args) throws IOException { MyScanner sc = new MyScanner(); PrintWriter out = new PrintWriter(System.out); int N = sc.nextInt(); if (N / 2 % 2 == 1) { output(-1, out); } else { int half = N / 2; int l = 1, r = half; int first = query(half, out, sc); int next = query(2 * half, out, sc); if (first == next) { output(half, out); return; } boolean less = first < next; while (l + 1 < r) { int med = (l + r) / 2; first = query(med, out, sc); next = query(med + half, out, sc); if (first == next) { output(med, out); return; } else if (first < next == less) { r = med; } else { l = med + 1; } } output(l, out); } } static int query(int pos, PrintWriter out, MyScanner sc) { out.println("? " + pos); out.flush(); return sc.nextInt(); } static void output(int pos, PrintWriter out) { out.println("! " + pos); out.flush(); } static class MyScanner { private BufferedReader br; private StringTokenizer tokenizer; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
logn
1019_B. The hat
CODEFORCES
import java.io.*; import java.util.*; public class B { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { int n = 0; int query(int p, InputReader in) { p %= n; if (p <= 0) p += n; System.out.println("? " + p); System.out.flush(); int x = in.nextInt(); return x; } public void solve(int testNumber, InputReader in, PrintWriter out) { n = in.nextInt(); if (n % 4 != 0) { out.println("! -1"); return; } int p = query(0, in); int q = query(n / 2, in); int l = 0; int r = n / 2; if (p == q) { out.println("! " + (n / 2)); return; } while (l + 1 < r) { int mid = (l + r) / 2; int u = query(mid, in); int v = query(mid + n / 2, in); if (u == v) { out.println("! " + (mid + n / 2)); return; } if ((p < q) == (u < v)) { l = mid; } else { r = mid; } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
logn
1019_B. The hat
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { int N; public void solve(int testNumber, InputReader in, PrintWriter out) { N = in.nextInt(); int low = 1; int lowVal = getval(1, out, in); int high = N / 2 + 1; int highVal = -lowVal; if (Math.abs(lowVal) % 2 == 1) { out.println("! -1"); out.flush(); } else { while (low < high) { int mid = (low + high) / 2; int a = getval(mid, out, in); if (Integer.signum(a) == 0) { out.println("! " + mid); out.flush(); return; } else { if (Integer.signum(a) == Integer.signum(lowVal)) { low = mid + 1; } else { high = mid; } } } out.println("! " + low); out.flush(); } } int getval(int i, PrintWriter out, InputReader in) { out.println("? " + i); out.flush(); int a = in.nextInt(); out.println("? " + (i + N / 2)); out.flush(); int b = in.nextInt(); return a - b; } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }
logn
1019_B. The hat
CODEFORCES
import java.io.*; import java.util.*; public class Main { final static int MAXN = 100005; static int n; static Scanner cin; static int[] a; static boolean[] used; public static int Query(int x) { System.out.print("? "); System.out.println(x); System.out.flush(); int a = cin.nextInt(); return a; } public static int Q(int x) { if(used[x]) return a[x]; used[x] = true; a[x] = Query(x) - Query(x + n / 2); if(a[x] == 0) { System.out.print("! "); System.out.println(x); System.out.flush(); cin.close(); System.exit(0); } return a[x]; } public static void main(String[] args) { cin = new Scanner(System.in); n = cin.nextInt(); a = new int[MAXN]; used = new boolean[MAXN]; if(n % 4 != 0) { System.out.println("! -1\n"); System.out.flush(); cin.close(); return; } int l = 1, r = n / 2, mid; while(l <= r) { mid = (l + r) / 2; int x = Q(mid); if(Q(l) * x < 0) { r = mid - 1; } else if(x * Q(r) < 0) { l = mid + 1; } } System.out.println("! -1\n"); System.out.flush(); cin.close(); } }
logn
1019_B. The hat
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class Main { static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} public int i(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;} public double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int[] arr(int n){int[] ret = new int[n];for (int i = 0; i < n; i++) {ret[i] = i();}return ret;} } // |----| /\ | | ----- | // | / / \ | | | | // |--/ /----\ |----| | | // | \ / \ | | | | // | \ / \ | | ----- ------- public static void main(String[] args)throws IOException { PrintWriter out= new PrintWriter(System.out); Reader sc=new Reader(); int n=sc.i(); System.out.println("? "+1); int a=sc.i(); System.out.println("? "+(1+n/2)); int b=sc.i(); if(a==b) { System.out.println("! "+1); System.exit(0); } int inv=0; if(a>b) inv=1; int low=2; int high=n/2; int q=0; while(low<=high) { if(q==60) break; int mid=(low+high)/2; System.out.println("? "+mid); a=sc.i(); System.out.println("? "+(mid+n/2)); b=sc.i(); if(a==b) { System.out.println("! "+mid); System.exit(0); } else if(a<b) { if(inv==0) low=mid+1; else high=mid-1; } else { if(inv==0) high=mid-1; else low=mid+1; } q++; } System.out.println("! -1"); out.flush(); } }
logn
1019_B. The hat
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { FastReader in; PrintWriter out; int n; public void solve(int testNumber, FastReader in, PrintWriter out) { this.in = in; this.out = out; n = in.nextInt(); if (n % 4 != 0) { out.println("! -1"); return; } int low = 0; int high = n >> 1; if (BValue(low) == 0) { out.println("! " + (low + 1)); return; } int fSign = Integer.signum(BValue(low)); while (high - low > 1) { int mid = (high + low) >> 1; int mSign = Integer.signum(BValue(mid)); if (mSign == 0) { out.println("! " + (mid + 1)); return; } if (mSign == -fSign) { high = mid; } else { low = mid; } } out.println("! -1"); } public int BValue(int index) { out.println("? " + (index + 1)); out.flush(); int f = in.nextInt(); out.println("? " + (index + 1 + (n >> 1))); out.flush(); int s = in.nextInt(); return f - s; } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
logn
1019_B. The hat
CODEFORCES
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; public class Div1_503B { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter printer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int N = Integer.parseInt(reader.readLine()); printer.println("? 1"); printer.flush(); int v1 = Integer.parseInt(reader.readLine()); printer.println("? " + (1 + N / 2)); printer.flush(); int v2 = Integer.parseInt(reader.readLine()); if ((v1 + v2) % 2 != 0) { printer.println("! -1"); printer.close(); return; } if (v1 == v2) { printer.println("! 1"); printer.close(); return; } boolean less = v1 < v2; int low = 1; int high = (1 + N / 2); while (low != high) { int mid = (low + high) >> 1; printer.println("? " + mid); printer.flush(); int r1 = Integer.parseInt(reader.readLine()); int q2 = (mid + N / 2); if (q2 > N) { q2 -= N; } printer.println("? " + q2); printer.flush(); int r2 = Integer.parseInt(reader.readLine()); if (r1 == r2) { printer.println("! " + mid); printer.close(); return; } if (r1 < r2 == less) { low = mid + 1; } else { high = mid - 1; } } printer.println("! " + low); printer.close(); return; } }
logn
1019_B. The hat
CODEFORCES
//package contests.CF1019; import java.io.*; import java.util.StringTokenizer; public class B { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); int half = n/2; pw.println("? 1"); pw.flush(); int a = sc.nextInt(); pw.println("? " + (1+half)); pw.flush(); int b = sc.nextInt(); if(a - b == 0){ pw.println("! 1"); } else if((a - b)%2 != 0) { pw.println("! -1"); }else{ boolean greater = a > b; int lo = 1; int hi = half; boolean ans = false; while(lo <= hi){ int mid = (lo + hi) /2; pw.println("? " + mid); pw.flush(); a = sc.nextInt(); pw.println("? " + (mid+half)); pw.flush(); b = sc.nextInt(); if(a == b){ pw.println("! " + mid); ans = true; break; } if(a > b != greater){ hi = mid-1; }else{ lo = mid+1; greater = a>b; } } if(!ans){ pw.println("! -1"); } } pw.flush(); pw.close(); } static int[][] packD(int n, int[] from, int[] to) { int[][] g = new int[n][]; int[] p = new int[n]; for (int f : from) if(f != -1) p[f]++; for (int i = 0; i < n; i++) g[i] = new int[p[i]]; for (int i = 0; i < from.length; i++) if(from[i] != -1) {g[from[i]][--p[from[i]]] = to[i];} return g; } static void shuffle(int[] a) { int n = a.length; for(int i = 0; i < n; i++) { int r = i + (int)(Math.random() * (n - i)); int tmp = a[i]; a[i] = a[r]; a[r] = tmp; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s)));} public String next() throws IOException {while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());return st.nextToken();} public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public boolean ready() throws IOException {return br.ready();} } }
logn
1019_B. The hat
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; import java.util.PriorityQueue; import java.util.Random; import java.util.StringTokenizer; public class Solution{ static FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static int n, d; public static void main(String[] args) throws IOException { // FastScanner fs = new FastScanner(); // PrintWriter out = new PrintWriter(System.out); int tt = 1; while(tt-->0) { n = fs.nextInt(); int l = 1, r = 1 + n/2; d = getB(l); if(d%2!=0) { out.println("! -1"); out.flush(); return; } if(d==0) { out.println("! 1"); out.flush(); return; } while(l<r) { int mid = (l+r)/2; if(check(mid)) { l = mid + 1; } else { r = mid; } int f = 1; } out.println("! "+l); } out.close(); } static boolean check(int i) { int k = getB(i); if(sig(d)==sig(k)) { return true; } return false; } static int getB(int i) { out.println("? "+i); out.flush(); int x1 = fs.nextInt(); int j = i + n/2; if(j>n) j -= n; out.println("? "+j); out.flush(); int x2 = fs.nextInt(); return x1 - x2; } static int sig(int x) { if(x>0) return 1; else if(x<0) return -1; return 0; } static final Random random=new Random(); static <T> void shuffle(T[] arr) { int n = arr.length; for(int i=0;i<n;i++ ) { int k = random.nextInt(n); T temp = arr[k]; arr[k] = arr[i]; arr[i] = temp; } } static void ruffleSort(int[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); int temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void ruffleSort(long[] a) { int n=a.length;//shuffle, then sort for (int i=0; i<n; i++) { int oi=random.nextInt(n); long temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static void reverse(int[] arr, int l, int r) { for(int i=l;i<l+(r-l)/2;i++){ int temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp; } } static void reverse(long[] arr, int l, int r) { for(int i=l;i<l+(r-l)/2;i++){ long temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp; } } static <T> void reverse(T[] arr, int l, int r) { for(int i=l;i<l+(r-l)/2;i++) { T temp = arr[i]; arr[i] = arr[r-i+l-1]; arr[r-i+l-1] = temp; } } static class FastScanner{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); public String next(){ while(!st.hasMoreElements()){ try{ st = new StringTokenizer(br.readLine()); } catch(IOException e){ e.printStackTrace(); } } return st.nextToken(); } public String nextLine() throws IOException { return br.readLine(); } public int nextInt(){ return Integer.parseInt(next()); } public int[] readArray(int n){ int[] a = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } public long nextLong() { return Long.parseLong(next()); } public char nextChar() { return next().toCharArray()[0]; } } }
logn
1019_B. The hat
CODEFORCES
import java.io.*; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; import java.util.stream.IntStream; public class B { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); Solver solver = new Solver(); solver.solve(in, out); out.close(); } static class Solver { int n; int n2; InputReader in; PrintWriter out; public void solve(InputReader in, PrintWriter out) { this.in = in; this.out = out; n = in.readInt(); n2 = n/2; int res = find(); out.print("! "); out.println(res); } public int find() { if (n%4 != 0) return -1; int c = compare(0); if (c == 0) return 1; int s = 1; int f = n2-1; if (c > 0) { s = n2+1; f = n-1; } while (s <= f) { int m = (s+f)/2; int v = compare(m); if (v == 0) return m+1; else if (v < 0) s = m+1; else f = m-1; } return -1; } public int compare(int z) { out.print("? "); out.println(z+1); out.flush(); int r1 = in.readInt(); out.print("? "); out.println((z+n2)%n+1); out.flush(); int r2 = in.readInt(); return r1-r2; } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { this.reader = new BufferedReader(new InputStreamReader(stream)); } public String read() { try { if (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException ex) { throw new RuntimeException(ex); } return tokenizer.nextToken(); } public int readInt() { return Integer.parseInt(read()); } public long readLong() { return Long.parseLong(read()); } public void readIntArrays(int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = readInt(); } } } } }
logn
1019_B. The hat
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in Actual solution is at the top * * @author ilyakor */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { int n; public void solve(int testNumber, InputReader in, OutputWriter out) { n = in.nextInt(); int base = calc(in, out, 0); if (base == 0) { out.printLine("! 1"); out.flush(); return; } if (Math.abs(base) % 2 != 0) { out.printLine("! -1"); out.flush(); return; } int down = 0, up = n / 2; int sdown = base < 0 ? -1 : 1; int sup = up < 0 ? -1 : 1; while (up - down > 1) { int t = (up + down) / 2; int cur = calc(in, out, t); if (cur == 0) { out.printLine("! " + (t + 1)); out.flush(); return; } int scur = cur < 0 ? -1 : 1; if (scur == sdown) { down = t; } else { up = t; } } throw new RuntimeException(); } private int calc(InputReader in, OutputWriter out, int val) { out.printLine("? " + (val + 1)); out.flush(); int res1 = in.nextInt(); out.printLine("? " + ((val + n / 2) % n + 1)); out.flush(); int res2 = in.nextInt(); return res1 - res2; } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void printLine(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } public void flush() { writer.flush(); } } static class InputReader { private InputStream stream; private byte[] buffer = new byte[10000]; private int cur; private int count; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isSpace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (count == -1) { throw new InputMismatchException(); } try { if (cur >= count) { cur = 0; count = stream.read(buffer); if (count <= 0) { return -1; } } } catch (IOException e) { throw new InputMismatchException(); } return buffer[cur++]; } public int readSkipSpace() { int c; do { c = read(); } while (isSpace(c)); return c; } public int nextInt() { int sgn = 1; int c = readSkipSpace(); if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res = res * 10 + c - '0'; c = read(); } while (!isSpace(c)); res *= sgn; return res; } } }
logn
1019_B. The hat
CODEFORCES
import java.io.*; import java.util.*; public class Main { private static final long MOD = 998244353; static int[] readArray(int size, InputReader in, boolean subOne) { int[] a = new int[size]; for (int i = 0; i < size; i++) { a[i] = in.nextInt() + (subOne ? -1 : 0); } return a; } static long[] readLongArray(int size, InputReader in) { long[] a = new long[size]; for (int i = 0; i < size; i++) { a[i] = in.nextLong(); } return a; } static void sortArray(int[] a) { Random random = new Random(); for (int i = 0; i < a.length; i++) { int randomPos = random.nextInt(a.length); int t = a[i]; a[i] = a[randomPos]; a[randomPos] = t; } Arrays.sort(a); } public static void main(String[] args) throws FileNotFoundException { // InputReader in = new InputReader(new FileInputStream("input.txt")); // PrintWriter out = new PrintWriter(new BufferedOutputStream(new FileOutputStream("milkvisits.out"))); // InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = in.nextInt(); if (n / 2 % 2 != 0) { out.println("! -1"); out.flush(); out.close(); return; } int[] a = new int[n]; Arrays.fill(a, Integer.MAX_VALUE); int l1 = 0; int r1 = l1 + n / 2; int l2 = r1; int r2 = n; int ans = -1; while (true) { getValue(in, out, a, l1); getValue(in, out, a, r1); getValue(in, out, a, l2); getValue(in, out, a, r2 % n); if (a[l1] == a[l2]) { ans = l1; break; } if (a[r1] == a[r2 % n]) { ans = r1; break; } int m1 = (l1 + r1) / 2; getValue(in, out, a, m1); int m2 = (l2 + r2) / 2; getValue(in, out, a, m2); if (a[m1] == a[m2]) { ans = m1; break; } if ((a[l1] <= a[m1] && a[l2] <= a[m2]) || (a[l1] >= a[m1] && a[l2] >= a[m2])) { if (a[l1] <= a[l2] && a[m1] >= a[m2]) { r1 = m1; r2 = m2; continue; } if (a[l1] >= a[l2] && a[m1] <= a[m2]) { r1 = m1; r2 = m2; continue; } } if (a[l1] <= a[m1] && a[l2] >= a[m2] && a[l1] <= a[l2] && a[m1] >= a[m2]){ r1 = m1; r2 = m2; continue; } if (a[l1] >= a[m1] && a[l2] <= a[m2] && a[l1] >= a[l2] && a[m1] <= a[m2]){ r1 = m1; r2 = m2; continue; } l1 = m1; l2 = m2; } out.println("! " + (ans + 1)); out.close(); } private static void getValue(InputReader in, PrintWriter out, int[] a, int l) { if (a[l] == Integer.MAX_VALUE) { out.println("? " + (l + 1)); out.flush(); a[l] = in.nextInt(); } } private static boolean check(long x, long s, long a, List<Long> divA) { int ind = binSearchRight(divA, x, 0, divA.size()); if (ind >= 0 && ind < divA.size()) { long y = a / divA.get(ind); return y <= s / x; } return false; } static int binSearchRight(List<Long> list, long key, int start, int end) { int l = start - 1; int r = end; while (l < r - 1) { int m = (l + r) / 2; if (list.get(m) <= key) { l = m; } else { r = m; } } return l; } private static void outputArray(int[] ans, PrintWriter out, boolean addOne) { StringBuilder str = new StringBuilder(); for (int i = 0; i < ans.length; i++) { long an = ans[i] + (addOne ? 1 : 0); str.append(an).append(' '); } out.println(str); } private static void outputArray(List<Integer> ans, PrintWriter out, boolean addOne) { StringBuilder str = new StringBuilder(); for (int j = 0; j < ans.size(); j++) { long i = ans.get(j); long an = i + (addOne ? 1 : 0); str.append(an); if (j < ans.size() - 1) { str.append(' '); } } out.println(str); // out.flush(); } private static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextString() { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public char nextChar() { return next().charAt(0); } public String nextWord() { return next(); } private List<Integer>[] readTree(int n) { return readGraph(n, n - 1); } private List<Integer>[] readGraph(int n, int m) { List<Integer>[] result = new ArrayList[n]; for (int i = 0; i < n; i++) { result[i] = new ArrayList<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; result[u].add(v); result[v].add(u); } return result; } private Map<Integer, Long>[] readWeightedGraph(int n, int m) { Map<Integer, Long>[] result = new HashMap[n]; for (int i = 0; i < n; i++) { result[i] = new HashMap<>(); } for (int i = 0; i < m; i++) { int u = nextInt() - 1; int v = nextInt() - 1; long w = nextLong(); result[u].put(v, Math.min(w, result[u].getOrDefault(v, Long.MAX_VALUE))); result[v].put(u, Math.min(w, result[v].getOrDefault(u, Long.MAX_VALUE))); } return result; } } }
logn
1019_B. The hat
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { FastReader in; PrintWriter out; int n; public void solve(int testNumber, FastReader in, PrintWriter out) { this.in = in; this.out = out; n = in.nextInt(); if (n % 4 != 0) { out.println("! -1"); return; } int low = 0; int high = n >> 1; if (BValue(low) == 0) { out.println("! " + (low + 1)); return; } boolean value = BValue(low) > 0; while (high - low > 1) { int mid = (high + low) >> 1; int BVal = BValue(mid); if (BVal == 0) { out.println("! " + (mid + 1)); return; } if (value) { if (BVal < 0) { high = mid; } else { low = mid; } } else { if (BVal > 0) { high = mid; } else { low = mid; } } } out.println("! -1"); } public int BValue(int index) { out.println("? " + (index + 1)); out.flush(); int f = in.nextInt(); out.println("? " + (index + 1 + (n >> 1))); out.flush(); int s = in.nextInt(); return f - s; } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
logn
1019_B. The hat
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Liavontsi Brechka */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DEhabIEsheOdnaOcherednayaZadachaNaXor solver = new DEhabIEsheOdnaOcherednayaZadachaNaXor(); solver.solve(1, in, out); out.close(); } static class DEhabIEsheOdnaOcherednayaZadachaNaXor { public void solve(int testNumber, InputReader in, PrintWriter out) { int c = 0; int d = 0; int prevSign = 0; int nextSign; boolean zeroOut = true; for (int i = 29; i >= 0; i--) { if (zeroOut) { print(c, d, out); prevSign = read(in); } print((1 << i) | c, (1 << i) | d, out); nextSign = read(in); if (prevSign == nextSign) { zeroOut = false; print((1 << i) | c, d, out); nextSign = read(in); if (nextSign < 0) { c = (1 << i) | c; d = (1 << i) | d; } } else { zeroOut = true; if (nextSign < 0) c = (1 << i) | c; else d = (1 << i) | d; } } out.printf("! %d %d", c, d); out.flush(); } private void print(int c, int d, PrintWriter out) { out.printf("? %d %d\n", c, d); out.flush(); } private int read(InputReader in) { return in.nextInt(); } } static class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(readLine()); } return tokenizer.nextToken(); } public String readLine() { String line; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } }
logn
1088_D. Ehab and another another xor problem
CODEFORCES
import java.util.*; public class mad{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int cura = 0,curb = 0; int ver; System.out.println("? 0 0"); System.out.flush(); ver = sc.nextInt(); for(int i=29;i>=0;i--){ System.out.println("? "+(cura+(1<<i))+" "+curb); System.out.flush(); int temp1 = sc.nextInt(); System.out.println("? "+cura+" "+(curb+(1<<i))); System.out.flush(); int temp2 = sc.nextInt(); if(temp1!=temp2){ if(temp2==1){ cura += (1<<i); curb += (1<<i); } } else{ if(ver==1) cura += (1<<i); if(ver==-1) curb += (1<<i); ver = temp1; } } System.out.println("! "+cura+" "+curb); } }
logn
1088_D. Ehab and another another xor problem
CODEFORCES
import java.util.*; import java.io.*; public class code{ public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int ok,ok2; int va,vb; va = 0; vb = 0; out.println("? "+va+" "+vb); out.flush(); ok = sc.nextInt(); for(int i=29;i>=0;i--){ if(ok==0){ va += (1<<i); out.println("? "+va+" "+vb); out.flush(); ok2 = sc.nextInt(); if(ok2==1){ va -= (1<<i); }else{ vb += (1<<i); } }else{ va += (1<<i); vb += (1<<i); out.println("? "+va+" "+vb); out.flush(); ok2 = sc.nextInt(); if(ok==ok2){ vb -= (1<<i); out.println("? "+va+" "+vb); out.flush(); ok2 = sc.nextInt(); if(ok2==1){ va -= (1<<i); }else{ vb += (1<<i); } }else{ if(ok==1){ vb -= (1<<i); out.println("? "+va+" "+vb); out.flush(); ok = sc.nextInt(); } else { va -= (1<<i); out.println("? "+va+" "+vb); out.flush(); ok = sc.nextInt(); } } } } out.println("! "+va+" "+vb); out.flush(); } }
logn
1088_D. Ehab and another another xor problem
CODEFORCES
import java.util.*; public class ehab4 { public static void main( String[] args ) { Scanner in = new Scanner( System.in ); int a = 0, b = 0; System.out.println( "? 0 0 " ); System.out.flush(); int c = in.nextInt(); for ( int i = 29; i >= 0; i-- ) { System.out.println( "? " + ( a + ( 1 << i ) ) + " " + b ); System.out.flush(); int q1 = in.nextInt(); System.out.println( "? " + a + " " + ( b + ( 1 << i ) ) ); System.out.flush(); int q2 = in.nextInt(); if ( q1 == q2 ) { if ( c == 1 ) a += ( 1 << i ); else if ( c == -1 ) b += ( 1 << i ); c = q1; } else if ( q1 == -1 ) { a += ( 1 << i ); b += ( 1 << i ); } else if ( q1 == -2 ) return; } System.out.println( "! " + a + " " + b ); System.out.flush(); } }
logn
1088_D. Ehab and another another xor problem
CODEFORCES
import java.util.*; public class ehab4 { public static void main( String[] args ) { Scanner in = new Scanner( System.in ); int a = 0, b = 0; System.out.println( "? 0 0 " ); System.out.flush(); int c = in.nextInt(); for ( int i = 29; i >= 0; i-- ) { System.out.println( "? " + ( a + ( 1 << i ) ) + " " + b ); System.out.flush(); int q1 = in.nextInt(); System.out.println( "? " + a + " " + ( b + ( 1 << i ) ) ); System.out.flush(); int q2 = in.nextInt(); if ( q1 == q2 ) { if ( c == 1 ) a += ( 1 << i ); else if ( c == -1 ) b += ( 1 << i ); c = q1; } else if ( q1 == -1 ) { a += ( 1 << i ); b += ( 1 << i ); } else if ( q1 == -2 ) return; } System.out.println( "! " + a + " " + b ); System.out.flush(); } }
logn
1088_D. Ehab and another another xor problem
CODEFORCES
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class EhabAndAnotherAnotherXorProblem implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int initial = ask(0, 0); int a = 0, b = 0; if (initial == 0) { for (int i = 0; i < 30; i++) { int response = ask(1 << i, 0); if (response == -1) { a |= (1 << i); } } b = a; } else { for (int i = 29; i >= 0; i--) { int response = ask(a | (1 << i), b | (1 << i)); if (response != initial) { if (response == 1) { b |= (1 << i); } else { a |= (1 << i); } initial = ask(a, b); } else { response = ask(a | (1 << i), b); if (response == -1) { a |= (1 << i); b |= (1 << i); } } } } answer(a, b); } private int ask(int c, int d) { out.printf("? %d %d\n", c, d); out.flush(); return in.ni(); } private void answer(int a, int b) { out.printf("! %d %d\n", a, b); out.flush(); } @Override public void close() throws IOException { in.close(); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (EhabAndAnotherAnotherXorProblem instance = new EhabAndAnotherAnotherXorProblem()) { instance.solve(); } } }
logn
1088_D. Ehab and another another xor problem
CODEFORCES
import java.util.Scanner; import java.io.*; import java.util.*; public class ReallyBigNumbers817c { static long sd(String s) { long c = 0; for (int i = 0; i < s.length(); i++) { c += s.charAt(i); } return c - s.length() * 0x30; } public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = in.nextLong(); long s = in.nextLong(); // number // level -- > (n + 8) / 9 * 9; --- > s long i = (s/10+1)*10 ; if (n<10||n-sd(n+"")<s) { System.out.println(0); return; } while(!(i-sd(i+"")>=s)){ i+=10; } System.out.println(n-i+1); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Scanner; import java.util.StringTokenizer; public class ReallyBigNumbers1 { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); PrintWriter out = new PrintWriter(System.out); long n = Long.parseLong(st.nextToken()); long s = Long.parseLong(st.nextToken()); int r = 0 ; long l = 0L ; long u = n ; if( (l-sumDigits(l)< s ) && (u-sumDigits(u)< s ) ) { out.println(0); out.flush(); out.close(); return ; } long specified = 0L ; while( true ) { long m = (l + u) / 2L ; if( ( m - sumDigits(m) ) >= s && ( (m-1) - sumDigits(m-1) ) < s ) { specified = m ; break ; } if( l > u ) break ; else { if( ( m - sumDigits(m) ) >= s ) u = m-1 ; else l = m+1 ; } } out.println( n-specified+1 ); out.flush(); out.close(); } public static int sumDigits(long n) { char [] a = (n+"").toCharArray(); int sum = 0 ; for(int k = 0 ; k < a.length ; k++) { sum += Integer.parseInt(a[k]+"") ; } return sum ; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; public class Edu23 { public static int sum(String s){ int tot=0; for(int i =0; i<s.length();i++){ tot+=(s.charAt(i)-'0'); } return tot; } public static BigInteger comb(int n, int k){ if(k==0) return new BigInteger("1"); else return comb(n,k-1).multiply(new BigInteger(n-k+1+"")).divide(new BigInteger(k+"")); } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long s = sc.nextLong(); long i; for(i =s ; i-sum(i+"")<s;i++) if(i%10==0)i+=9; System.out.println(Math.max(0, n-i+1)); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String f) throws FileNotFoundException { br = new BufferedReader(new FileReader(f)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public int[] nextIntArray1(int n) throws IOException { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } public int[] shuffle(int[] a, int n) { int[] b = new int[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Random r = new Random(); for (int i = 0; i < n; i++) { int j = i + r.nextInt(n - i); int t = b[i]; b[i] = b[j]; b[j] = t; } return b; } public int[] nextIntArraySorted(int n) throws IOException { int[] a = nextIntArray(n); a = shuffle(a, n); Arrays.sort(a); return a; } public long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public long[] nextLongArray1(int n) throws IOException { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } public long[] nextLongArraySorted(int n) throws IOException { long[] a = nextLongArray(n); Random r = new Random(); for (int i = 0; i < n; i++) { int j = i + r.nextInt(n - i); long t = a[i]; a[i] = a[j]; a[j] = t; } Arrays.sort(a); return a; } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; /** * * 作者:张宇翔 创建日期:2017年6月16日 上午9:00:48 描述:写字楼里写字间,写字间里程序员; 程序人员写程序,又拿程序换酒钱。 * 酒醒只在网上坐,酒醉还来网下眠; 酒醉酒醒日复日,网上网下年复年。 但愿老死电脑间,不愿鞠躬老板前; 奔驰宝马贵者趣,公交自行程序员。 * 别人笑我忒疯癫,我笑自己命太贱; 不见满街漂亮妹,哪个归得程序员? */ public class Main { private final static int Max = (int) (1e5 + 10); private static long n,s; public static void main(String[] args) { InitData(); GetAns(); } private static void InitData() { Scanner cin = new Scanner(System.in); n=cin.nextLong(); s=cin.nextLong(); }; private static void GetAns() { long i; long ans=0; for(i=s;i<=n;i++){ long k=i-sum(i); if(k>=s){ if(i%10==9){ break; } ans++; } } if(n>=s){ System.out.println(ans-i+n+1); }else{ System.out.println(0); } }; private static long sum(long ans){ long sum=0; while(ans>0){ sum+=(ans%10); ans/=10; } return sum; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class C { public static void main(String[] args) { new C(); } C() { Scanner in = new Scanner(System.in); long n = in.nextLong(), s = in.nextLong(); long lo = 1, hi = 1000000000000000000L; while(lo<hi){ //System.out.println(lo+" "+hi); //STUPID STUPID DUMB long mid = (lo+hi)/2; if(reallyBig(mid,s)) hi = mid; else lo = mid+1; } //System.out.println(lo+" "+hi); if(!reallyBig(lo,s)) System.out.println(0); else System.out.println(Math.max(n-lo+1,0)); //System.out.println(reallyBig(100000000000000009L,100000000000000000L)); in.close(); } boolean reallyBig(long n, long s){ int sum = 0; long temp = n; while(temp>0){ sum += temp%10; temp/=10; } return n-sum>=s; } } /* 12 1 25 20 10 9 1000000000000000000 1000000000000000000 1000000000000000000 100000000000000000 */
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); atskb solver = new atskb(); solver.solve(1, in, out); out.close(); } static class atskb { long s; public void solve(int testNumber, InputReader in, PrintWriter out) { long n = in.nextLong(); s = in.nextLong(); long ans = binarysearch(0, (long) Math.pow(10, 18) + 20 * 9); if (ans > n) { out.print(0); } else { out.print(n - ans + 1); } } public long binarysearch(long l, long r) { if (l == r) { if (check(l)) return l; return -1; } if (l - r == -1) { if (check(l)) return l; if (check(r)) { return r; } //return -1; } long mid = l + (r - l) / 2; if (check(mid)) return binarysearch(l, mid); return binarysearch(mid + 1, r); } public boolean check(long m) { String str = m + ""; long sum = 0; for (int i = 0; i < str.length(); i++) { sum += str.charAt(i) - '0'; } if (sum + s <= m) return true; return false; } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar; private int snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; /** * Road to 1600 raiting */ public class Main { static class Task { PrintWriter out; int[] num = new int[3]; public void solve(MyScanner in, PrintWriter out) { this.out = out; long n = in.nextLong(); long s = in.nextLong(); long l = 1; long r = n; while (r - l > 5) { long x = (l + r) / 2; long ans = ans(x); if (ans >= s) { r = x; } else { l = x; } } for (long i = l; i <= r; i++) { if (ans(i) >= s) { out.println((n - i + 1)); return; } } out.println(0); } long ans(long n) { long res = n; while (n > 9) { res -= n % 10; n /= 10; } res -= n; return res; } } public static void main(String[] args) { MyScanner in = new MyScanner(); PrintWriter out = new PrintWriter(System.out); Task solver = new Task(); solver.solve(in, out); out.close(); } public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class Probram3 { public static int get(long n) { int sum = 0; while(n != 0) { sum += n % 10; n = n / 10; } return sum; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long n = scanner.nextLong(); long s = scanner.nextLong(); long l = 1; long r = Long.MAX_VALUE; long index = 0; while(l <= r) { long mid = (l + r) / 2; if(mid - get(mid) >= s) { index = mid; r = mid - 1; }else{ l = mid + 1; } } System.out.println(Math.max(0, n-index+1)); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class C { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); long n = sc.nextLong(); Long S = sc.nextLong(); long l = 0; long h = n; long ans = n; while(l <= h) { long mid = (l + h) / 2; long t = mid; long sum = 0; while(t > 0) { sum += t % 10; t /= 10; } if(mid - sum < S) { ans = mid; l = mid + 1; }else h = mid - 1; } out.println(n - ans); out.flush(); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream System){br = new BufferedReader(new InputStreamReader(System));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String nextLine()throws IOException{return br.readLine();} public int nextInt() throws IOException {return Integer.parseInt(next());} public double nextDouble() throws IOException {return Double.parseDouble(next());} public char nextChar()throws IOException{return next().charAt(0);} public Long nextLong()throws IOException{return Long.parseLong(next());} public boolean ready() throws IOException{return br.ready();} public void waitForInput(){for(long i = 0; i < 3e9; i++);} } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.math.BigInteger; import java.util.*; public class C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long s = sc.nextLong(); BigInteger k = findFirst(BigInteger.valueOf(s)); if (BigInteger.valueOf(n).compareTo(k) >= 0) { System.out.println(n - k.longValue() + 1); } else { System.out.println("0"); } } public static BigInteger findFirst(BigInteger s) // first number where sum of digs >= s { BigInteger b = BigInteger.ZERO; while (cd(b).compareTo(b.subtract(s)) > 0) { BigInteger c = BigInteger.ONE; while (cd(b.add(c)).compareTo(b.add(c).subtract(s)) > 0) { c = c.multiply(BigInteger.TEN); } // possibly overshot c = c.divide(BigInteger.TEN); if (c.compareTo(BigInteger.TEN) < 0) c = BigInteger.TEN; // always add at least 10 b = b.add(c); } return b; } public static BigInteger cd(BigInteger n) { BigInteger t = BigInteger.ZERO; while (n.compareTo(BigInteger.ZERO) > 0) { t = t.add(n.mod(BigInteger.TEN)); n = n.divide(BigInteger.TEN); } return t; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class ReallyBigNumbers { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long s = sc.nextLong(); long m = s; while(m-digitAdd(m)<s && m<=n){ m++; } System.out.println(Math.max(n-m+1, 0)); } private static int digitAdd(long s){ int sum = 0; for(long i = 0,j=1L;i<(int)Math.log10(s)+1; i++,j*=10){ sum += (s/j)%10; } return sum; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class Test { private static long sum(long value) { long ans = 0; while (value > 0) { ans += value % 10; value /= 10; } return ans; } public static void main(String[] args) { Scanner scan = new Scanner(System.in); long n , s , ans = 0; n = scan.nextLong(); s = scan.nextLong(); long current = s; while (current <= n && current <= s + 20 * 9) { // current - sum(current) >= s long temp = sum(current); if (current - temp >= s) { ans ++; } current ++; } if (current <= n) { ans += (n - current + 1); } System.out.println(ans); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); long n = Long.parseLong(st.nextToken()); long s = Long.parseLong(st.nextToken()); long posible = binarySearch(n,s); long dig, answer; long i, cmed; for (i = posible; i >= 0; i--) { dig = 0; cmed = i; while (cmed > 0) { dig = dig+cmed%10; cmed/=10; } if (i-dig < s) { break; } } answer = n-i; System.out.println(answer); } private static long binarySearch(long n, long s){ long med=n, l = 0, r = n, cmed, dig; while(l<=r){ med = (l+r)/2; cmed = med; dig = 0; while (cmed > 0) { dig = dig+cmed%10; cmed/=10; } if (med-dig == s) { break; }else { if (med-dig > s) { r = med-1; }else { l = med+1; } } //System.out.println(med); } return med; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class ReallyBigNumbers817c { static long sd(String s) { long c = 0; for (int i = 0; i < s.length(); i++) { c += s.charAt(i); } return c - s.length() * 0x30; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); long n = Long.parseLong(st.nextToken()); long s = Long.parseLong(st.nextToken()); long i = (s / 10 + 1) * 10; if (n < 10 || n - sd(n + "") < s) { System.out.println(0); return; } while (!(i - sd(""+i) >= s)) { i += 10; } System.out.println(n - i + 1); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.math.BigDecimal; import java.util.Scanner; public class ReallyBigNumbers { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long n = scan.nextLong(); long s = scan.nextLong(); long ans = 0; long l = 0; long r = n; while (l <= r) { // Key is in a[lo..hi] or not present. long mid = l + (r - l) / 2; if(isReallyBig(mid, s)){ ans = mid; r = mid-1; } else l = mid+1; } if(ans == 0) System.out.println(ans); else System.out.println(n-ans+1); } static boolean isReallyBig(long m, long s){ String x = m+""; long sum = 0; for(int i = 0; i < x.length(); i++){ sum += x.charAt(i)-'0'; } if(m-sum >= s) return true; return false; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.math.*; import java.security.KeyStore.Entry; import java.util.*; public class Main { private InputStream is; private PrintWriter out; int time = 0, dp[][], DP[][], start[], parent[], end[], val[], black[], MOD = (int)(1e9+7), arr[], arr1[]; int MAX = 10000000, N, K, p; ArrayList<Integer>[] amp, amp1; boolean b[], b1[]; Pair prr[]; char ch[][]; HashSet<Integer> hs = new HashSet<>(); public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { //new Main().soln(); } catch (Exception e) { System.out.println(e); } } }, "1", 1 << 26).start(); new Main().soln(); } void solve() { long n = nl(), s = nl(); long low = 1, high = n+1; long ans = high; while(low<=high){ long mid = (high+low)/2; if((mid - getSum(mid))>=s){ high = mid-1; ans = mid; } else{ low = mid+1; } } System.out.println(Math.max(0, n-ans+1)); } int getSum(long s){ String str = Long.toString(s); int ans = 0; for(char ch : str.toCharArray()) ans += (ch-'0'); return ans; } int recur(int x){ //System.out.println(x); int ans = 0; b[x] = true; for(int i : amp[x]){ if(!b[i]){ b[i] = true; ans = Math.max(recur(i), ans); b[i] = false; } } return 1+ans; } int max = 0; int getParent(int x){ //System.out.println(x+" "+parent[x]); if(parent[x]!=x){ parent[x] = getParent(parent[x]); } return parent[x]; } int bfs(int x){ b[x] = true; Queue<Integer> q = new LinkedList<>(); q.add(x); while(!q.isEmpty()){ int y = q.poll(); for(int i : amp[y]){ if(!b[i]){ b[i] = true; val[i] = val[y]+1; max = Math.max(val[i], max); q.add(i); } } } return max; } class Pair implements Comparable<Pair>{ int u, v, r; Pair(int u, int v){ this.u = u; this.v = v; }public int hashCode() { return Objects.hash(); } public boolean equals(Object o) { Pair other = (Pair) o; return ((u == other.u && v == other.v));// || (v == other.u && u == other.v)); } public int compareTo(Pair other) { //return Integer.compare(other.r, r); return Long.compare(u, other.u) != 0 ? (Long.compare(u, other.u)) : (Long.compare(other.v,v)); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } int min(int x,int y){ if(x<y) return x; return y; } int max(int x,int y){ if(x>y) return x; return y; } void dfs(int x){ b[x] = true; for(int i : amp[x]){ if(!b[i]){ dfs(i); } } } void buildGraph(int m){ while(m-->0) { int x = ni()-1, y = ni()-1; amp[x].add(y); amp[y].add(x); } } long modInverse(long a, long mOD2){ return power(a, mOD2-2, mOD2); } long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y/2, m) % m; p = (p * p) % m; return (y%2 == 0)? p : (x * p) % m; } boolean isPrime(int x){ for(int i = 2;i*1L*i<=x;i++) if(x%i==0) return false; return true; } public long gcd(long a, long b){ if(b==0) return a; return gcd(b,a%b); } void failFn(String str, int arr[]){ int len = 0; arr[0] = 0; int i = 1; while(i<arr.length){ if(str.charAt(i)==str.charAt(len)){ arr[i++] = ++len; continue; } if(len == 0){ arr[i] = len; i++; continue; } if(str.charAt(i)!=str.charAt(len)){ len = arr[len-1]; } } } static class ST1{ int arr[], st[], size; ST1(int a[]){ arr = a.clone(); size = 10*arr.length; st = new int[size]; build(0,arr.length-1,1); } void build(int ss, int se, int si){ if(ss==se){ st[si] = arr[ss]; return; } int mid = (ss+se)/2; int val = 2*si; build(ss,mid,val); build(mid+1,se,val+1); st[si] = (st[val]+ st[val+1]); } int get(int ss, int se, int l, int r, int si){ if(l>se || r<ss || l>r) return Integer.MAX_VALUE; if(l<=ss && r>=se) return st[si]; int mid = (ss+se)/2; int val = 2*si; return (get(ss,mid,l,r,val)+ get(mid+1,se,l,r,val+1)); } } static class ST{ int arr[],lazy[],n; ST(int a){ n = a; arr = new int[10*n]; lazy = new int[10*n]; } void up(int l,int r,int val){ update(0,n-1,0,l,r,val); } void update(int l,int r,int c,int x,int y,int val){ if(lazy[c]!=0){ lazy[2*c+1]+=lazy[c]; lazy[2*c+2]+=lazy[c]; if(l==r) arr[c]+=lazy[c]; lazy[c] = 0; } if(l>r||x>y||l>y||x>r) return; if(x<=l&&y>=r){ lazy[c]+=val; return ; } int mid = l+r>>1; update(l,mid,2*c+1,x,y,val); update(mid+1,r,2*c+2,x,y,val); arr[c] = (arr[2*c+1]+ arr[2*c+2]); } int an(int ind){ return ans(0,n-1,0,ind); } int ans(int l,int r,int c,int ind){ if(lazy[c]!=0){ lazy[2*c+1]+=lazy[c]; lazy[2*c+2]+=lazy[c]; if(l==r) arr[c]+=lazy[c]; lazy[c] = 0; } if(l==r) return arr[c]; int mid = l+r>>1; if(mid>=ind) return ans(l,mid,2*c+1,ind); return ans(mid+1,r,2*c+2,ind); } } public static class FenwickTree { int[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates public FenwickTree(int size) { array = new int[size + 1]; } public int rsq(int ind) { assert ind > 0; int sum = 0; while (ind > 0) { sum += array[ind]; //Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number ind -= ind & (-ind); } return sum; } public int rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, int value) { assert ind > 0; while (ind < array.length) { array[ind] += value; //Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number ind += ind & (-ind); } } public int size() { return array.length - 1; } } public static int[] shuffle(int[] a, Random gen){ for(int i = 0, n = a.length;i < n;i++) { int ind = gen.nextInt(n-i)+i; int d = a[i]; a[i] = a[ind]; a[ind] = d; } return a; } long power(long x, long y, int mod){ long ans = 1; while(y>0){ if(y%2==0){ x = (x*x)%mod; y/=2; } else{ ans = (x*ans)%mod; y--; } } return ans; } void soln() { is = System.in; out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); //out.close(); out.flush(); //tr(System.currentTimeMillis() - s + "ms"); } // To Get Input // Some Buffer Methods private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' // ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.*; import java.lang.*; import java.io.*; public class ER23C { static long s; public static void main (String[] args) throws java.lang.Exception { InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); long n = in.nextLong(); s = in.nextLong(); long l = 0, h = n; while (l < h) { long mid = (l + h ) / 2; // System.out.println("mid is : " + mid); // System.out.println("high is : " + h); //System.out.println("low is : " + l); if (Ok(mid)) h = mid; else l = mid + 1; } if (Ok(h)) w.println(n - h + 1); else w.println(n - h); w.close(); } static boolean Ok(long n) { int sum = 0; long temp = n; while (n > 0) { sum += (n % 10); n = n / 10; } //System.out.println("n is :" + n + " sum is : " + sum); return (temp - sum >= s); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new UnknownError(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int peek() { if (numChars == -1) return -1; if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { return -1; } if (numChars <= 0) return -1; } return buf[curChar]; } public void skip(int x) { while (x-- > 0) read(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public String nextString() { return next(); } public String next() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { StringBuffer buf = new StringBuffer(); int c = read(); while (c != '\n' && c != -1) { if (c != '\r') buf.appendCodePoint(c); c = read(); } return buf.toString(); } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public boolean hasNext() { int value; while (isSpaceChar(value = peek()) && value != -1) read(); return value != -1; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.IOException; import java.io.PrintWriter; import java.util.*; import java.io.*; public class Main { boolean[] b; int[] r; ArrayList<ArrayList<Integer>> q; public void dfs(int u, int p) { for (int i = 0; i < q.get(u).size(); i++) { int v = q.get(u).get(i); if (v != p) { r[v] = r[u] + 1; if (b[u]) { b[v] = b[u]; } dfs(v, u); } } } public void solve() throws IOException { long n = nextLong(); long s = nextLong(); long t = 0; if(s + 200 < n){ t = n - s - 200; } for(long i = s; i <= Math.min(s + 200,n); i++){ long p = 0; long u = i; while (u > 0){ p += u % 10; u /= 10; } if(i - p >= s){ t++; } } out.print(t); } BufferedReader br; StringTokenizer sc; PrintWriter out; public String nextToken() throws IOException { while (sc == null || !sc.hasMoreTokens()) { try { sc = new StringTokenizer(br.readLine()); } catch (Exception e) { return null; } } return sc.nextToken(); } public Integer nextInt() throws IOException { return Integer.parseInt(nextToken()); } public Long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static void main(String[] args) throws IOException { try { Locale.setDefault(Locale.US); } catch (Exception e) { } new Main().run(); } public void run() { try { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // br = new BufferedReader(new FileReader("lesson.in")); // out = new PrintWriter(new File("lesson.out")); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; public class C817{ void solve() { long n = nl(), s = nl(); long l = 0, r = n; while(l < r) { long mid = (l + r)/2; if(mid - digSum(mid) < s) l = mid + 1; else r = mid; } out.println(l - digSum(l) >= s ? (n - l + 1) : 0); } int digSum(long k) { int sum = 0; while(k != 0) { sum += k % 10; k /= 10; } return sum; } public static void main(String[] args){new C817().run();} private byte[] bufferArray = new byte[1024]; private int bufLength = 0; private int bufCurrent = 0; InputStream inputStream; PrintWriter out; public void run() { inputStream = System.in; out = new PrintWriter(System.out); solve(); out.flush(); } int nextByte() { if(bufLength==-1) throw new InputMismatchException(); if(bufCurrent>=bufLength) { bufCurrent = 0; try {bufLength = inputStream.read(bufferArray);} catch(IOException e) { throw new InputMismatchException();} if(bufLength<=0) return -1; } return bufferArray[bufCurrent++]; } boolean isSpaceChar(int x) {return (x<33 || x>126);} boolean isDigit(int x) {return (x>='0' && x<='9');} int nextNonSpace() { int x; while((x=nextByte())!=-1 && isSpaceChar(x)); return x; } int ni() { long ans = nl(); if ( Integer.MIN_VALUE <= ans && ans <= Integer.MAX_VALUE ) return (int)ans; throw new InputMismatchException(); } long nl() { long ans = 0; boolean neg = false; int x = nextNonSpace(); if(x=='-') { neg = true; x = nextByte(); } while(!isSpaceChar(x)) { if(isDigit(x)) { ans = ans*10 + x -'0'; x = nextByte(); } else throw new InputMismatchException(); } return neg ? -ans:ans; } String ns() { StringBuilder sb = new StringBuilder(); int x = nextNonSpace(); while(!isSpaceChar(x)) { sb.append((char)x); x = nextByte(); } return sb.toString(); } char nc() { return (char)nextNonSpace();} double nd() { return (double)Double.parseDouble(ns()); } char[] ca() { return ns().toCharArray();} char[] ca(int n) { char[] ans = new char[n]; int p =0; int x = nextNonSpace(); while(p<n) { ans[p++] = (char)x; x = nextByte(); } return ans; } int[] ia(int n) { int[] ans = new int[n]; for(int i=0;i<n;i++) ans[i]=ni(); return ans; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashMap; import java.util.Map; public class Ds { static long lim; public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] line = in.readLine().split("\\s+"); long n = Long.parseLong(line[0]); lim = Long.parseLong(line[1]); long pos = -1; for(int i=61; i>=0; i--) { long shift = (long) Math.pow(2, i); if(pos + shift - sumOfDigits(pos + shift) < lim) { pos += shift; } } pos++; if(pos <= n && pos- sumOfDigits(pos) >= lim) { System.out.println(n - pos + 1); } else { System.out.println(0); } } private static long sumOfDigits(long d) { long sum = 0; long num = d; while(num != 0) { sum += (num%10); num /= 10; } return sum; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; /** * * @author Jishnu_T */ public class ReallyBigNums { private static long[] factorArray(long s) { int d=0; long n=s; long f=1; while(n>0) { n=n/10; d++; f = f*10; } long[] fact = new long[d+1]; n=s; for(int i=d;i>=0;i--) { if(f==1) fact[i] = n; else { fact[i] = n/(f-1); n = n%(f-1); f=f/10; } } int carry=0; for(int i=0;i<=d;i++) { fact[i] = fact[i]+carry; if(fact[i]>9 || (i==0 && fact[i]>0)) { fact[i] = 0; carry = 1; for(int j=i-1;j>=0;j--) { fact[j] =0; } } else { carry = 0; } } return fact; } private static long bigNumCount(long n, long s) { if(s>=n) return 0; if(s==0) return n; long[] fact = factorArray(s); long startNum = 0; int len = fact.length; long tenPow = 1; for(int i=0;i<len;i++) { startNum = startNum+tenPow*fact[i]; tenPow = tenPow*10; } return(Math.max(0,n-startNum+1)); } private static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String args[]) { FastReader r = new FastReader(); long n = r.nextLong(); long s= r.nextLong(); System.out.println(bigNumCount(n,s)); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.math.BigInteger; import java.util.*; public class a { public static void main(String args[])throws IOException{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); OutputStream out=new BufferedOutputStream(System.out); String s[]=br.readLine().trim().split("\\ "); BigInteger a1=new BigInteger(s[0]); BigInteger a=new BigInteger(s[0]); String q=a.toString(); String q1=q.substring(q.length()-1, q.length()); a=a.subtract(new BigInteger(q1)); //System.out.println(a.toString()); BigInteger c=new BigInteger("1"); BigInteger b=new BigInteger(s[1]); int z=check(a,a.toString(),b); if(z==1) { out.write("0".getBytes()); out.flush(); //System.out.println("jwefcyuwe"); return; } while(a.compareTo(c)>0) { BigInteger d=a; if(d.subtract(c).compareTo(new BigInteger("9"))==-1) { break; } else { BigInteger mid=a; mid=mid.add(c); mid=mid.divide(new BigInteger("2")); //System.out.println(mid.toString()); if(check(mid,mid.toString(),b)==1) { c=mid; c=c.add(new BigInteger("1")); } else { a=mid; //System.out.println(a.toString()); } } } q=a.toString(); q1=q.substring(q.length()-1, q.length()); a=a.subtract(new BigInteger(q1)); BigInteger ans=a1.subtract(a); ans=ans.add(new BigInteger("1")); out.write(ans.toString().getBytes()); //System.out.print("sfvlksfv"); out.flush(); } static int check(BigInteger a,String s,BigInteger b) { int l=s.length(); long z=0; for(int i=0;i<l;i++) { z+=Long.parseLong(s.substring(i,i+1)); } BigInteger c=a.subtract(new BigInteger(Long.toString(z))); //System.out.println(c.toString()); return -1*c.compareTo(b); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { public static void main (String[] args) throws java.lang.Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); long n = Long.parseLong(st.nextToken()), s = Long.parseLong(st.nextToken()); long m = s; while (m-f(m)<s && m<=n) m++; System.out.println(Math.max(n-m+1, 0)); } public static int f(long n) { int sum = 0; for (long i=0, j=1L ; i<(int)Math.log10(n)+1 ; i++, j*=10) { sum += (n/j)%10; } return sum; } }
logn
817_C. Really Big Numbers
CODEFORCES
//package educational.round23; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class C3 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { long n = nl(); long S = nl(); long ret = Math.max(0, n-S+1); for(long i = S;i <= S + 300 && i <= n;i++){ long b = i; for(long x = i;x > 0;x /= 10){ b -= x % 10; } if(b < S)ret--; } out.println(ret); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new C3().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] in = br.readLine().split(" "); long n = Long.parseLong(in[0]), s = Long.parseLong(in[1]); Solver solver = new Solver(n, s); System.out.println(solver.solve()); } } class Solver { private long n, s; Solver(long n, long s) { this.n = n; this.s = s; } public long solve() { long low = 1, high = n; for (int i = 0; i < 72; ++i) { long x = low + (high - low) / 2; if (check(x)) high = x - 1; else low = x + 1; } return n - high; } private boolean check(long x) { long tmp = x; int sum = 0; while (tmp > 0) { sum += tmp % 10; tmp /= 10; } return x - sum >= s; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class A { public static boolean realbig (long num, long s) { String str = num + ""; String[] digs = str.split(""); int sum = 0; for(String dig : digs) { sum+= Integer.parseInt(dig); } if(num-sum < s) { return false; } else { return true; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long s = sc.nextLong(); sc.close(); long count = 0; long i = s; for(; i < s+200 && i <= n; i++) { if(realbig(i,s)) { count++; } } if(i <= n) { count+=n-i+1; } System.out.println(count); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.util.InputMismatchException; public class ques3 { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } public String next() { return nextString(); } public char nextChar(){ int c=read(); while (isSpaceChar(c)) { c = read(); } return (char)c; } public String nextString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public Long nextLong() { return Long.parseLong(nextString()); } public Double nextDouble() { return Double.parseDouble(nextString()); } } public static void main(String[] args) { InputReader sc=new InputReader(System.in); long n=sc.nextLong(); long s=sc.nextLong(); long start=0,end=n; while(start<end) { long mid=(start+end)/2; if(func(mid)>=s) end=mid; else start=mid+1; } if(func(start)>=s) System.out.println(n-start+1); else System.out.println(0); } public static long func(long n) { long temp=n; int sum=0; while(temp>0) { sum+=temp%10; temp/=10; } return n-sum; } }
logn
817_C. Really Big Numbers
CODEFORCES
//package educational.round23; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class C { InputStream is; PrintWriter out; String INPUT = ""; void solve() { long n = nl(); long S = nl(); long d = 1000000000000000000L; out.println(dfs(d, n, S)); } long dfs(long d, long n, long S) { if(d == 0)return 0L; long ret = 0; for(int i = 0;i <= n/d;i++){ if(S <= 0){ ret += Math.min(n-i*d+1, d); }else if(S < d){ ret += dfs(d/10, i == n/d ? n%d : d-1, S); } S -= d-1; } return ret; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new C().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class b817 { public static Scanner scn = new Scanner(System.in); public static void main(String[] args) { // TODO Auto-generated method stub long n = scn.nextLong(); long s = scn.nextLong(); long lo = 0; long hi = n ; while(lo<=hi) { long mid=(lo+hi)/2; if(check(mid, s))// no's greater thn this grtr { hi=mid-1; } else { lo=mid+1; } } if(check(lo, s)) { System.out.println(n-lo+1); } else // could check initially too { System.out.println("0"); } } public static boolean check(long n, long s) { long sum=0; long a=n; while(n>0) { sum=sum+(n%10); n=n/10; } if(a-sum>=s) { return true; } return false; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { int digitsum(long n) { int ans = 0; while (n > 0) { ans += n % 10; n /= 10; } return ans; } public void solve(int testNumber, InputReader in, PrintWriter out) { long n = in.nextLong(); long s = in.nextLong(); if (s >= n) { out.println(0); return; } long ans = s; while (ans < s + digitsum(ans)) { ans++; } if (n >= ans) { out.println(n - ans + 1); } else { out.println(0); } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class P817C { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long n = scan.nextLong(); long s = scan.nextLong(); long ans = 0; if (s > n) { System.out.println(0); return; } if (n > s+200) { ans += n-(s+200); n = s+200; } for (long i = s; i <= n; i++) { char[] num = (""+i).toCharArray(); int sum = 0; for (int j = 0; j < num.length; j++) sum += num[j] - '0'; if (i - sum >= s) ans++; } System.out.println(ans); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.*; import java.io.*; import java.lang.Math.*; public class Main{ static long s; static boolean check(long n){ int sum = 0; long storen=n; while(n>0){ int k = (int)(n%10); n /=10; sum+=k; } return storen-(long)sum >= s; } public static void main(String args[]){ PrintWriter pw=new PrintWriter(System.out); InputReader ip=new InputReader(System.in); long n; n=ip.nextLong(); s=ip.nextLong(); if(s>n){ pw.println("0"); } else{ long l=0,r=n; boolean possible=false; long mid=0; int it=100; while(it-->0){ mid = (l+r)/2; if(check(mid)){ r=mid; possible = true; } else{ l=mid+1; } // pw.println(mid); // pw.println(l+" "+r); } if(possible){ pw.println(n-l+1); } else{ pw.println("0"); } } pw.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long n = scanner.nextLong(); long s = scanner.nextLong(); long l = 0, r = n + 1; while(r - l > 1) { long mid = (l + r) / 2; long k = mid, sum = 0; while(k != 0) { sum += k % 10; k /= 10; } if(mid - sum >= s) r = mid; else l = mid; } System.out.print(n - r + 1); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.*; import java.io.*; public class CFEdu23C { static long sum(long n) { long ans=0; while(n>0) { ans+=(n%10); n/=10; } return ans; } static long BS(long l,long h,long s) { if(l<=h) { long m=(l+h)/2l; if(m-sum(m)>=s && (m==1 || (m-1)-sum(m-1)<s)) return m; else if(m-sum(m)>=s) return BS(l, m-1, s); else return BS(m+1, h, s); } return (h+1); } public static void main(String args[]) { InputReader in = new InputReader(System.in); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); /*------------------------------My Code starts here------------------------------*/ long n=in.nextLong(),s=in.nextLong(); long x=BS(0,n,s); out.print(n-x+1); out.close(); /*------------------------------The End------------------------------------------*/ } public static final long l = (int) (1e9 + 7); private static int[] nextIntArray(InputReader in, int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); return a; } private static long[] nextLongArray(InputReader in, int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = in.nextLong(); return a; } private static int[][] nextIntMatrix(InputReader in, int n, int m) { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) a[i][j] = in.nextInt(); } return a; } private static void show(int[] a) { for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } private static void show2DArray(char[][] a) { for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) System.out.print(a[i][j]); System.out.println(); } } static class Pair { private int first; private int second; public Pair(int i, int j) { this.first = i; this.second = j; } public int getFirst() { return first; } public int getSecond() { return second; } public void setFirst(int k) { this.first = k; } public void setSecond(int k) { this.second = k; } } static int modPow(int a, int b, int p) { int result = 1; a %= p; while (b > 0) { if ((b & 1) != 0) result = (result * a) % p; b = b >> 1; a = (a * a) % p; } return result; } public static void SieveOfEratosthenes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); prime[1] = false; int i, j; for (i = 2; i * i <= n; i++) { if (prime[i]) { for (j = i; j <= n; j += i) { if (j != i) prime[j] = false; } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream inputstream) { reader = new BufferedReader(new InputStreamReader(inputstream)); tokenizer = null; } public String nextLine() { String fullLine = null; while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { fullLine = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return fullLine; } return fullLine; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; public class Main { private static Reader in; private static PrintWriter out; public static void main(String args[]) throws IOException { in = new Reader(); out = new PrintWriter(System.out); long n = in.nextLong(); long s = in.nextLong(); long low = 0, mid = 0, high = n; while (low <= high) { mid = (low + high) / 2; if (func(mid, s)) { high = mid - 1; } else { low = mid + 1; } } out.println(n - low + 1); out.close(); } private static boolean func(long num, long s) { long sum = 0, temp = num; while (temp > 0) { sum += (temp) % 10; temp /= 10; } return ((num - sum) >= s); } static class Reader { final private int BUFFER_SIZE = 1 << 16; private DataInputStream din; private byte[] buffer; private int bufferPointer, bytesRead; public Reader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Reader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public String next() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == ' ' || c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public String nextLine() throws IOException { byte[] buf = new byte[64]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public long nextLong() throws IOException { long ret = 0; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (neg) return -ret; return ret; } public double nextDouble() throws IOException { double ret = 0, div = 1; byte c = read(); while (c <= ' ') c = read(); boolean neg = (c == '-'); if (neg) c = read(); do { ret = ret * 10 + c - '0'; } while ((c = read()) >= '0' && c <= '9'); if (c == '.') { while ((c = read()) >= '0' && c <= '9') { ret += (c - '0') / (div *= 10); } } if (neg) return -ret; return ret; } private void fillBuffer() throws IOException { bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); if (bytesRead == -1) buffer[0] = -1; } private byte read() throws IOException { if (bufferPointer == bytesRead) fillBuffer(); return buffer[bufferPointer++]; } public void close() throws IOException { if (din == null) return; din.close(); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class main implements Runnable{ static ArrayList <Integer> adj[]; static int co=0,f=0; static void Check2(int n){ adj=new ArrayList[n+1]; for(int i=0;i<=n;i++){ adj[i]=new ArrayList<>(); } } static void add(int i,int j){ adj[i].add(j); adj[j].add(i); } public static void main(String[] args) { new Thread(null, new main(), "Check2", 1<<26).start();// to increse stack size in java } static long mod=(long)(1e9+7); public void run() { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ //Scanner in=new Scanner(System.in); InputReader in = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); long n=in.nextLong(); long s=in.nextLong(); long l=1; long r=(long)(n); long ans=-1; while(l<=r){ long mid=(l+r)/2; if(ch(mid,s)){ ans=mid; r=mid-1; } else { l=mid+1; } } if(ans==-1)w.println(0); else w.println(n-ans+1); w.close(); } public boolean ch(long a,long s){ long p=0; long val=a; while(val>0){ p=p+val%10; val=val/10; } if(a-p>=s)return true; return false; } public boolean rec(int a,int b,int x,int y,int c,int d,int co){ if(a>x|b>y)return false; if(a<-100000||b<-100000||co>100000)return false; if(a==x&&b==y)return true; return (rec(a+c,b+d,x,y,c,d,co+1)||rec(a+c,b-d,x,y,c,d,co+1)||rec(a-c,b+d,x,y,c,d,co+1)||rec(a-c,b-d,x,y,c,d,co+1)); } static int gcd(int a,int b){ if(b==0)return a; return gcd(b,a%b); } static void dfs(int i,int v[],int val,int b[]){ if(v[i]==1)return ; v[i]=1; b[i]=val; Iterator <Integer> it=adj[i].iterator(); while(it.hasNext()){ int q=it.next(); dfs(q,v,val,b); } } static void sev(int a[],int n){ for(int i=2;i<=n;i++)a[i]=i; for(int i=2;i<=n;i++){ if(a[i]!=0){ for(int j=2*i;j<=n;){ a[j]=0; j=j+i; } } } } static class pair implements Comparable<pair> { int x,y; pair(int c,int d){ x=c; y=d; } public int compareTo(pair o){ return (this.x-o.x); //sort in incrementing order w.r.t to c } } static class node{ int y; int val; node(int a,int b){ y=a; val=b; } } static void rec(String s,int a,int b,int n){ if(b==n){ System.out.println(s); return ; } String p=s; if(a>b){ s=p+")" ; rec(s,a,b+1,n); } if(a<n){ s=p+"("; rec(s,a+1,b,n); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class Main{ public static boolean check(BigInteger a, BigInteger b){ long n = 0; String aStr = a.toString(); for (int i=0; i < aStr.length() ;i++ ) { n += Long.valueOf(aStr.charAt(i)-'0'); } return a.subtract(BigInteger.valueOf(n)).compareTo(b) >= 0; } public static void main(String[] args) { try(BufferedReader in = new BufferedReader(new InputStreamReader(System.in))){ String[] str = in.readLine().split(" "); BigInteger n = new BigInteger(str[0]); BigInteger s = new BigInteger(str[1]); BigInteger left = BigInteger.ONE; BigInteger right = new BigInteger(n.toString()).add(BigInteger.TEN); BigInteger TWO = BigInteger.ONE.add(BigInteger.ONE); BigInteger t; while(right.subtract(left).compareTo(BigInteger.ONE)>0){ t = left.add(right.subtract(left).divide(TWO)); if(check(t, s)){ right = t; }else{ left = t; } } BigInteger result = n.subtract(right).add(BigInteger.ONE); if (result.compareTo(BigInteger.ZERO)<=0) { System.out.println(0); }else{ System.out.println(result); } }catch (IOException e) { e.printStackTrace(); } } }
logn
817_C. Really Big Numbers
CODEFORCES
/** * DA-IICT * Author : Savaliya Sagar */ import java.io.*; import java.math.*; import java.util.*; public class C817 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { long n = nl(); long s = nl(); long l = 1; long r = n; long ans = 0; while(l<=r){ long mid = (l+r)/2; long sum = 0; long temp = mid; while(temp!=0){ sum += temp%10; temp/=10; } if(mid-sum<s){ ans = mid; l = mid+1; }else r = mid - 1; } out.println(n-ans); } void run() throws Exception { is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if (!INPUT.isEmpty()) tr(System.currentTimeMillis() - s + "ms"); } public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { new C817().run(); } catch (Exception e) { e.printStackTrace(); } } }, "1", 1 << 26).start(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != // ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeMap; public class P { public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); long N = sc.nextLong(), S = sc.nextLong(); long lo = 0, hi = N; long pivot = 0; while (lo <= hi) { long mid = lo + (hi - lo) / 2; int sumOfDigits = 0; long saveMid = mid; while (saveMid > 0) { sumOfDigits += saveMid % 10; saveMid /= 10; } if (mid - sumOfDigits < S) { pivot = mid; lo = mid + 1; } else hi = mid - 1; } System.out.println(N - pivot); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String f) throws FileNotFoundException { br = new BufferedReader(new FileReader(f)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException { return br.ready(); } public int[] nextIntArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public int[] nextIntArray1(int n) throws IOException { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextInt(); return a; } public int[] shuffle(int[] a, int n) { int[] b = new int[n]; for (int i = 0; i < n; i++) b[i] = a[i]; Random r = new Random(); for (int i = 0; i < n; i++) { int j = i + r.nextInt(n - i); int t = b[i]; b[i] = b[j]; b[j] = t; } return b; } public int[] nextIntArraySorted(int n) throws IOException { int[] a = nextIntArray(n); a = shuffle(a, n); Arrays.sort(a); return a; } public long[] nextLongArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public long[] nextLongArray1(int n) throws IOException { long[] a = new long[n + 1]; for (int i = 1; i <= n; i++) a[i] = nextLong(); return a; } public long[] nextLongArraySorted(int n) throws IOException { long[] a = nextLongArray(n); Random r = new Random(); for (int i = 0; i < n; i++) { int j = i + r.nextInt(n - i); long t = a[i]; a[i] = a[j]; a[j] = t; } Arrays.sort(a); return a; } } }
logn
817_C. Really Big Numbers
CODEFORCES
/* * DA-IICT * Author: Jugal Kalal */ import java.util.*; import java.io.*; import java.math.*; import java.text.DecimalFormat; public class Practice{ static long MOD=(long)Math.pow(10,9)+7; public static void main(String args[]) { new Thread(null, new Runnable() { public void run() { try{ solve(); w.close(); } catch(Exception e){ e.printStackTrace(); } } }, "1", 1 << 26).start(); } static InputReader in; static PrintWriter w; static void solve(){ in = new InputReader(System.in); w = new PrintWriter(System.out); long n=in.nextLong(); long s=in.nextLong(); long low=1,high=n,ans=-1; while(low<=high){ long mid=(low+high)/2; if(check(mid,s)){ ans=mid; high=mid-1; }else{ low=mid+1; } } if(ans==-1){ w.println(0); }else w.println(n-ans+1); } static boolean check(long n,long s){ long temp=n; long sum=0; while(temp>0){ sum+=temp%10; temp=temp/10; } if(n-sum>=s){ return true; } return false; } static int adj[][]; // static ArrayList<Integer> adj[]; //Adjacency Lists static int V; // No. of vertices // Constructor static void Graph(int v){ V = v; adj=new int[v][v]; // adj = new ArrayList[v]; // for (int i=0; i<v; ++i){ // adj[i] = new ArrayList(); // } } // Function to add an edge into the graph static void addEdge(int u,int v,int w){ // adj[u].add(v); // adj[v].add(u); adj[u][v]=w; } // static void bfs(int s,int n){ // boolean visited[]=new boolean[n]; // LinkedList<Integer> queue=new LinkedList<Integer>(); // queue.add(s); // visited[s]=true; // while(!queue.isEmpty()){ // int num=queue.pop(); //// System.out.println(ans.toString()); // for(int i=0;i<adj[num].size();i++){ // if(!visited[adj[num].get(i)]){ // visited[adj[num].get(i)]=true; // queue.add(adj[num].get(i)); // } // } // } // } static long gcd(long a,long b){ if(a==0){ return b; } return gcd(b%a,a); } static long power(long base, long exponent, long modulus){ long result = 1L; while (exponent > 0) { if (exponent % 2L == 1L) result = (result * base) % modulus; exponent = exponent >> 1; base = (base * base) % modulus; } return result; } static HashMap<Long,Long> primeFactors(long n){ HashMap<Long,Long> ans=new HashMap<Long,Long>(); // Print the number of 2s that divide n while (n%2L==0L) { if(ans.containsKey(2L)){ ans.put(2L,ans.get(2L)+1L); }else{ ans.put(2L,1L); } n /= 2L; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (long i = 3; i <= Math.sqrt(n); i+= 2L) { // While i divides n, print i and divide n while (n%i == 0) { if(ans.containsKey(i)){ ans.put(i,ans.get(i)+1L); }else{ ans.put(i,1L); } n /= i; } } // This condition is to handle the case whien // n is a prime number greater than 2 if (n > 2) ans.put(n,1L); return ans; } ////for marking all prime numbers greater than 1 and less than equal to N static void sieve(int N) { boolean isPrime[]=new boolean[N+1]; isPrime[0] = true; isPrime[1] = true; for(int i = 2; i * i <= N; ++i) { if(isPrime[i] == false) {//Mark all the multiples of i as composite numbers for(int j = i * i; j <= N ;j += i) isPrime[j] = true; } } } // //if str2 (pattern) is subsequence of str1 (Text) or not // static boolean function(String str1,String str2){ // str2 = str2.replace("", ".*"); //returns .*a.*n.*n.*a. // return (str1.matches(str2)); // returns true // } static int Arr[]; static long size[]; //modified initialize function: static void initialize(int N){ Arr=new int[N]; size=new long[N]; for(int i = 0;i<N;i++){ Arr[ i ] = i ; size[ i ] = 1; } } static boolean find(int A,int B){ if( root(A)==root(B) ) //if A and B have same root,means they are connected. return true; else return false; } // modified root function. static void weighted_union(int A,int B,int n){ int root_A = root(A); int root_B = root(B); if(size[root_A] < size[root_B ]){ Arr[ root_A ] = Arr[root_B]; size[root_B] += size[root_A]; } else{ Arr[ root_B ] = Arr[root_A]; size[root_A] += size[root_B]; } } static int root (int i){ while(Arr[ i ] != i){ Arr[ i ] = Arr[ Arr[ i ] ] ; i = Arr[ i ]; } return i; } static boolean isPrime(long n) { if(n < 2L) return false; if(n == 2L || n == 3L) return true; if(n%2L == 0 || n%3L == 0) return false; long sqrtN = (long)Math.sqrt(n)+1L; for(long i = 6L; i <= sqrtN; i += 6L) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } // static HashMap<Integer,Integer> level;; // static HashMap<Integer,Integer> parent; static int maxlevel=0; // static boolean T[][][]; // static void subsetSum(int input[], int total, int count) { // T = new boolean[input.length + 1][total + 1][count+1]; // for (int i = 0; i <= input.length; i++) { // T[i][0][0] = true; // for(int j = 1; j<=count; j++){ // T[i][0][j] = false; // } // } // int sum[]=new int[input.length+1]; // for(int i=1;i<=input.length;i++){ // sum[i]=sum[i-1]+input[i-1]; // } // for (int i = 1; i <= input.length; i++) { // for (int j = 1; j <= (int)Math.min(total,sum[i]); j++) { // for (int k = 1; k <= (int)Math.min(i,count); k++){ // if (j >= input[i - 1]) {//Exclude and Include // T[i][j][k] = T[i - 1][j][k] || T[i - 1][j - input[i - 1]][k-1]; // } else { // T[i][j][k] = T[i-1][j][k]; // } // } // } // } // } // static <K,V extends Comparable<? super V>> // SortedSet<Map.Entry<K,V>> entriesSortedByValues(Map<K,V> map) { // SortedSet<Map.Entry<K,V>> sortedEntries = new TreeSet<Map.Entry<K,V>>( // new Comparator<Map.Entry<K,V>>() { // @Override public int compare(Map.Entry<K,V> e1, Map.Entry<K,V> e2) { // int res = e2.getValue().compareTo(e1.getValue()); // return res != 0 ? res : 1; // } // } // ); // sortedEntries.addAll(map.entrySet()); // return sortedEntries; // } //minimum prime factor of all the numbers less than n static int minPrime[]; static void minimumPrime(int n){ minPrime=new int[n+1]; minPrime[1]=1; for (int i = 2; i * i <= n; ++i) { if (minPrime[i] == 0) { //If i is prime for (int j = i * i; j <= n; j += i) { if (minPrime[j] == 0) { minPrime[j] = i; } } } } for (int i = 2; i <= n; ++i) { if (minPrime[i] == 0) { minPrime[i] = i; } } } static long modInverse(long A, long M) { long x=extendedEuclid(A,M)[0]; return (x%M+M)%M; //x may be negative } static long[] extendedEuclid(long A, long B) { if(B == 0) { long d = A; long x = 1; long y = 0; return new long[]{x,y,d}; } else { long arr[]=extendedEuclid(B, A%B); long temp = arr[0]; arr[0] = arr[1]; arr[1] = temp - (A/B)*arr[1]; return arr; } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public String nextLine() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class CodeforcesC { public static void main(String[] args) { Scanner ob = new Scanner(System.in); long n = ob.nextLong(); long s = ob.nextLong(); long l = 1; long r = n; while(l<=r){ long mid = (l + r)/2; if(reallybignumber(mid,s)){ r = mid-1; }else{ l = mid +1; } } /******long l1 = l; ***while(l1<=n) { System.out.print(l1 + " "); l1++; }*///////// System.out.println(n-l+1); } private static boolean reallybignumber(long n,long s) { long m = n; long sum=0; int d=1; while(m>0){ long rem = m % 10; sum = rem * d + sum; m = m / 10; } if(n-sum >= s) return true; else return false; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.BitSet; import java.util.Calendar; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.SortedSet; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; /** * # * @author pttrung */ public class C_Edu_Round_23 { public static long MOD = 1000000007; static long[][][][] dp; public static void main(String[] args) throws FileNotFoundException { // PrintWriter out = new PrintWriter(new FileOutputStream(new File( // "output.txt"))); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(); long n = in.nextLong(); long s = in.nextLong(); if (s == 0) { out.println(n); } else { String N = "" + n; dp = new long[N.length()][163][2][2]; long re = 0; for (int i = 1; i < 163 && i <= n; i++) { long tmp = s + i; if (tmp <= n) { String S = "" + tmp; while(S.length() < N.length()){ S = '0' + S; } for (long[][][] a : dp) { for (long[][] b : a) { for (long[] c : b) { Arrays.fill(c, -1); } } } re += cal(0, i, 0, 0, N, S); } } out.println(re); } out.close(); } public static long cal(int index, int left, int lessThanN, int largerThanS, String n, String s) { if (index == n.length()) { if (left == 0) { return 1; } return 0; } if (dp[index][left][lessThanN][largerThanS] != -1) { return dp[index][left][lessThanN][largerThanS]; } int x = n.charAt(index) - '0'; int y = s.charAt(index) - '0'; long re = 0; for (int i = 0; i < 10 && i <= left; i++) { if (i <= x || lessThanN == 1) { if (i >= y || largerThanS == 1) { re += cal(index + 1, left - i, i < x ? 1 : lessThanN, i > y ? 1 : largerThanS, n, s); } } } return dp[index][left][lessThanN][largerThanS] = re; } public static int[] KMP(String val) { int i = 0; int j = -1; int[] result = new int[val.length() + 1]; result[0] = -1; while (i < val.length()) { while (j >= 0 && val.charAt(j) != val.charAt(i)) { j = result[j]; } j++; i++; result[i] = j; } return result; } public static boolean nextPer(int[] data) { int i = data.length - 1; while (i > 0 && data[i] < data[i - 1]) { i--; } if (i == 0) { return false; } int j = data.length - 1; while (data[j] < data[i - 1]) { j--; } int temp = data[i - 1]; data[i - 1] = data[j]; data[j] = temp; Arrays.sort(data, i, data.length); return true; } public static int digit(long n) { int result = 0; while (n > 0) { n /= 10; result++; } return result; } public static double dist(long a, long b, long x, long y) { double val = (b - a) * (b - a) + (x - y) * (x - y); val = Math.sqrt(val); double other = x * x + a * a; other = Math.sqrt(other); return val + other; } public static class Point implements Comparable<Point> { int x, y; public Point(int start, int end) { this.x = start; this.y = end; } @Override public int hashCode() { int hash = 5; hash = 47 * hash + this.x; hash = 47 * hash + this.y; return hash; } @Override public boolean equals(Object obj) { if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } final Point other = (Point) obj; if (this.x != other.x) { return false; } if (this.y != other.y) { return false; } return true; } @Override public int compareTo(Point o) { return Integer.compare(x, o.x); } } public static class FT { long[] data; FT(int n) { data = new long[n]; } public void update(int index, long value) { while (index < data.length) { data[index] += value; index += (index & (-index)); } } public long get(int index) { long result = 0; while (index > 0) { result += data[index]; index -= (index & (-index)); } return result; } } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } public static long pow(long a, long b, long MOD) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2, MOD); if (b % 2 == 0) { return val * val % MOD; } else { return val * (val * a % MOD) % MOD; } } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner() throws FileNotFoundException { // System.setOut(new PrintStream(new BufferedOutputStream(System.out), true)); br = new BufferedReader(new InputStreamReader(System.in)); // br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt")))); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { throw new RuntimeException(); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { st = null; try { return br.readLine(); } catch (Exception e) { throw new RuntimeException(); } } public boolean endLine() { try { String next = br.readLine(); while (next != null && next.trim().isEmpty()) { next = br.readLine(); } if (next == null) { return true; } st = new StringTokenizer(next); return st.hasMoreTokens(); } catch (Exception e) { throw new RuntimeException(); } } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Amine L */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastInput in = new FastInput(inputStream); FastOutput out = new FastOutput(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, FastInput in, FastOutput out) { long n = in.nextLong(); long s = in.nextLong(); long cnt = 0; long res = 0; for (long i = s; i <= Math.min(s + 200, n); i++) { long d = i; int sum = 0; while (d > 0) { long l = d % 10; sum += l; d /= 10; } if ((i - sum) >= s) { cnt++; } } long tmp = n - Math.min(n, s + 200); if (tmp < 0) tmp = 0; cnt += tmp; out.println(cnt); } } static class FastInput { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private FastInput.SpaceCharFilter filter; public FastInput(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class FastOutput { private final PrintWriter writer; public FastOutput(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public FastOutput(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(long i) { writer.println(i); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; public class Main { private InputStream is; private PrintWriter out; int time = 0, dp[][], DP[][], start[], parent[], end[], val[], black[], MOD = (int)(1e9+7), arr[], arr1[]; int MAX = 10000000, N, K, p; ArrayList<Integer>[] amp, amp1; boolean b[], b1[]; Pair prr[]; char ch[][]; HashSet<Integer> hs = new HashSet<>(); public static void main(String[] args) throws Exception { new Thread(null, new Runnable() { public void run() { try { //new Main().soln(); } catch (Exception e) { System.out.println(e); } } }, "1", 1 << 26).start(); new Main().soln(); } void solve() { long n = nl(), s = nl(); long low = 1, high = n+1; long ans = high; while(low<=high){ long mid = (high+low)/2; if((mid - getSum(mid))>=s){ high = mid-1; ans = mid; } else{ low = mid+1; } } System.out.println(Math.max(0, n-ans+1)); } int getSum(long s){ String str = Long.toString(s); int ans = 0; for(char ch : str.toCharArray()) ans += (ch-'0'); return ans; } int recur(int x){ //System.out.println(x); int ans = 0; b[x] = true; for(int i : amp[x]){ if(!b[i]){ b[i] = true; ans = Math.max(recur(i), ans); b[i] = false; } } return 1+ans; } int max = 0; int getParent(int x){ //System.out.println(x+" "+parent[x]); if(parent[x]!=x){ parent[x] = getParent(parent[x]); } return parent[x]; } int bfs(int x){ b[x] = true; Queue<Integer> q = new LinkedList<>(); q.add(x); while(!q.isEmpty()){ int y = q.poll(); for(int i : amp[y]){ if(!b[i]){ b[i] = true; val[i] = val[y]+1; max = Math.max(val[i], max); q.add(i); } } } return max; } class Pair implements Comparable<Pair>{ int u, v, r; Pair(int u, int v){ this.u = u; this.v = v; }public int hashCode() { return Objects.hash(); } public boolean equals(Object o) { Pair other = (Pair) o; return ((u == other.u && v == other.v));// || (v == other.u && u == other.v)); } public int compareTo(Pair other) { //return Integer.compare(other.r, r); return Long.compare(u, other.u) != 0 ? (Long.compare(u, other.u)) : (Long.compare(other.v,v)); } public String toString() { return "[u=" + u + ", v=" + v + "]"; } } int min(int x,int y){ if(x<y) return x; return y; } int max(int x,int y){ if(x>y) return x; return y; } void dfs(int x){ b[x] = true; for(int i : amp[x]){ if(!b[i]){ dfs(i); } } } void buildGraph(int m){ while(m-->0) { int x = ni()-1, y = ni()-1; amp[x].add(y); amp[y].add(x); } } long modInverse(long a, long mOD2){ return power(a, mOD2-2, mOD2); } long power(long x, long y, long m) { if (y == 0) return 1; long p = power(x, y/2, m) % m; p = (p * p) % m; return (y%2 == 0)? p : (x * p) % m; } boolean isPrime(int x){ for(int i = 2;i*1L*i<=x;i++) if(x%i==0) return false; return true; } public long gcd(long a, long b){ if(b==0) return a; return gcd(b,a%b); } void failFn(String str, int arr[]){ int len = 0; arr[0] = 0; int i = 1; while(i<arr.length){ if(str.charAt(i)==str.charAt(len)){ arr[i++] = ++len; continue; } if(len == 0){ arr[i] = len; i++; continue; } if(str.charAt(i)!=str.charAt(len)){ len = arr[len-1]; } } } static class ST1{ int arr[], st[], size; ST1(int a[]){ arr = a.clone(); size = 10*arr.length; st = new int[size]; build(0,arr.length-1,1); } void build(int ss, int se, int si){ if(ss==se){ st[si] = arr[ss]; return; } int mid = (ss+se)/2; int val = 2*si; build(ss,mid,val); build(mid+1,se,val+1); st[si] = (st[val]+ st[val+1]); } int get(int ss, int se, int l, int r, int si){ if(l>se || r<ss || l>r) return Integer.MAX_VALUE; if(l<=ss && r>=se) return st[si]; int mid = (ss+se)/2; int val = 2*si; return (get(ss,mid,l,r,val)+ get(mid+1,se,l,r,val+1)); } } static class ST{ int arr[],lazy[],n; ST(int a){ n = a; arr = new int[10*n]; lazy = new int[10*n]; } void up(int l,int r,int val){ update(0,n-1,0,l,r,val); } void update(int l,int r,int c,int x,int y,int val){ if(lazy[c]!=0){ lazy[2*c+1]+=lazy[c]; lazy[2*c+2]+=lazy[c]; if(l==r) arr[c]+=lazy[c]; lazy[c] = 0; } if(l>r||x>y||l>y||x>r) return; if(x<=l&&y>=r){ lazy[c]+=val; return ; } int mid = l+r>>1; update(l,mid,2*c+1,x,y,val); update(mid+1,r,2*c+2,x,y,val); arr[c] = (arr[2*c+1]+ arr[2*c+2]); } int an(int ind){ return ans(0,n-1,0,ind); } int ans(int l,int r,int c,int ind){ if(lazy[c]!=0){ lazy[2*c+1]+=lazy[c]; lazy[2*c+2]+=lazy[c]; if(l==r) arr[c]+=lazy[c]; lazy[c] = 0; } if(l==r) return arr[c]; int mid = l+r>>1; if(mid>=ind) return ans(l,mid,2*c+1,ind); return ans(mid+1,r,2*c+2,ind); } } public static class FenwickTree { int[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates public FenwickTree(int size) { array = new int[size + 1]; } public int rsq(int ind) { assert ind > 0; int sum = 0; while (ind > 0) { sum += array[ind]; //Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number ind -= ind & (-ind); } return sum; } public int rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, int value) { assert ind > 0; while (ind < array.length) { array[ind] += value; //Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number ind += ind & (-ind); } } public int size() { return array.length - 1; } } public static int[] shuffle(int[] a, Random gen){ for(int i = 0, n = a.length;i < n;i++) { int ind = gen.nextInt(n-i)+i; int d = a[i]; a[i] = a[ind]; a[ind] = d; } return a; } long power(long x, long y, int mod){ long ans = 1; while(y>0){ if(y%2==0){ x = (x*x)%mod; y/=2; } else{ ans = (x*ans)%mod; y--; } } return ans; } void soln() { is = System.in; out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); //out.close(); out.flush(); //tr(System.currentTimeMillis() - s + "ms"); } // To Get Input // Some Buffer Methods private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if (lenbuf == -1) throw new InputMismatchException(); if (ptrbuf >= lenbuf) { ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if (lenbuf <= 0) return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while ((b = readByte()) != -1 && isSpaceChar(b)) ; return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char) skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' // ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while (p < n && !(isSpaceChar(b))) { buf[p++] = (char) b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = readByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if (!oj) System.out.println(Arrays.deepToString(o)); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class C { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long n = scan.nextLong(); long s = scan.nextLong(); long low = 0; long high = n + 1; while (high-low>1) { long sum = 0; long mid = (high + low) / 2; long value = findSum(mid, sum); if (mid - value >= s) high = mid; else low = mid; } System.out.println(n - high + 1); scan.close(); } public static long findSum(long n, long sum) { if (n == 0) return sum; return findSum(n / 10, sum + n % 10); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.*; public class ed817Q3 { public static void main(String[] args){ InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = 1; for(int zxz=0;zxz<t;zxz++){ // my code starts here long n = in.nextLong(); long s = in.nextLong(); long start=0,end=n; long ans=n+1; while(start<=end){ long mid = start+(end-start)/2; if(mid-digitSum(mid)>=s){ ans = mid; end = mid-1; } else{ start=mid+1; } } System.out.println(n-ans+1); // my code ends here } } static int digitSum(long n){ int sum=0; while(n>0){ sum+=n%10; n=n/10; } return sum; } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class CF817C { static long count(long x) { return x < 10 ? x : count(x / 10) + x % 10; } static boolean check(long x, long s) { return x - count(x) >= s; } public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); long n = Long.parseLong(st.nextToken()); long s = Long.parseLong(st.nextToken()); int d = 9 * 18; long cnt; if (n >= s + d) { cnt = n - s - d; for (long x = s; x <= s + d; x++) if (check(x, s)) cnt++; } else { cnt = 0; for (long x = s; x <= n; x++) if (check(x, s)) cnt++; } System.out.println(cnt); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.*; import java.io.*; @SuppressWarnings("Duplicates") public class C817 { private FastScanner in; private PrintWriter out; private long calcSum(long x) { int ans = 0; while (x > 0) { ans += x % 10; x /= 10; } return ans; } private long calcDiff(long x) { return x - calcSum(x); } private long binSearch(long n, long s) { long l = 0; long r = n + 1; while (r - l > 1) { long m = (r + l) >> 1; if (calcDiff(m) >= s) { r = m; } else { l = m; } } return l; } private void solve() throws IOException { long n = in.nextLong(); long s = in.nextLong(); long ans = binSearch(n, s); out.println(n - ans); } private void run() { try { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } catch (IOException e) { e.printStackTrace(); } } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] arg) { new C817().run(); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class Main { public static void main(String[] args) { setup(); xuly(); } private static long dp[][] = new long[20][170]; private static void setup() { dp[0][0] = 1; for (int i = 1; i < 20; i ++) for (int j = 0; j < 170; j ++) for (int k = Math.max(j - 9, 0); k <= j; k ++) dp[i][j] += dp[i - 1][k]; } private static int sumD(long x) { int ret = 0; while(x > 0) { ret += x % 10; x /= 10; } return ret; } private static long numSatisfy(long limit, int sumDigit) { long ret = 0; int curSum = sumD(limit); if (curSum == sumDigit) ret ++; for (int i = 0; i < 20; i ++) { int bound = (int) (limit % 10); curSum -= bound; for (int d = 0; d < bound && curSum + d <= sumDigit; d ++) ret += dp[i][sumDigit - curSum - d]; limit /= 10; } return ret; } private static void xuly() { Scanner scanner = new Scanner(System.in); long n = scanner.nextLong(); long s = scanner.nextLong(); long ans = 0; for (int sum = 1; sum < 170; sum ++) if (n >= s + sum) ans += numSatisfy(n, sum) - numSatisfy(s + sum - 1, sum); System.out.print(ans); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.*; import java.io.*; public class _817C { static long sum = 0; static long BSearch2(long st, long end, long lim) { if (st > end) return 0; long mid = (st + end) >> 1; if (mid - sumDigit(mid) >= lim) { sum = mid; return BSearch2(st, mid - 1, lim); } if (mid - sumDigit(mid) < lim) return BSearch2(mid + 1, end, lim); return 0; } public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(in.readLine()); long s = Long.parseLong(st.nextToken()); long n = Long.parseLong(st.nextToken()); BSearch2(1, s, n); if (sum == 0) System.out.println("0"); else System.out.println(s - sum + 1); } static long sumDigit(long z) { String s = "" + z; int c = 0; for (int i = 0; i < s.length(); i++) c += s.charAt(i); return c - s.length() * 0x30; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class C { public static void main(String[] args) throws InterruptedException{ FastScanner scan = new FastScanner(); PrintWriter out = new PrintWriter(System.out); long n = scan.nextLong(), s = scan.nextLong(); long lo = 1, hi = n+1; for(int bs = 0; bs < 100; bs++) { long mid = (lo+hi)>>1; long mid2 = mid; long c = 0; while(mid > 0) { c += mid%10; mid /= 10; } if(mid2-c < s) lo = mid2; else hi = mid2; } out.println(n-lo); out.close(); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e){e.printStackTrace();} } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try {st = new StringTokenizer(br.readLine());} catch (Exception e) {e.printStackTrace();} return st.nextToken(); } public int nextInt() {return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} public String nextLine() { String line = ""; if(st.hasMoreTokens()) line = st.nextToken(); else try {return br.readLine();}catch(IOException e){e.printStackTrace();} while(st.hasMoreTokens()) line += " "+st.nextToken(); return line; } public int[] nextIntArray(int n) { int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongArray(int n){ long[] a = new long[n]; for(int i = 0; i < n; i++) a[i] = nextLong(); return a; } public double[] nextDoubleArray(int n){ double[] a = new double[n]; for(int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public char[][] nextGrid(int n, int m){ char[][] grid = new char[n][m]; for(int i = 0; i < n; i++) grid[i] = next().toCharArray(); return grid; } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.*; import javax.swing.Painter; import java.io.*; public class C { FastScanner in; PrintWriter out; boolean systemIO = true; public static class Pair implements Comparable<Pair> { int x; int y; public Pair(int x, int y) { super(); this.x = x; this.y = y; } @Override public int compareTo(Pair o) { return x - o.x; } } public static void quickSort(long[] a, int from, int to) { if (to - from <= 1) { return; } int i = from; int j = to - 1; long x = a[from + (new Random()).nextInt(to - from)]; while (i <= j) { while (a[i] < x) { i++; } while (a[j] > x) { j--; } if (i <= j) { long t = a[i]; a[i] = a[j]; a[j] = t; i++; j--; } } quickSort(a, from, j + 1); quickSort(a, j + 1, to); } public static long check(long x) { long sum = 0; long k = x; while (x > 0) { sum += x % 10; x /= 10; } return k - sum; } public void solve() throws IOException { long n = in.nextLong(); long s = in.nextLong(); long ans = 0; for (long i = s; i <= Math.min(n, s + 10000L); i++) { if (check(i) >= s) { ans++; } } ans += (n - Math.min(n, s + 10000L)); System.out.println(ans); } public void run() throws IOException { if (systemIO) { in = new FastScanner(System.in); out = new PrintWriter(System.out); } else { in = new FastScanner(new File("input.txt")); out = new PrintWriter(new File("output.txt")); } solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String nextLine() { try { return br.readLine(); } catch (IOException e) { return null; } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } // AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA public static void main(String[] arg) throws IOException { new C().run(); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * * @author Fuad */ public class Codeforces { private static boolean greater(long mid, long s) { int sum = 0; long num = mid; while (num != 0) { sum += (num % 10); num /= 10; } return mid - sum >= s; } static class pair { int first; int second; pair(int f, int s) { first = f; second = s; } pair() { } } public static void main(String[] args) throws Exception { // TODO code application logic here BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); long n, s; String arr[] = br.readLine().split(" "); n = Long.parseLong(arr[0]); s = Long.parseLong(arr[1]); long l = 1; long h = n; while (l < h) { long mid = (l + h) / 2; if (greater(mid, s)) { h = mid; } else { l = mid + 1; } } System.out.println(greater(h, s) ? n - h + 1 : 0); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.Arrays; import java.util.HashSet; import java.util.Iterator; import java.util.Set; import java.util.StringTokenizer; public class Solution { long sum(long n){ long sm = 0; while(n!=0){ sm+=(n%10); n/=10; } return sm; } void solve() throws IOException{ long n = in.nextLong(); long s = in.nextLong(); long l = 0; long h = n+1; long ans = -1; while(l<h){ long mid = (l + h)/2; long sum = sum(mid); if(mid - sum >= s){ ans = mid; h = mid; } else l = mid+1; // System.out.println(mid); // ans = (mid+1); } // System.out.println(ans); if(ans==-1) out.println("0"); else out.println(n+1-ans); } // solve class FastScanner{ BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(""); } public int nextInt() throws IOException{ if(st.hasMoreTokens()) return Integer.parseInt(st.nextToken()); else{ st = new StringTokenizer(br.readLine()); return nextInt(); } } public long nextLong() throws IOException{ if(st.hasMoreTokens()) return Long.parseLong(st.nextToken()); else{ st = new StringTokenizer(br.readLine()); return nextLong(); } } public String readLine() throws IOException{ return br.readLine(); } } FastScanner in = new FastScanner(); static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out)); public static void main(String args[])throws IOException{ new Solution().solve(); out.close(); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; public class A { static int[] UPPER = new int[64], LOWER = new int[64]; static long[][][] memo; static long dp(int bit, int lims, int digs) { if(bit == -1) return digs == 0 ? 1 : 0; if(memo[lims][bit][digs] != -1) return memo[lims][bit][digs]; long ret = 0; for(int d = 0, end = digs < 10 ? digs + 1 : 10; d < end; ++d) if(((lims & 1) == 1 || d >= LOWER[bit]) && ((lims & 2) == 2 || d <= UPPER[bit])) ret += dp(bit - 1, lims | (d > LOWER[bit] ? 1 : 0) | (d < UPPER[bit] ? 2 : 0), digs - d); return memo[lims][bit][digs] = ret; } static void create(int[] x, long n) { for(int i = 0; i < 64; ++i) { x[i] = (int) (n % 10); n /= 10; } } static void prepMemo(int sod) { memo = new long[4][64][sod + 1]; for(long[][] x: memo) for(long[] y: x) Arrays.fill(y, -1); } public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); long n = sc.nextLong(), s = sc.nextLong(); create(UPPER, n); long ans = 0; for(int sod = 1; sod <= 162; ++sod) { create(LOWER, s + sod); prepMemo(sod); ans += dp(63, 0, sod); } out.println(ans); out.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public Scanner(String s) throws FileNotFoundException{ br = new BufferedReader(new FileReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { return Double.parseDouble(next()); } public boolean ready() throws IOException {return br.ready();} } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.*; public class ed817Q3 { public static void main(String[] args){ InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int t = 1; for(int zxz=0;zxz<t;zxz++){ // my code starts here long n = in.nextLong(); long s = in.nextLong(); long start=s,end=n; long ans=n+1; while(start<=end){ long mid = start+(end-start)/2; if(mid-digitSum(mid)>=s){ ans = mid; end = mid-1; } else{ start=mid+1; } } System.out.println(n-ans+1); // my code ends here } } static int digitSum(long n){ int sum=0; while(n>0){ sum+=n%10; n=n/10; } return sum; } static class InputReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) c = snext(); int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public String readString() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, Scanner in, PrintWriter out) { long n = in.nextLong(); long s = in.nextLong(); long ans = 0; long i = 0; for (i = s; i <= n; i++) { long t = i - sum(i); if (t >= s) { if (i % 10 == 9) { break; } ans++; } } if (n >= s) { out.println(ans - i + n + 1); } else { out.println(0); } } static long sum(long a) { long sum = 0; while (a != 0) { sum += (a % 10); a /= 10; } return sum; } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { class IOManager { BufferedReader reader; PrintWriter writer; StringTokenizer tokenizer; IOManager() { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(new BufferedOutputStream(System.out)); } IOManager(String file) throws FileNotFoundException { reader = new BufferedReader(new FileReader(file)); writer = new PrintWriter(new BufferedOutputStream(System.out)); } String next() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { String line = reader.readLine(); if (line == null) throw new IOException("No lines left."); tokenizer = new StringTokenizer(line); } return tokenizer.nextToken(); } public Integer nextInt() throws IOException { return Integer.parseInt(next()); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public Double nextDouble() throws IOException { return Double.parseDouble(next()); } public void writeSpace(Object obj) { writer.print(obj.toString() + " "); } public void writeLine(Object obj) { writer.println(obj.toString()); } public void writeDouble(Double x, int n) { String format = "%." + n + "f"; writer.printf(format, x); } public void write(Object obj) { writer.print(obj.toString()); } public void close() { writer.close(); } } class Pair implements Comparable { public long u, v; public Pair(long u, long v) { this.u = u; this.v = v; } @Override public int compareTo(Object o) { Pair that = (Pair) o; if (Long.compare(u, that.u) != 0) return Long.compare(u, that.u); return Long.compare(v, that.v); } } class Graph { public int n, m; public List<Integer>[] adj; public Graph(int n, int m) { this.n = n; this.m = m; adj = new List[n + 1]; for (int i = 0; i <= n; ++i) { adj[i] = new ArrayList<>(); } } public void add(int u, int v) { adj[u].add(v); } public void add2Ways(int u, int v) { add(u, v); add(v, u); } public void dfs(int i, boolean fr[]) { fr[i] = false; for (int j: adj[i]) { if (fr[j]) { dfs(j, fr); } } } public boolean isConnected() { boolean fr[] = new boolean[n + 1]; Arrays.fill(fr, true); dfs(1, fr); for (int i = 2; i <= n; ++i) { if (fr[i]) return false; } return true; } public boolean hasEuclideanPath() { if (!isConnected()) return false; int cnt = 0; for (int i = 1; i <= n; ++i) { if (adj[i].size() % 2 == 1) cnt++; } return cnt <= 2; } public boolean dfsHamiltonian(int i, boolean[] fr, int reached) { fr[i] = false; reached++; if (reached == n) return true; for (int j: adj[i]) { if (fr[j]) { if (dfsHamiltonian(j, fr, reached)) return true; } } fr[i] = true; return false; } public boolean hasHamiltonianPathFrom(int st) { boolean fr[] = new boolean[n + 1]; Arrays.fill(fr, true); return dfsHamiltonian(st, fr, 0); } } // Actually rooted forest class Tree extends Graph { public Tree(int n, int m) { super(n, m); } public int getHeight(int i) { int res = 0; for (Integer j: adj[i]) { res = Math.max(res, getHeight(j) + 1); } return res; } } class FenwickTree { int n; int tree[]; public FenwickTree() { } public FenwickTree(int maxn) { n = maxn; tree = new int[maxn + 10]; } public void add(int i, int x) { while (i <= n) { tree[i] += x; i += i & (-i); } } public void add(int i) { add(i, 1); } public int get(int i) { int res = 0; while (i > 0) { res += tree[i]; i -= i & (-i); } return res; } } IOManager ioManager; Main() { ioManager = new IOManager(); } Main(String file) throws FileNotFoundException { ioManager = new IOManager(file); } long n, s; int m, a[], sum[]; long pow[]; long f[][]; void doi(long n) { m = 0; while (n > 0) { a[m++] = (int) (n % 10L); n /= 10L; } } int getsum(long n) { int res = 0; while (n > 0) { res += (int) (n % 10L); n /= 10L; } return res; } void solve() throws IOException { n = ioManager.nextLong(); s = ioManager.nextLong(); a = new int[100]; pow = new long[100]; pow[0] = 1; for (int i = 1; i < 100; ++i) { pow[i] = pow[i - 1] * 10L; } doi(n); sum = new int[m + 1]; sum[m] = 0; for (int i = m - 1; i >= 0; --i) sum[i] = sum[i + 1] + a[i]; long first = -1; for (long i = s + 1; i <= n; ++i) { if (i - getsum(i) >= s) { first = i; break; } } if (first == -1) { ioManager.writeLine(0); return; } ioManager.writeLine(n - first + 1); } void close() { ioManager.close(); } public static void main(String args[]) throws IOException { Main solver; if (!"true".equals(System.getProperty("ONLINE_JUDGE"))) { solver = new Main("input.txt"); } else { solver = new Main(); } solver.solve(); solver.close(); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.util.StringTokenizer; public class C { static StringTokenizer st; static BufferedReader br; static PrintWriter pw; public static void main(String[] args) throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); long n = nextLong(); long s = nextLong(); long ans = 0; if (s+200 <= n) ans += n - (s+200) + 1; for (long i = s; i < s+200; i++) { if (i <= n && i-sumDigits(i) >= s) { ans++; } } System.out.println(ans); pw.close(); } private static long sumDigits(long n) { long sum = 0; while (n > 0) { sum += n % 10; n /= 10; } return sum; } private static int nextInt() throws IOException { return Integer.parseInt(next()); } private static long nextLong() throws IOException { return Long.parseLong(next()); } private static double nextDouble() throws IOException { return Double.parseDouble(next()); } private static String next() throws IOException { while (st==null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import static java.lang.Math.*; public class TestClass implements Runnable { /*int x,y; public TestClass(int x,int y) { this.x=x; this.y=y; }*/ public static void main(String args[]) { new Thread(null, new TestClass(),"TESTCLASS",1<<18).start(); } public void run() { //Scanner scan=new Scanner(System.in); InputReader hb=new InputReader(System.in); PrintWriter w=new PrintWriter(System.out); long n=hb.nextLong(); long s=hb.nextLong(); long start=0; long end=n; long ans=0; while(start<=end) { long mid=(start+end)/2; if(mid-get(mid)>=s) { end=mid-1; ans=mid; } else { start=mid+1; } } if(ans<1) w.print(0); else w.print(n-ans+1); w.close(); } public long get(long a) { String str = Long.toString(a); int ans = 0; for(char ch : str.toCharArray()) ans += (ch-'0'); return ans; } private void shuffle(int[] arr) { Random ran = new Random(); for (int i = 0; i < arr.length; i++) { int i1 = ran.nextInt(arr.length); int i2 = ran.nextInt(arr.length); int temp = arr[i1]; arr[i1] = arr[i2]; arr[i2] = temp; } } static class DSU { int parent[]; int sizeParent[]; DSU(int n) { parent=new int[n]; sizeParent=new int[n]; Arrays.fill(sizeParent,1); for(int i=0;i<n;i++) parent[i]=i; } int find(int x) { if(x!=parent[x]) parent[x]=find(parent[x]); return parent[x]; } void union(int x,int y) { x=find(x); y=find(y); if(sizeParent[x]>=sizeParent[y]) { if(x!=y) sizeParent[x]+=sizeParent[y]; parent[y]=x; } else { if(x!=y) sizeParent[y]+=sizeParent[x]; parent[x]=y; } } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class Pair implements Comparable<Pair> { int a; int b; String str; public Pair(int a,int b) { this.a=a; this.b=b; str=min(a,b)+" "+max(a,b); } public int compareTo(Pair pair) { if(Integer.compare(a,pair.a)==0) return Integer.compare(b,pair.b); return Integer.compare(a,pair.a); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class Q1 { static ArrayList<Integer> adj[],adj2[]; static int color[],cc; static long mod=1000000007; static TreeSet<Integer> ts[]; static boolean b[],visited[],possible,ans1,ans2; static Stack<Integer> s; static int totalnodes,colored,min,minc; static int c[]; static long sum[]; static HashMap<Integer,Integer> hm; public static void main(String[] args) throws IOException { //Scanner sc=new Scanner(System.in); in=new InputReader(System.in); out=new PrintWriter(System.out); String n1=in.readString(); String s1=in.readString(); long s=Long.parseLong(s1); long n=Long.parseLong(n1); long l=s-1; long r=n+1; HashSet<Long> hset=new HashSet<>(); long last=-1; while(l<r) { long mid=(l+r)/2; long sum=0; if(hset.contains(mid)) break; String d=String.valueOf(mid); for(int i=0;i<d.length();i++) { sum=sum+(d.charAt(i)-'0'); } //debug(sum); hset.add(mid); if(mid-sum>=s) { last=mid; r=mid; } else { l=mid; } } if(last==-1) out.println("0"); else { out.println(n-last+1); } out.close(); } static InputReader in; static PrintWriter out; static void dfs(int i,int parent) { if(color[i]!=cc) ans1= false; for(int j:adj[i]) { if(j!=parent) { dfs(j,i); } } } /*public static void seive(long n){ b = new boolean[(int) (n+1)]; Arrays.fill(b, true); for(int i = 2;i*i<=n;i++){ if(b[i]){ sum[i]=count[i]; // System.out.println(sum[i]+" wf"); for(int p = 2*i;p<=n;p+=i){ b[p] = false; sum[i]+=count[p]; //System.out.println(sum[i]); } } } }*/ static class Pair implements Comparable<Pair> { int i; int j; int index; public Pair(){ } public Pair(int u, int v,int index) { this.i = u; this.j= v; this.index=index; } public int compareTo(Pair other) { return this.i-other.i; } /*public String toString() { return "[u=" + u + ", v=" + v + "]"; }*/ } static class Node2{ Node2 left = null; Node2 right = null; Node2 parent = null; int data; } //binaryStree static class BinarySearchTree{ Node2 root = null; int height = 0; int max = 0; int cnt = 1; ArrayList<Integer> parent = new ArrayList<>(); HashMap<Integer, Integer> hm = new HashMap<>(); public void insert(int x){ Node2 n = new Node2(); n.data = x; if(root==null){ root = n; } else{ Node2 temp = root,temp2 = null; while(temp!=null){ temp2 = temp; if(x>temp.data) temp = temp.right; else temp = temp.left; } if(x>temp2.data) temp2.right = n; else temp2.left = n; n.parent = temp2; parent.add(temp2.data); } } public Node2 getSomething(int x, int y, Node2 n){ if(n.data==x || n.data==y) return n; else if(n.data>x && n.data<y) return n; else if(n.data<x && n.data<y) return getSomething(x,y,n.right); else return getSomething(x,y,n.left); } public Node2 search(int x,Node2 n){ if(x==n.data){ max = Math.max(max, n.data); return n; } if(x>n.data){ max = Math.max(max, n.data); return search(x,n.right); } else{ max = Math.max(max, n.data); return search(x,n.left); } } public int getHeight(Node2 n){ if(n==null) return 0; height = 1+ Math.max(getHeight(n.left), getHeight(n.right)); return height; } } public static void debug(Object... o) { System.out.println(Arrays.deepToString(o)); } public static String rev(String s) { StringBuilder sb=new StringBuilder(s); sb.reverse(); return sb.toString(); } static long lcm(long a, long b) { return a * (b / gcd(a, b)); } static long gcd(long a, long b) { while (b > 0) { long temp = b; b = a % b; // % is remainder a = temp; } return a; } public static long max(long x, long y, long z){ if(x>=y && x>=z) return x; if(y>=x && y>=z) return y; return z; } static int[] sieve(int n,int[] arr) { for(int i=2;i*i<=n;i++) { if(arr[i]==0) { for(int j=i*2;j<=n;j+=i) arr[j]=1; } } return arr; } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int snext() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } int sgn = 1; if (c == '-') { sgn = -1; c = snext(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = snext(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } public String readString() { int c = snext(); while (isSpaceChar(c)) { c = snext(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = snext(); while (isSpaceChar(c)) c = snext(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = snext(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { InputStream inputStream = System.in; // InputStream inputStream = new FileInputStream("dijkstra.in"); OutputStream outputStream = System.out; // OutputStream outputStream = new FileOutputStream("dijkstra.out"); InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Answer solver = new Answer(); solver.solve(in, out); out.close(); } } class Answer { private int sumd(long x) { int sum = 0; while (x != 0) { sum += x % 10; x /= 10; } return sum; } private long bin(long l, long r, long s) { if (l > r) { return -1; } long x = (l + r) >> 1; int sum = sumd(x); if (x - sum < s) { return bin(x + 1, r, s); } long t = bin(l, x - 1, s); if (t != -1) { return t; } return x; } public void solve(InputReader in, PrintWriter out) throws IOException { long n = in.nextLong(); long s = in.nextLong(); long t = bin(1, n, s); if (t == -1) { out.print("0"); } else { long ans = n - t + 1; out.print(ans); } } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Arrays; import java.util.ArrayList; public class C{ static long s; public static void main(String[] args)throws IOException { Reader.init(System.in); long n=Reader.nextLong(); s=Reader.nextLong(); long lo=1,hi=n,mid; while(lo<hi){ mid=(lo+hi)/2; if(diff(mid)>=s) hi=mid; else lo=mid+1; } if(diff(lo)>=s) System.out.println(n-lo+1); else System.out.println(0); } static long diff(long n){ String s=String.valueOf(n); int sum=0; for(int i=0;i<s.length();i++){ sum+=Integer.parseInt(s.valueOf(s.charAt(i))); } return (n-sum); } } /** Class for buffered reading int and double values */ class Reader { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class TestClass { static PrintWriter out = new PrintWriter(System.out); public static void main(String args[] ) throws Exception { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String s[] = in.readLine().split(" "); long n = Long.parseLong(s[0]); long k = Long.parseLong(s[1]); long x = bs(n,k); out.println(n-x+1); out.close(); } public static long bs(long n,long k) { long l=0,h=n; while(l<=h) { long mid = l + (h-l)/2; long x = mid - sum(mid); if(x>=k) { h = mid-1; } else { l = mid+1; } } return l; } public static long sum(long x) { long ans=0; while(x>0) { ans += x%10; x=x/10; } return ans; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = Long.parseLong(in.next()); long s = Long.parseLong(in.next()); if(!check(n, s)){ System.out.println(0); } else { long min = 1; long max = n; while(min != max) { long mid = (min + max) / 2; if(check(mid, s)) { max = mid; } else { min = mid + 1; } } //System.out.println("found: " + min); System.out.println((n - min + 1)); } } public static boolean check(long x, long s) { if(x - sumd(x) < s) { return false; } else { return true; } } public static long sumd(long x) { long sum = 0; while(x != 0) { sum += x % 10; x /= 10; } return sum; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class ReallyBigNumbers1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long s = sc.nextLong(); int r = 0 ; long l = 0L ; long u = n ; if( (l-sumDigits(l)< s ) && (u-sumDigits(u)< s ) ) { System.out.println(0); return ; } long specified = 0L ; while( true ) { long m = (l + u) / 2L ; if( ( m - sumDigits(m) ) >= s && ( (m-1) - sumDigits(m-1) ) < s ) { specified = m ; break ; } if( l > u ) break ; else { if( ( m - sumDigits(m) ) >= s ) u = m-1 ; else l = m+1 ; } } System.out.println( n-specified+1 ); } public static int sumDigits(long n) { char [] a = (n+"").toCharArray(); int sum = 0 ; for(int k = 0 ; k < a.length ; k++) { sum += Integer.parseInt(a[k]+"") ; } return sum ; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { class IO { BufferedReader reader; PrintWriter writer; StringTokenizer tokenizer; IO() { reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(new BufferedOutputStream(System.out)); tokenizer = new StringTokenizer(""); } IO(String file) throws FileNotFoundException { reader = new BufferedReader(new FileReader(file)); writer = new PrintWriter(new BufferedOutputStream(System.out)); tokenizer = new StringTokenizer(""); } String next() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { String line = reader.readLine(); if (line == null) return null; tokenizer = new StringTokenizer(line); } return tokenizer.nextToken(); } public Integer nextInt() throws IOException { return Integer.parseInt(next()); } public Long nextLong() throws IOException { return Long.parseLong(next()); } public Double nextDouble() throws IOException { return Double.parseDouble(next()); } public void write(Object obj) { writer.print(obj.toString()); } public void writeSpace(Object obj) { writer.print(obj.toString() + " "); } public void writeLine(Object obj) { writer.println(obj.toString()); } public void close() { writer.close(); } } IO io; Main() { io = new IO(); } Main(String file) throws FileNotFoundException { io = new IO(file); } void solve() throws IOException { long n = io.nextLong(), s = io.nextLong(); long min = s; while (true) { min++; long sum = 0, tem = min; while (tem > 0) { sum += tem % 10; tem /= 10; } if (min - sum >= s) break; } io.write(Math.max(0, n - min + 1)); } void close() { io.close(); } public static void main(String args[]) throws IOException { // Main solver = new Main("input.txt"); Main solver = new Main(); solver.solve(); solver.close(); } }
logn
817_C. Really Big Numbers
CODEFORCES
/* * Author- Priyam Vora * BTech 2nd Year DAIICT */ import java.awt.Checkbox; import java.awt.Point; import java.io.*; import java.math.*; import java.util.*; import java.util.Map.Entry; import javax.print.attribute.SetOfIntegerSyntax; public class Main{ private static InputStream stream; private static byte[] buf = new byte[1024]; private static int curChar; private static int numChars; private static SpaceCharFilter filter; private static PrintWriter pw; private static long count = 0,mod=1000000007; // private static TreeSet<Integer> ts=new TreeSet[200000]; public final static int INF = (int) 1E9; public static void main(String[] args) { InputReader(System.in); pw = new PrintWriter(System.out); new Thread(null ,new Runnable(){ public void run(){ try{ solve(); pw.close(); } catch(Exception e){ e.printStackTrace(); } } },"1",1<<26).start(); } public static void test(){ int t=nextInt(); while(t-->0){ solve(); } } public static long pow(long n, long p,long mod) { if(p==0) return 1; if(p==1) return n%mod; if(p%2==0){ long temp=pow(n, p/2,mod); return (temp*temp)%mod; }else{ long temp=pow(n,p/2,mod); temp=(temp*temp)%mod; return(temp*n)%mod; } } public static long pow(long n, long p) { if(p==0) return 1; if(p==1) return n; if(p%2==0){ long temp=pow(n, p/2); return (temp*temp); }else{ long temp=pow(n,p/2); temp=(temp*temp); return(temp*n); } } public static void Merge(long a[],int p,int r){ if(p<r){ int q = (p+r)/2; Merge(a,p,q); Merge(a,q+1,r); Merge_Array(a,p,q,r); } } public static void Merge_Array(long a[],int p,int q,int r){ long b[] = new long[q-p+1]; long c[] = new long[r-q]; for(int i=0;i<b.length;i++) b[i] = a[p+i]; for(int i=0;i<c.length;i++) c[i] = a[q+i+1]; int i = 0,j = 0; for(int k=p;k<=r;k++){ if(i==b.length){ a[k] = c[j]; j++; } else if(j==c.length){ a[k] = b[i]; i++; } else if(b[i]<c[j]){ a[k] = b[i]; i++; } else{ a[k] = c[j]; j++; } } } public static long gcd(long x, long y) { if (x == 0) return y; else return gcd( y % x,x); } public static boolean isPrime(int n) { // Corner cases if (n <= 1) return false; if (n <= 3) return true; // This is checked so that we can skip // middle five numbers in below loop if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static LinkedList<Integer> adj[]; static boolean Visited[]; static HashSet<Integer> exc; static long oddsum[]=new long[1000001]; static int co=0,ans=0; static int n,m; static String s[]; static int ind; public static void solve() { long n=nextLong(); long s=nextLong(); long low=1,high=n,ans=-1; while(low<=high){ long mid=(low+high)/2; if(check(mid,s)){ ans=mid; high=mid-1; }else { low=mid+1; } } if(ans==-1) pw.println(0); else pw.println(n-ans+1); } private static boolean check(long mid,long s){ long n=mid; int sum=0; while(mid>0){ sum+=(mid%10); mid/=10; } if(n-sum >=s) return true; return false; } static int[] levl; static int h_max=0; public static void dfs(int curr,int lev){ Visited[curr]=true; levl[curr]=lev; h_max=Math.max(h_max, levl[curr]); for(int x:adj[curr]){ if(!Visited[x]){ dfs(x,lev+1); } } } public static String reverseString(String s) { StringBuilder sb = new StringBuilder(s); sb.reverse(); return (sb.toString()); } private static void BFS(int sou,int dest){ Queue<Integer> q=new LinkedList<Integer>(); q.add(sou); Visited[sou]=true; while(!q.isEmpty()){ int top=q.poll(); for(int i:adj[top]){ //pw.println(i+" "+top); if(!Visited[i]) { q.add(i); } Visited[i]=true; if(i==dest){ pw.println("Yes"); return; } } } pw.println("No"); } private static long ncr(int n,int k){ if (k < 0 || k > n) return 0; if (n-k < k) k = n-k; BigInteger x = BigInteger.ONE; for (int i = 1; i <= k; i++) { x = x.multiply(new BigInteger(""+(n-i+1))); x = x.divide(new BigInteger(""+i)); } return x.longValue(); } private static long fact(long count){ long ans=1; for(int i=1;i<=count;i++){ ans*=i; } return ans; } static int state=1; static long no_exc=0,no_vert=0; static Stack<Integer> st; static HashSet<Integer> inset; private static void topo(int curr){ Visited[curr]=true; inset.add(curr); for(int x:adj[curr]){ if(adj[x].contains(curr) || inset.contains(x)){ state=0; return; } if(state==0) return; } st.push(curr); inset.remove(curr); } static HashSet<Integer> hs; private static void buildgraph(int n){ adj=new LinkedList[n+1]; Visited=new boolean[n+1]; for(int i=0;i<=n;i++){ adj[i]=new LinkedList<Integer>(); } } // To Get Input // Some Buffer Methods public static void sort(long a[]){ Merge(a, 0, a.length-1); } public static void InputReader(InputStream stream1) { stream = stream1; } private static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private static boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } private static int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } private static int nextInt() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } private static String nextToken() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private static String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } private static int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } private static long[][] next2dArray(int n, int m) { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = nextLong(); } } return arr; } private static char[][] nextCharArray(int n,int m){ char [][]c=new char[n][m]; for(int i=0;i<n;i++){ String s=nextLine(); for(int j=0;j<s.length();j++){ c[i][j]=s.charAt(j); } } return c; } private static long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } private static void pArray(int[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static void pArray(long[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static void pArray(boolean[] arr) { for (int i = 0; i < arr.length; i++) { pw.print(arr[i] + " "); } pw.println(); return; } private static boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } private interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class Node{ int to; long dist; Node(int to,long dist){ this.to=to; this.dist=dist; } } /* class Pair implements Comparable<Pair>{ int ele,ind; Pair(int ele,int ind){ this.ele=ele; this.ind=ind; } @Override public int compareTo(Pair o) { return ele-o.ele; } public int hashCode() { //int hu = (int) (x ^ (x >>> 32)); //int hv = (int) (y ^ (y >>> 32)); //int hw = (int) (mass ^ (mass >>> 32)); //return 31 * hu + hv ; return 0; } public boolean equals(Object o) { Pair other = (Pair) o; // return x == other.x && y == other.y; return false; } } */class Dsu{ private int rank[], parent[] ,n; private static int[] parent1; Dsu(int size){ this.n=size+1; rank=new int[n]; //parent=new int[n]; parent=new int[n]; makeSet(); } void makeSet(){ for(int i=0;i<n;i++){ parent[i]=i; } } int find(int x){ if(parent[x]!=x){ parent[x]=find(parent[x]); } return parent[x]; } boolean union(int x,int y){ int xRoot=find(x); int yRoot=find(y); if(xRoot==yRoot) return false; if(rank[xRoot]<rank[yRoot]){ parent[xRoot]=yRoot; }else if(rank[yRoot]<rank[xRoot]){ parent[yRoot]=xRoot; }else{ parent[yRoot]=xRoot; rank[xRoot]++; } return true; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Main { static InputReader in = new InputReader(System.in); static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { long n = in.nextLong(); long s = in.nextLong(); if(diff(n) < s) { System.out.println(0); out.close(); return; } long lo = 1; long hi = n; while(lo < hi) { long mid = lo + (hi - lo) / 2; if(diff(mid) >= s) hi = mid; else lo = mid + 1; } System.out.println(n - lo + 1); out.close(); } static long diff(long n) { char[] ca = (n + "").toCharArray(); int sum = 0; for(char c : ca) sum += (c - '0'); return n - sum; } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class c { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long n = scan.nextLong(); long s = scan.nextLong(); scan.close(); long start = s - s % 10; while (start <= n && !isBig(start, s)) { start += 10; } if (start > n) { System.out.println(0); } else { System.out.println(n - start + 1); } } private static boolean isBig(long a, long s) { char[] digits = ("" + a).toCharArray(); int counter = 0; for (int i = 0; i < digits.length; i++) { counter += digits[i] - '0'; } return a - counter >= s; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.PrintStream; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Wolfgang Beyer */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { public void solve(int testNumber, InputReader in, PrintWriter out) { long n = in.nextLong(); long s = in.nextLong(); if (n - digitSum(n) < s) { out.println(0); return; } long left = 0; long right = n; while (left < right) { long mid = left + (right - left) / 2; if (mid - digitSum(mid) >= s) { // if condition(mid) == true right = mid; } else { left = mid + 1; } } out.println(n - left + 1); } long digitSum(long a) { long result = 0; while (a > 0) { result += a % 10; a /= 10; } return result; } } static class InputReader { private static BufferedReader in; private static StringTokenizer tok; public InputReader(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); } public long nextLong() { return Long.parseLong(next()); } public String next() { try { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); //tok = new StringTokenizer(in.readLine(), ", \t\n\r\f"); //adds commas as delimeter } } catch (IOException ex) { System.err.println("An IOException was caught :" + ex.getMessage()); } return tok.nextToken(); } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long n = scanner.nextLong(); long s = scanner.nextLong(); long myLong = s; long count =0; while(true){ if(myLong>n){ break; } char[] num = (""+myLong).toCharArray(); int sum = 0; for (int j = 0; j < num.length; j++) sum += num[j] - '0'; if(myLong- sum>=s){ //count++; break; } myLong++; } System.out.println(Math.max(n-myLong+1,0)); scanner.close(); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class ReallyBigNumbers { public static void main(String[] args) { @SuppressWarnings("resource") Scanner sc = new Scanner(System.in); long n = sc.nextLong(); // max long s = sc.nextLong(); // differential long bigNums = 0; long inARow = 0; for (long i = s; i <= n; i++) { if (inARow == 9) { bigNums += (n - i+1); break; } else { if (i >= s + digitSum(i)) { bigNums++; inARow++; } else { inARow = 0; } } } System.out.println(bigNums); } public static long digitSum(long a) { long sum = a % 10; if (9 < a) { a /= 10; sum += digitSum(a); } return sum; } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Arrays; import java.util.Scanner; public class P817C { public static void main(String[] args) { Scanner scan = new Scanner(System.in); long n = scan.nextLong(); long s = scan.nextLong(); long ans = 0; if (s > n) { System.out.println(0); return; } if (n > s+200) { ans += n-(s+200); n = s+200; } for (long i = s; i <= n; i++) { char[] num = (""+i).toCharArray(); int sum = 0; for (int j = 0; j < num.length; j++) sum += num[j] - '0'; if (i - sum >= s) ans++; } System.out.println(ans); } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Scanner; public class C { private static final String REGEX = " "; private static final Boolean DEBUG = false; private static final String FILE_NAME = "input.txt"; public static void main(String[] args) throws IOException { if (DEBUG) { generate(); } Solver solver = new Solver(); solver.readData(); solver.solveAndPrint(); } private static void generate() throws IOException { // FileWriter writer = new FileWriter("input.txt"); // writer.close(); } private static class Solver { long n, s; void readData() throws IOException { InputStream in = DEBUG ? new FileInputStream(FILE_NAME) : System.in; Scanner scanner = new Scanner(in); n = scanner.nextLong(); s = scanner.nextLong(); scanner.close(); } void solveAndPrint() { long cur = s + 1; long sum = getSum(cur); long res = 0; while (cur <= n) { if (cur - sum >= s) { System.out.println(n - cur + 1); return; } cur++; if (cur % 10 != 0) { sum++; } else { sum = getSum(cur); } } System.out.println(0); } long getSum(long cur) { long res = 0; while (cur > 0) { res += cur % 10; cur /= 10; } return res; } @SuppressWarnings("SameParameterValue") int[] splitInteger(String string, int n) { final String[] split = string.split(REGEX, n); int[] result = new int[split.length]; for (int i = 0; i < n; ++i) { result[i] = Integer.parseInt(split[i]); } return result; } public int[] splitInteger(String string) { return splitInteger(string, 0); } @SuppressWarnings("SameParameterValue") long[] splitLong(String string, int n) { final String[] split = string.split(REGEX, n); long[] result = new long[split.length]; for (int i = 0; i < n; ++i) { result[i] = Long.parseLong(split[i]); } return result; } public long[] splitLong(String string) { return splitLong(string, 0); } @SuppressWarnings("SameParameterValue") double[] splitDouble(String string, int n) { final String[] split = string.split(REGEX, n); double[] result = new double[split.length]; for (int i = 0; i < n; ++i) { result[i] = Double.parseDouble(split[i]); } return result; } public double[] splitDouble(String string) { return splitDouble(string, 0); } @SuppressWarnings("SameParameterValue") String[] splitString(String string, int n) { return string.split(REGEX, n); } public String[] splitString(String string) { return splitString(string, 0); } public int max(int a, int b) { return Math.max(a, b); } public long max(long a, long b) { return Math.max(a, b); } public int min(int a, int b) { return Math.min(a, b); } public long min(long a, long b) { return Math.min(a, b); } public double max(double a, double b) { return Math.max(a, b); } public double min(double a, double b) { return Math.min(a, b); } private final static int MOD = 1000000009; int multMod(int a, int b) { return ((a % MOD) * (b % MOD)) % MOD; } int sumMod(int a, int b) { return ((a % MOD) + (b % MOD)) % MOD; } long multMod(long a, long b) { return ((a % MOD) * (b % MOD)) % MOD; } long sumMod(long a, long b) { return ((a % MOD) + (b % MOD)) % MOD; } } }
logn
817_C. Really Big Numbers
CODEFORCES
import java.util.Scanner; public class Main { final static long Mod = 1000000009; static long n, m, k, t, l, u, ans; static Scanner cin = new Scanner(System.in); static long multi_mod(long base, long cur) { long res = 1; while(cur > 0) { if(cur % 2 == 1) res = (res * base) % Mod; cur >>= 1; base = (base * base) % Mod; } return res; } public static void main(String[] args) { n = cin.nextLong(); m = cin.nextLong(); k = cin.nextLong(); l = (k - 1)*(n / k) + n % k; if(m <= l) { System.out.println(m); } else { t = n / k; u = m - l; ans = (0 + (t - u) * (k - 1) + n % k) % Mod; ans = (ans + ((k)*((multi_mod(2, u + 1) - 2 + Mod) % Mod)) % Mod) % Mod; System.out.println(ans); } } }
logn
338_A. Quiz
CODEFORCES
import java.math.BigInteger; import java.util.Scanner; public class C { public static void main(String[] args) { Scanner sc=new Scanner(System.in); long s=0,mod=1000000009; int n=sc.nextInt(),m=sc.nextInt(),k=sc.nextInt(),c=n/k; if(m<=c*(k-1)+(n%k))System.out.println(m); else { int a=m-c*(k-1)-(n%k); long l=0,pase=0; //System.out.println(a); long pot=BigInteger.valueOf(2).modPow(BigInteger.valueOf(a), BigInteger.valueOf(mod)).longValue(); pot=(2*(pot-1))%mod; /*System.out.println(pot); System.out.println(k);*/ System.out.println(((pot*k)%mod+(m-a*k))%mod); } } }
logn
338_A. Quiz
CODEFORCES
import java.io.*; import java.util.Arrays; import java.util.Scanner; import java.util.StringTokenizer; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); new TaskC().solve(in, out); out.close(); } } class TaskC { static final long mod=1000000009; public void solve(InputReader in,PrintWriter out) { int n=in.nextInt(); int m=in.nextInt(); int k=in.nextInt(); int l=0,r=m/k; while(l<r) { int mid=(l+r)>>>1; if(good(n,m,k,mid)){ r=mid; } else{ l=mid+1; } } Mat u=new Mat(); u.set(k,1,0,0); Mat v=new Mat(); v.set(2,0,k,1); u=u.mul(v.powMat(l)); out.println((u.e[0][0]-k+(m-l*k)+mod)%mod); } private boolean good(int n, int m, int k, int mid) { n-=mid*k; m-=mid*k; int ans=n/k*(k-1)+n%k; if(ans>=m) return true; return false; } private static class Mat { long[][] e=new long[2][2]; Mat mul(Mat u) { Mat s=new Mat(); s.set(0,0,0,0); for(int i=0;i<2;i++) { for(int j=0;j<2;j++) { for(int k=0;k<2;k++){ s.e[i][j]=(s.e[i][j]+e[i][k]*u.e[k][j])%mod; } } } return s; } Mat powMat(int k) { Mat u=this; Mat s=new Mat(); s.set(1,0,0,1); while(k!=0) { if(k%2==1) { s=s.mul(u); } u=u.mul(u); k/=2; } return s; } void set(long i, long j, long k, long l) { e[0][0]=i; e[0][1]=j; e[1][0]=k; e[1][1]=l; } } } class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } }
logn
338_A. Quiz
CODEFORCES