exec_outcome
stringclasses
1 value
code_uid
stringlengths
32
32
file_name
stringclasses
111 values
prob_desc_created_at
stringlengths
10
10
prob_desc_description
stringlengths
63
3.8k
prob_desc_memory_limit
stringclasses
18 values
source_code
stringlengths
117
65.5k
lang_cluster
stringclasses
1 value
prob_desc_sample_inputs
stringlengths
2
802
prob_desc_time_limit
stringclasses
27 values
prob_desc_sample_outputs
stringlengths
2
796
prob_desc_notes
stringlengths
4
3k
lang
stringclasses
5 values
prob_desc_input_from
stringclasses
3 values
tags
listlengths
0
11
src_uid
stringlengths
32
32
prob_desc_input_spec
stringlengths
28
2.37k
difficulty
int64
-1
3.5k
prob_desc_output_spec
stringlengths
17
1.47k
prob_desc_output_to
stringclasses
3 values
hidden_unit_tests
stringclasses
1 value
PASSED
8b5b6e48ee82e3b224a9c657374baf69
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.util.Arrays; import java.util.HashSet; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; import java.util.Random; import java.io.FileWriter; import java.io.PrintWriter; /* Solution Created: 22:03:45 20/02/2022 Custom Competitive programming helper. */ public class Main { static long mod = 1000000007; static int n,p; static HashSet<Integer> identities; static boolean used(int i) { while(true) { if(identities.contains(i)) return true; if(i==0) return false; if(i%4 == 0) i/=4; else { if(i%2==1) { i = (i-1)/2; }else return false; } } } static int pow(int i) { int ans = 0, cur = 1; while(cur<=i) { cur*=2; ans++; } return ans-1; } public static void solve() { n = in.nextInt(); p = in.nextInt(); int[] a = in.na(n); identities = new HashSet<>(); Arrays.sort(a); long[] dp = new long[p+3]; for(int i : a) { if(used(i)) continue; int x = pow(i); if(x<=p) dp[x]++; identities.add(i); } long ans = dp[0] + (dp[1]+=dp[0]); for(int i = 2; i<p; i++) { dp[i] += (dp[i-1]+dp[i-2])%mod; ans = (ans + dp[i]) % mod; } out.println(ans); } public static void main(String[] args) { in = new Reader(); out = new Writer(); int t = 1; while(t-->0) solve(); out.exit(); } static Reader in; static Writer out; static class Reader { static BufferedReader br; static StringTokenizer st; public Reader() { this.br = new BufferedReader(new InputStreamReader(System.in)); } public Reader(String f){ try { this.br = new BufferedReader(new FileReader(f)); } catch (IOException e) { e.printStackTrace(); } } public int[] na(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public double[] nd(int n) { double[] a = new double[n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } public long[] nl(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } public char[] nca() { return next().toCharArray(); } public String[] ns(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } public int nextInt() { ensureNext(); return Integer.parseInt(st.nextToken()); } public double nextDouble() { ensureNext(); return Double.parseDouble(st.nextToken()); } public Long nextLong() { ensureNext(); return Long.parseLong(st.nextToken()); } public String next() { ensureNext(); return st.nextToken(); } public String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); return null; } } private void ensureNext() { if (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } } } static class Util{ private static Random random = new Random(); static long[] fact; public static void initFactorial(int n, long mod) { fact = new long[n+1]; fact[0] = 1; for (int i = 1; i < n+1; i++) fact[i] = (fact[i - 1] * i) % mod; } public static long modInverse(long a, long MOD) { long[] gcdE = gcdExtended(a, MOD); if (gcdE[0] != 1) return -1; // Inverted doesn't exist long x = gcdE[1]; return (x % MOD + MOD) % MOD; } public static long[] gcdExtended(long p, long q) { if (q == 0) return new long[] { p, 1, 0 }; long[] vals = gcdExtended(q, p % q); long tmp = vals[2]; vals[2] = vals[1] - (p / q) * vals[2]; vals[1] = tmp; return vals; } public static long nCr(int n, int r, long MOD) { if (r == 0) return 1; return (fact[n] * modInverse(fact[r], MOD) % MOD * modInverse(fact[n - r], MOD) % MOD) % MOD; } public static long nCr(int n, int r) { return (fact[n]/fact[r])/fact[n-r]; } public static long nPr(int n, int r, long MOD) { if (r == 0) return 1; return (fact[n] * modInverse(fact[n - r], MOD) % MOD) % MOD; } public static long nPr(int n, int r) { return fact[n]/fact[n-r]; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; 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; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } static long pow(long x, long pow, long mod){ long res = 1; x = x % mod; if (x == 0) return 0; while (pow > 0){ if ((pow & 1) != 0) res = (res * x) % mod; pow >>= 1; x = (x * x) % mod; } return res; } public static int gcd(int a, int b) { int tmp = 0; while(b != 0) { tmp = b; b = a%b; a = tmp; } return a; } public static long gcd(long a, long b) { long tmp = 0; while(b != 0) { tmp = b; b = a%b; a = tmp; } return a; } public static int random(int min, int max) { return random.nextInt(max-min+1)+min; } public static void dbg(Object... o) { System.out.println(Arrays.deepToString(o)); } public static void reverse(int[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { int tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(int[] s) { reverse(s, 0, s.length-1); } public static void reverse(long[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { long tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(long[] s) { reverse(s, 0, s.length-1); } public static void reverse(float[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { float tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(float[] s) { reverse(s, 0, s.length-1); } public static void reverse(double[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { double tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(double[] s) { reverse(s, 0, s.length-1); } public static void reverse(char[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { char tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static void reverse(char[] s) { reverse(s, 0, s.length-1); } public static <T> void reverse(T[] s, int l , int r) { for(int i = l; i<=(l+r)/2; i++) { T tmp = s[i]; s[i] = s[r+l-i]; s[r+l-i] = tmp; } } public static <T> void reverse(T[] s) { reverse(s, 0, s.length-1); } public static void shuffle(int[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); int t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(long[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); long t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(float[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); float t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(double[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); double t = s[i]; s[i] = s[j]; s[j] = t; } } public static void shuffle(char[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); char t = s[i]; s[i] = s[j]; s[j] = t; } } public static <T> void shuffle(T[] s) { for (int i = 0; i < s.length; ++i) { int j = random.nextInt(i + 1); T t = s[i]; s[i] = s[j]; s[j] = t; } } public static void sortArray(int[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(long[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(float[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(double[] a) { shuffle(a); Arrays.sort(a); } public static void sortArray(char[] a) { shuffle(a); Arrays.sort(a); } public static <T extends Comparable<T>> void sortArray(T[] a) { Arrays.sort(a); } public static int[][] rotate90(int[][] a){ int n = a.length, m = a[0].length; int[][] ans = new int[m][n]; for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) ans[m-j-1][i] = a[i][j]; return ans; } public static char[][] rotate90(char[][] a){ int n = a.length, m = a[0].length; char[][] ans = new char[m][n]; for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) ans[m-j-1][i] = a[i][j]; return ans; } } static class Writer { private PrintWriter pw; public Writer(){ pw = new PrintWriter(System.out); } public Writer(String f){ try { pw = new PrintWriter(new FileWriter(f)); } catch (IOException e) { e.printStackTrace(); } } public void yesNo(boolean condition) { println(condition?"YES":"NO"); } public void printArray(int[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); } public void printlnArray(int[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); pw.println(); } public void printArray(long[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); } public void printlnArray(long[] a) { for(int i = 0; i<a.length; i++) print(a[i]+" "); pw.println(); } public void print(Object o) { pw.print(o.toString()); } public void println(Object o) { pw.println(o.toString()); } public void println() { pw.println(); } public void flush() { pw.flush(); } public void exit() { pw.close(); } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
5f6b36ae137aee7253615104add3f39a
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.*; import java.util.*; public class new1{ static long mod = 1000000007; static long count = 0; public static long gcd(long a, long b) { if (a == 0) return b; return gcd(b%a, a); } public static void main(String[] args) throws IOException{ BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); FastReader s = new FastReader(); int t = 1;//s.nextInt(); for(int z = 0; z < t; z++) { int n = s.nextInt(); int n1 = s.nextInt(); long[] dp = new long[n1 + 1]; dp[n1] = 1; dp[n1 - 1] = 2; for(int i = n1 - 2; i >= 1; i--) { long aa = (dp[i + 1] + dp[i + 2] + 1) % mod; dp[i] = aa; } // System.out.println(Arrays.toString(dp)); ArrayList<Long> aList = new ArrayList<Long>(); Set<Long> st = new HashSet<Long>(); for(int i = 0; i < n;i ++) { aList.add(s.nextLong()); } aList.sort(null); long ans = 0; for(int i = 0; i < n; i++) { long val = aList.get(i); long val1 = val; boolean pos = false; while(true) { //System.out.println(val); if(val % 4 == 0) val = val / 4; else if((val - 1) % 2 == 0) val = (val - 1) / 2; else break; if(val <= 0) break; if(st.contains(val)) pos = true; } if(!pos) { st.add(val1); int nb = 0; long n2 = val1; while(n2 > 0) { n2 = n2 / 2; nb++; } if(nb <= n1) ans = (ans + dp[nb]) % mod; } } System.out.println(ans); //System.out.println(count); } output.flush(); } } 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(); } public 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; }}
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
038345bb7c2918c51033818b302ac337
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
/** * Created by Himanshu **/ import java.util.*; import java.io.*; import java.math.*; public class D1635 { public static void main(String[] args) throws IOException { PrintWriter out = new PrintWriter(System.out); Reader s = new Reader(); int n = s.i(), p = s.i(); int[] arr = s.arr(n); Arrays.sort(arr); Set<Integer> set = new HashSet<>(); long[] ans = new long[p]; long mod = (long) (1e9 + 7); for (int i = 0; i < n; i++) { int curr = arr[i]; int log = (int) (Math.log(curr) / Math.log(2)); if (i == 0 && log < p) { ans[log]++; } else if (check(curr, arr[0], set) && log < p) { ans[log]++; } set.add(curr); } for (int i = 1; i < p; i++) { ans[i] = (ans[i] + ans[i - 1]) % mod; if (i >= 2) { ans[i] = (ans[i] + ans[i - 2]) % mod; } } long sum = 0L; for (long x : ans) { sum = (sum + x) % mod; } out.println(sum); out.flush(); } private static boolean check(int curr, int min, Set<Integer> set) { if (curr < min) return true; if (set.contains(curr)) return false; if (curr % 4 == 0) return check(curr / 4, min, set); if (curr % 2 == 1) return check(curr / 2, min, set); return true; } public static void shuffle(long[] arr) { int n = arr.length; Random rand = new Random(); for (int i = 0; i < n; i++) { long temp = arr[i]; int randomPos = i + rand.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = temp; } } private static long phi(long n) { long result = n; for (long i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; result -= result / i; } } if (n > 1) result -= result / n; return result; } private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } public static long nCr(long[] fact, long[] inv, int n, int r, long mod) { if (n < r) return 0; return ((fact[n] * inv[n - r]) % mod * inv[r]) % mod; } private static void factorials(long[] fact, long[] inv, long mod, int n) { fact[0] = 1; inv[0] = 1; for (int i = 1; i <= n; ++i) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = power(fact[i], mod - 2, mod); } } private static long power(long a, long n, long p) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a) % p; n /= 2; } else { result = (result * a) % p; n--; } } return result; } private static long power(long a, long n) { long result = 1; while (n > 0) { if (n % 2 == 0) { a = (a * a); n /= 2; } else { result = (result * a); n--; } } return result; } private static long query(long[] tree, int in, int start, int end, int l, int r) { if (start >= l && r >= end) return tree[in]; if (end < l || start > r) return 0; int mid = (start + end) / 2; long x = query(tree, 2 * in, start, mid, l, r); long y = query(tree, 2 * in + 1, mid + 1, end, l, r); return x + y; } private static void update(int[] arr, long[] tree, int in, int start, int end, int idx, int val) { if (start == end) { tree[in] = val; arr[idx] = val; return; } int mid = (start + end) / 2; if (idx > mid) update(arr, tree, 2 * in + 1, mid + 1, end, idx, val); else update(arr, tree, 2 * in, start, mid, idx, val); tree[in] = tree[2 * in] + tree[2 * in + 1]; } private static void build(int[] arr, long[] tree, int in, int start, int end) { if (start == end) { tree[in] = arr[start]; return; } int mid = (start + end) / 2; build(arr, tree, 2 * in, start, mid); build(arr, tree, 2 * in + 1, mid + 1, end); tree[in] = (tree[2 * in + 1] + tree[2 * in]); } 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 long[] arrLong(int n) { long[] ret = new long[n]; for (int i = 0; i < n; i++) { ret[i] = l(); } return ret; } } // static final int ALPHABET_SIZE = 26; // static class TrieNode { // D1629.TrieNode[] children = new D1629.TrieNode[ALPHABET_SIZE]; // boolean isLeaf; // boolean isPalindrome; // public TrieNode() { // isLeaf = false; // isPalindrome = false; // for (int i = 0; i < ALPHABET_SIZE; i++) // children[i] = null; // } // } // static class pairLong implements Comparator<pairLong> { // long first, second; // // pairLong() { // } // // pairLong(long first, long second) { // this.first = first; // this.second = second; // } // // @Override // public int compare(pairLong p1, pairLong p2) { // if (p1.first == p2.first) { // if(p1.second > p2.second) return 1; // else return -1; // } // if(p1.first > p2.first) return 1; // else return -1; // } // } // static class pair implements Comparator<pair> { // int first, second; // // pair() { // } // // pair(int first, int second) { // this.first = first; // this.second = second; // } // // @Override // public int compare(pair p1, pair p2) { // if (p1.first == p2.first) return p1.second - p2.second; // return p1.first - p2.first; // } // } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
88fb738c2612ef937fa5a95b6d09bff2
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
//some updates in import stuff import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; //key points learned //max space ever that could be alloted in a program to pass in cf //int[][] prefixSum = new int[201][200_005]; -> not a single array more!!! //never allocate memory again again to such bigg array, it will give memory exceeded for sure //believe in your fucking solution and keep improving it!!! (sometimes) public class Main{ static int mod = (int) (Math.pow(10, 9)+7); static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 }; static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 }; static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 }; static final double eps = 1e-10; static List<Integer> primeNumbers = new ArrayList<>(); public static void main(String[] args) { MyScanner sc = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); //code below int n = sc.nextInt(); int p = sc.nextInt(); int[] arr = new int[n]; Set<Integer> set = new HashSet<>(); for(int i = 0; i < n; i++){ arr[i] = sc.nextInt(); //simple input } sort(arr); //sorting for easier //now ArrayList<Integer> imp = new ArrayList<>(); for(int i = 0; i < n; i++){ int curr = arr[i]; //thinking about it for sure now! boolean flag = true; if(set.contains(curr)) flag = false; while(curr != 0 && flag){ if(curr % 2 == 1){ curr = (curr - 1)/2; if(set.contains(curr)) flag = false; }else{ if(curr % 4 == 0){ curr = curr / 4; if(set.contains(curr)) flag = false; }else{ break; } } } //now after this is simple if(flag){ imp.add(arr[i]); } set.add(arr[i]); } //now after this process is done, find the number of bits //and place in in the p array int[] ways = new int[p]; for(int num : imp){ //count the number of bits in a given number int cnt = 0; while(num != 0){ num = num >> 1; cnt++; } if(cnt - 1 < p) ways[cnt - 1]++; } int ans = 0; for(int i= 0; i < p; i++){ ans = (ans + ways[i])%mod; if(i + 1 < p){ ways[i + 1] = (ways[i + 1] + ways[i])%mod; } if(i + 2 < p){ ways[i + 2] = (ways[i + 2] + ways[i])%mod; } } System.out.println(ans); out.close(); } //new stuff to learn (whenever this is need for them, then only) //Lazy Segment Trees //Persistent Segment Trees //Square Root Decomposition //Geometry & Convex Hull //High Level DP -- yk yk //String Matching Algorithms //Heavy light Decomposition //Updation Required //Fenwick Tree - both are done (sum) //Segment Tree - both are done (min, max, sum) //-----CURRENTLY PRESENT-------// //Graph //DSU //powerMODe //power //Segment Tree (work on this one) //Prime Sieve //Count Divisors //Next Permutation //Get NCR //isVowel //Sort (int) //Sort (long) //Binomial Coefficient //Pair //Triplet //lcm (int & long) //gcd (int & long) //gcd (for binomial coefficient) //swap (int & char) //reverse //primeExponentCounts //Fast input and output //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //------------------------------------------------------------------- //GRAPH (basic structure) public static class Graph{ public int V; public ArrayList<ArrayList<Integer>> edges; //2 -> [0,1,2] (current) Graph(int V){ this.V = V; edges = new ArrayList<>(V+1); for(int i= 0; i <= V; i++){ edges.add(new ArrayList<>()); } } public void addEdge(int from , int to){ edges.get(from).add(to); edges.get(to).add(from); } } //DSU (path and rank optimised) public static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; Arrays.fill(rank, 1); Arrays.fill(parent,-1); this.n = n; } public int find(int curr){ if(parent[curr] == -1) return curr; //path compression optimisation return parent[curr] = find(parent[curr]); } public void union(int a, int b){ int s1 = find(a); int s2 = find(b); if(s1 != s2){ //union by size if(rank[s1] < rank[s2]){ parent[s1] = s2; rank[s2] += rank[s1]; }else{ parent[s2] = s1; rank[s1] += rank[s2]; } } } } //with mod public static long powerMOD(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ x %= mod; res %= mod; res = (res * x)%mod; } // y must be even now y = y >> 1; // y = y/2 x%= mod; x = (x * x)%mod; // Change x to x^2 } return res%mod; } //without mod public static long power(long x, long y) { long res = 1L; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0){ res = (res * x); } // y must be even now y = y >> 1; // y = y/ x = (x * x); } return res; } public static class segmentTree{ //so let's make a constructor function for this bad boi for sure!!! public long[] arr; public long[] tree; //COMPLEXITY (normal segment tree, stuff) //build -> O(n) //query -> O(logn) //update -> O(logn) //update-range -> O(n) (worst case) //simple iteration and stuff for sure public segmentTree(long[] arr){ int n = arr.length; this.arr = new long[n]; for(int i= 0; i < n; i++){ this.arr[i] = arr[i]; } tree = new long[4*n + 1]; } //pretty basic idea if you read the code once //first make child node once //then form the parent node using them public void buildTree(int s, int e, int index){ if(s == e){ tree[index] = arr[s]; return; } //recursive case int mid = (s + e)/2; buildTree(s, mid, 2 * index); buildTree(mid + 1, e, 2*index + 1); //the condition we want from children be like this tree[index] = min(tree[2 * index], tree[2 * index + 1]); return; } //definitely index based 0 query!!! //only int index = 1!! //baaki everything is simple as fuck public long query(int s, int e, int qs , int qe, int index){ //complete overlap if(s >= qs && e <= qe){ return tree[index]; } //no overlap if(qe < s || qs > e){ return Long.MAX_VALUE; } //partial overlap int mid = (s + e)/2; long left = query( s, mid , qs, qe, 2*index); long right = query( mid + 1, e, qs, qe, 2*index + 1); return min(left, right); } //updates are pretty cool //point update and range update!!! public void updateNode(int s, int e, int i, long increment, int index){ //case where I is out of bounds if(i < s || i > e){ return; } if(s == e){ //making increment tree[index] += increment; return; } //otherwise int mid = (s + e)/2; updateNode(s, mid, i, increment, 2 * index); updateNode(mid + 1, e, i, increment, 2 * index + 1); tree[index] = min(tree[2 * index], tree[2 * index + 1]); return; } //gonna do range updates for sure now!! //let's do this bois!!! (solve this problem for sure) public void updateRange(int s, int e, int l, int r, long increment, int index){ //out of bounds if(l > e || r < s){ return; } //leaf node if(s == e){ tree[index] += increment; } //recursive case int mid = (s + e)/2; updateRange(s, mid, l, r, increment, 2 * index); updateRange(mid + 1, e, l, r, increment, 2 * index); tree[index] = min(tree[2 * index], tree[2 * index + 1]); } } //prime sieve public static void primeSieve(int n){ BitSet bitset = new BitSet(n+1); for(long i = 0; i < n ; i++){ if (i == 0 || i == 1) { bitset.set((int) i); continue; } if(bitset.get((int) i)) continue; primeNumbers.add((int)i); for(long j = i; j <= n ; j+= i) bitset.set((int)j); } } //number of divisors public static int countDivisors(long number){ if(number == 1) return 1; List<Integer> primeFactors = new ArrayList<>(); int index = 0; long curr = primeNumbers.get(index); while(curr * curr <= number){ while(number % curr == 0){ number = number/curr; primeFactors.add((int) curr); } index++; curr = primeNumbers.get(index); } if(number != 1) primeFactors.add((int) number); int current = primeFactors.get(0); int totalDivisors = 1; int currentCount = 2; for (int i = 1; i < primeFactors.size(); i++) { if (primeFactors.get(i) == current) { currentCount++; } else { totalDivisors *= currentCount; currentCount = 2; current = primeFactors.get(i); } } totalDivisors *= currentCount; return totalDivisors; } //primeExponentCounts public static int primeExponentsCount(int n) { if (n <= 1) return 0; int sqrt = (int) Math.sqrt(n); int remainingNumber = n; int result = 0; for (int i = 2; i <= sqrt; i++) { while (remainingNumber % i == 0) { result++; remainingNumber /= i; } } //in case of prime numbers this would happen if (remainingNumber > 1) { result++; } return result; } //now adding next permutation function to java hehe public static boolean next_permutation(int[] p) { for (int a = p.length - 2; a >= 0; --a) if (p[a] < p[a + 1]) for (int b = p.length - 1;; --b) if (p[b] > p[a]) { int t = p[a]; p[a] = p[b]; p[b] = t; for (++a, b = p.length - 1; a < b; ++a, --b) { t = p[a]; p[a] = p[b]; p[b] = t; } return true; } return false; } //finding the value of NCR in O(RlogN) time and O(1) space public static long getNcR(int n, int r) { long p = 1, k = 1; if (n - r < r) r = n - r; if (r != 0) { while (r > 0) { p *= n; k *= r; long m = __gcd(p, k); p /= m; k /= m; n--; r--; } } else { p = 1; } return p; } //is vowel function public static boolean isVowel(char c) { return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U'); } //to sort the array with better method public static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } //sort long public static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } //for calculating binomialCoeff public static int binomialCoeff(int n, int k) { int C[] = new int[k + 1]; // nC0 is 1 C[0] = 1; for (int i = 1; i <= n; i++) { // Compute next row of pascal // triangle using the previous row for (int j = Math.min(i, k); j > 0; j--) C[j] = C[j] + C[j - 1]; } return C[k]; } //Pair with int int public static class Pair{ public int a; public int b; public int hashCode; Pair(int a , int b){ this.a = a; this.b = b; this.hashCode = Objects.hash(a, b); } @Override public String toString(){ return a + " -> " + b; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair that = (Pair) o; return a == that.a && b == that.b; } @Override public int hashCode() { return this.hashCode; } } //Triplet with int int int public static class Triplet{ public int a; public int b; public int c; Triplet(int a , int b, int c){ this.a = a; this.b = b; this.c = c; } @Override public String toString(){ return a + " -> " + b; } } //Shortcut function public static long lcm(long a , long b){ return a * (b/gcd(a,b)); } //let's make one for calculating lcm basically public static int lcm(int a , int b){ return (a * b)/gcd(a,b); } //int version for gcd public static int gcd(int a, int b){ if(b == 0) return a; return gcd(b , a%b); } //long version for gcd public static long gcd(long a, long b){ if(b == 0) return a; return gcd(b , a%b); } //for ncr calculator(ignore this code) public static long __gcd(long n1, long n2) { long gcd = 1; for (int i = 1; i <= n1 && i <= n2; ++i) { // Checks if i is factor of both integers if (n1 % i == 0 && n2 % i == 0) { gcd = i; } } return gcd; } //swapping two elements in an array public static void swap(int[] arr, int left , int right){ int temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //for char array public static void swap(char[] arr, int left , int right){ char temp = arr[left]; arr[left] = arr[right]; arr[right] = temp; } //reversing an array public static void reverse(int[] arr){ int left = 0; int right = arr.length-1; while(left <= right){ swap(arr, left,right); left++; right--; } } public static long expo(long a, long b, long mod) { long res = 1; while (b > 0) { if ((b & 1) == 1L) res = (res * a) % mod; //think about this one for a second a = (a * a) % mod; b = b >> 1; } return res; } //SOME EXTRA DOPE FUNCTIONS public static long mminvprime(long a, long b) { return expo(a, b - 2, b); } public static long mod_add(long a, long b, long m) { a = a % m; b = b % m; return (((a + b) % m) + m) % m; } public static long mod_sub(long a, long b, long m) { a = a % m; b = b % m; return (((a - b) % m) + m) % m; } public static long mod_mul(long a, long b, long m) { a = a % m; b = b % m; return (((a * b) % m) + m) % m; } public static long mod_div(long a, long b, long m) { a = a % m; b = b % m; return (mod_mul(a, mminvprime(b, m), m) + m) % m; } //O(n) every single time remember that public static long nCr(long N, long K , long mod){ long upper = 1L; long lower = 1L; long lowerr = 1L; for(long i = 1; i <= N; i++){ upper = mod_mul(upper, i, mod); } for(long i = 1; i <= K; i++){ lower = mod_mul(lower, i, mod); } for(long i = 1; i <= (N - K); i++){ lowerr = mod_mul(lowerr, i, mod); } // out.println(upper + " " + lower + " " + lowerr); long answer = mod_mul(lower, lowerr, mod); answer = mod_div(upper, answer, mod); return answer; } // long[] fact = new long[2 * n + 1]; // long[] ifact = new long[2 * n + 1]; // fact[0] = 1; // ifact[0] = 1; // for (long i = 1; i <= 2 * n; i++) // { // fact[(int)i] = mod_mul(fact[(int)i - 1], i, mod); // ifact[(int)i] = mminvprime(fact[(int)i], mod); // } //ifact is basically inverse factorial in here!!!!!(imp) public static long combination(long n, long r, long m, long[] fact, long[] ifact) { long val1 = fact[(int)n]; long val2 = ifact[(int)(n - r)]; long val3 = ifact[(int)r]; return (((val1 * val2) % m) * val3) % m; } //-----------PrintWriter for faster output--------------------------------- public static PrintWriter out; //-----------MyScanner class for faster input---------- 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; } } //-------------------------------------------------------- }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
c60593e1b370538f81ce2fc4654b4dc8
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.*; import java.util.*; public class Solution extends PrintWriter { long MOD = 1_000_000_007L; void solve() { int n = sc.nextInt(); int p = sc.nextInt(); Integer[] a = new Integer[n]; for(int i = 0; i < n; i++) a[i] = sc.nextInt(); Arrays.sort(a); HashSet<Integer> set = new HashSet<>(); long[] cnt = new long[p+1]; for(int i = 0, idx = 0; i <= p; i++) { while(idx < n && a[idx] < (1<<i)) { int aa = a[idx]; boolean done = false; while(aa > 0) { if(set.contains(aa)) { done = true; break; } if(aa % 2 == 1) { aa /= 2; } else if(aa % 4 == 0){ aa /= 4; } else break; } if(!done) cnt[i]++; set.add(a[idx++]); } cnt[i] += (i-1>=0?cnt[i-1]:0) + (i-2>=0?cnt[i-2]:0); cnt[i] %= MOD; } long ans = 0L; for(long x : cnt) ans = (ans + x) % MOD; println(ans); } // Solution() throws FileNotFoundException { super(new File("saida_6.txt")); } // InputReader sc = new InputReader(new FileInputStream("saida5.txt")); Solution() { super(System.out); } InputReader sc = new InputReader(System.in); static class InputReader { InputReader(InputStream in) { this.in = in; } InputStream in; private byte[] buf = new byte[16384]; private int curChar; private int numChars; public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = in.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } 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 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; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1 || c == ';' ; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] $) { new Thread(null, new Runnable() { public void run() { long start = System.nanoTime(); try {Solution solution = new Solution(); solution.solve(); solution.flush();} catch (Exception e) {e.printStackTrace(); System.exit(1);} System.err.println((System.nanoTime()-start)/1E9); } }, "1", 1 << 27).start(); } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
2f4275435c246297e10bbca653976daf
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; /** TODO: unsolved D. Infinite Set https://codeforces.com/contest/1635/problem/D My idea: 1. How to use 2^p ? 2. 4y > 2y + 1 3. SOLUTION: 1. 2k + 1 must be odd, 4k must be even dp[i]: the number of x which length = i i = 0 => 1 i = 1 => 1 i = 2 => dp[i] = dp[i - 1] = 1 i = 3 => dp[i] = dp[i - 1] + dp[i - 2] = 2 if p = 4, so return dp[0] + dp[1] + dp[2] + dp[3] after delete unless number * */ public class D1 { static int N = 200010; static int mod = (int)1e9 + 7; static int n, p; static int[] a = new int[N]; static long[] dp = new long[N]; static Map<Integer, Integer> map = new HashMap<>(); public static void main(String[] args) { FastScanner sc = new FastScanner(); n = sc.nextInt(); p = sc.nextInt(); for(int i = 1; i <= n; i++) a[i] = sc.nextInt(); solver(); } static void solver() { Arrays.sort(a, 1, n + 1); // initial delete all the number can transfer each other for(int i = 1; i <= n; i++){ int cur = a[i]; // System.out.println(" -> " + cur); boolean flag = true; while (cur > 0){ if(map.containsKey(cur)) flag = false; if((cur & 1) == 1) cur = cur >> 1; else if((cur & 2) == 2) break; else cur = cur >> 2; } if (flag) map.put(a[i], 1); } // System.out.println(map); // pre dp[0] = 1; dp[1] = 1; for(int i = 2; i < N; i++) dp[i] = (dp[i - 1] + dp[i - 2]) % mod; for(int i = 1; i < N; i++) dp[i] = (dp[i] + dp[i - 1]) % mod; long res = 0; for(int i: map.keySet()){ int cur = Integer.toBinaryString(i).length(); if(cur > p) continue; // System.out.println("->" + cur + " " + dp[cur]); res = (res + dp[p - cur]) % mod; } System.out.println(res); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
b778c547384bc8a3b12cf5b941a9363f
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; import static java.lang.Math.*; import static java.lang.Math.ceil; import static java.util.Arrays.sort; public class Round11 { static long dp[]; static HashSet<Long> set; public static void main(String[] args) { FastReader fastreader = new FastReader(); PrintWriter out = new PrintWriter(System.out); int t = 1; outer: while (t-- > 0) { int n = fastreader.nextInt(); int p = fastreader.nextInt(); long[] a = new long[n]; dp = new long[p + 1]; for (int i = 0; i < n; i++) a[i] = fastreader.nextLong(); set = new HashSet<>(); for (int i = 0; i < n; i++) set.add(a[i]); for (int i = 1; i <= p; i++) { if (i < 31) { for (int j = 0; j < n; j++) { if (a[j] >= (1L << (i - 1)) && a[j] < (1L << i)) { boolean ok = true; if (a[j] % 4 == 0 && !fun(a[j] / 4)) ok = false; if (a[j] % 2 == 1 && !fun(a[j] / 2)) ok = false; if (ok) dp[i]++; } } } if (i == 1) dp[i] = (dp[i - 1] + dp[i])%IBIG; else { dp[i] = (dp[i] + dp[i - 1]) % IBIG; dp[i] = (dp[i] + dp[i - 2]) % IBIG; } } long ans = 0; for (int i = 1; i <= p; i++) { ans = (ans + dp[i]) % IBIG; } out.println(ans); } out.close(); } private static boolean fun(long l) { if (set.contains(l)) return false; if (l == 0) return true; if (l % 4 == 0) return fun(l / 4); if (l % 2 == 1) return fun(l / 2); return true; } // constants static final int IBIG = 1000000007; static final int IMAX = 2147483647; static final long LMAX = 9223372036854775807L; static Random __r = new Random(); // math util static int minof(int a, int b, int c) { return min(a, min(b, c)); } static int minof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); int min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static long minof(long a, long b, long c) { return min(a, min(b, c)); } static long minof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return min(x[0], x[1]); if (x.length == 3) return min(x[0], min(x[1], x[2])); long min = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] < min) min = x[i]; return min; } static int maxof(int a, int b, int c) { return max(a, max(b, c)); } static int maxof(int... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); int max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static long maxof(long a, long b, long c) { return max(a, max(b, c)); } static long maxof(long... x) { if (x.length == 1) return x[0]; if (x.length == 2) return max(x[0], x[1]); if (x.length == 3) return max(x[0], max(x[1], x[2])); long max = x[0]; for (int i = 1; i < x.length; ++i) if (x[i] > max) max = x[i]; return max; } static int powi(int a, int b) { if (a == 0) return 0; int ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static long powl(long a, int b) { if (a == 0) return 0; long ans = 1; while (b > 0) { if ((b & 1) > 0) ans *= a; a *= a; b >>= 1; } return ans; } static int fli(double d) { return (int) d; } static int cei(double d) { return (int) ceil(d); } static long fll(double d) { return (long) d; } static long cel(double d) { return (long) ceil(d); } static int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } static int[] exgcd(int a, int b) { if (b == 0) return new int[]{1, 0}; int[] y = exgcd(b, a % b); return new int[]{y[1], y[0] - y[1] * (a / b)}; } static long[] exgcd(long a, long b) { if (b == 0) return new long[]{1, 0}; long[] y = exgcd(b, a % b); return new long[]{y[1], y[0] - y[1] * (a / b)}; } static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static long mix(long x) { x += 0x9e3779b97f4a7c15L; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9L; x = (x ^ (x >> 27)) * 0x94d049bb133111ebL; return x ^ (x >> 31); } public static boolean[] findPrimes(int limit) { assert limit >= 2; final boolean[] nonPrimes = new boolean[limit]; nonPrimes[0] = true; nonPrimes[1] = true; int sqrt = (int) Math.sqrt(limit); for (int i = 2; i <= sqrt; i++) { if (nonPrimes[i]) continue; for (int j = i; j < limit; j += i) { if (!nonPrimes[j] && i != j) nonPrimes[j] = true; } } return nonPrimes; } // array util static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } 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; } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
904d05dae287b7ed3035b38f92bd1410
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.util.HashSet; import java.util.Scanner; public class MainClass { private static final int mod = 1000000007; private static final int N = 200010; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int p = scanner.nextInt(); int[] a = new int[N]; int[] f = new int[N]; int[] sum = new int[N]; f[0] = f[1] = 1; for (int i = 2; i < N; i++) { f[i] = (f[i - 1] + f[i - 2]) % mod; } sum[0] = 1; for (int i = 1; i < N; i++) { sum[i] = (sum[i - 1] + f[i]) % mod; } HashSet<Integer>mp = new HashSet<>(); for (int i = 1; i <= n; i++) { a[i] = scanner.nextInt(); mp.add(a[i]); } int ans = 0; for (int i = 1; i <= n; i++) { int x = a[i]; boolean flag = true; while (x > 0) { if (mp.contains(x) && x != a[i]) { flag = false; break; } if ((x & 3) == 0) { x >>= 2; continue; } if ((x & 1) == 1) { x >>= 1; continue; } break; } if (flag) { for (int j = 30; j >= 0; j--) { if (((a[i] >> j) & 1) == 1) { if (j + 1 <= p) { //System.out.println(a[i] + " " + (j + 1)); ans = (ans + sum[p - j - 1]) % mod; } break; } } } } System.out.println(ans); } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
d0da428a7183137324c3daa5b30a1c60
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class JaiShreeRam{ static Scanner in=new Scanner(); static long mod = 1000000007; static List<List<Integer>> adj; static int seive[]=new int[1000001]; static long C[][]; public static void main(String[] args) throws Exception{ //int z=in.readInt(); //while(z-->0) { solve(); //} } static void solve() { int n=in.readInt(); int p=in.readInt(); int a[]=nia(n); Set<Integer> s=new HashSet<>(); for(int i:a) s.add(i); TreeSet<Integer> st=new TreeSet<>(); for(int i:a) { boolean isavai=false; int ii=i; while((ii>1&&ii%2==1)||(ii>=4&&ii%4==0)) { if(ii%2==1) { ii=ii-1; ii/=2; } else { ii/=4; } if(s.contains(ii)) { isavai=true; break; } } if(!isavai) st.add(i); } int dp[]=new int[p]; long ans=0; for(int i=0;i<p;i++) { if(i<33) { long tmp=(1<<((long)i+1)); while(st.size()>0&&st.first()<tmp) { dp[i]++; st.pollFirst(); } } if(i>0) { dp[i]+=dp[i-1]; } if(i>1) { dp[i]+=dp[i-2]; } dp[i]%=mod; ans+=dp[i]; ans%=mod; } print(ans); } static void ncr(int n, int k){ C= new long[n + 1][k + 1]; int i, j; for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { if (j == 0 || j == i) C[i][j] = 1; else C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } } static boolean isPalin(String s) { int i=0,j=s.length()-1; while(i<=j) { if(s.charAt(i)!=s.charAt(j)) { return false; } i++; j--; } return true; } static int knapsack(int W, int wt[],int val[], int n){ int []dp = new int[W + 1]; for (int i = 1; i < n + 1; i++) { for (int w = W; w >= 0; w--) { if (wt[i - 1] <= w) { dp[w] = Math.max(dp[w],dp[w - wt[i - 1]] + val[i - 1]); } } } return dp[W]; } static void seive() { Arrays.fill(seive, 1); seive[0]=0; seive[1]=0; for(int i=2;i*i<1000001;i++) { if(seive[i]==1) { for(int j=i*i;j<1000001;j+=i) { if(seive[j]==1) { seive[j]=0; } } } } } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static int[] nia(int n){ int[] arr= new int[n]; int i=0; while(i<n){ arr[i++]=in.readInt(); } return arr; } static long[] nla(int n){ long[] arr= new long[n]; int i=0; while(i<n){ arr[i++]=in.readLong(); } return arr; } static int[] nia1(int n){ int[] arr= new int[n+1]; int i=1; while(i<=n){ arr[i++]=in.readInt(); } return arr; } static Integer[] nIa(int n){ Integer[] arr= new Integer[n]; int i=0; while(i<n){ arr[i++]=in.readInt(); } return arr; } static Long[] nLa(int n){ Long[] arr= new Long[n]; int i=0; while(i<n){ arr[i++]=in.readLong(); } return arr; } static long gcd(long a, long b) { if (b==0) return a; return gcd(b, a%b); } static void print(long i) { System.out.println(i); } static void print(Object o) { System.out.println(o); } static void print(int a[]) { for(int i:a) { System.out.print(i+" "); } System.out.println(); } static void print(long a[]) { for(long i:a) { System.out.print(i+" "); } System.out.println(); } static void print(ArrayList<Long> a) { for(long i:a) { System.out.print(i+" "); } System.out.println(); } static void print(Object a[]) { for(Object i:a) { System.out.print(i+" "); } System.out.println(); } static class Scanner{ BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String readString() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } double readDouble() { return Double.parseDouble(readString()); } int readInt() { return Integer.parseInt(readString()); } long readLong() { return Long.parseLong(readString()); } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
34db5f73ed517b8be87a322cb2a6d445
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { Scanner scn = new Scanner(System.in); OutputWriter out = new OutputWriter(System.out); // Always print a trailing "\n" and close the OutputWriter as shown at the end of your output // example: int n = scn.nextInt();int p=scn.nextInt(); int[] a=new int[p+1]; int c=1000000000+7; a[0]=1;a[1]=1; for(int i=2;i<=p;i++){ a[i]=(a[i-1]+a[i-2])%c; } for(int i=1;i<=p;i++){ a[i]=(a[i]+a[i-1])%c; } Integer[] arr=new Integer[n];Set<Integer> set=new HashSet<Integer>(); for(int i1=0;i1<n;i1++){ arr[i1]=scn.nextInt(); set.add(arr[i1]); } int ans=0; for(int i=0;i<n;i++){ int val=arr[i];int tog=0; while((val%2==1)||(val%4==0)){ if(val==0){ break; } if(val%4==0){ val=val/4; if(set.contains(val)){ tog=1;break; } } else if(val%2==1){ val=(val-1)/2; if(set.contains(val)){ tog=1;break; } } } if(tog==0){ val=arr[i];int cnt=0; while(val>=2){ val=val/2;cnt++; } if(p-1>=cnt){ ans+=a[p-1-cnt];ans=ans%c; } } } out.print(ans+" "); out.close(); } // fast input static class Scanner { public BufferedReader reader; public StringTokenizer tokenizer; public Scanner(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { String line = reader.readLine(); if (line == null) return null; tokenizer = new StringTokenizer(line); } catch (Exception e) { throw(new RuntimeException()); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } // fast output static class OutputWriter { BufferedWriter writer; public OutputWriter(OutputStream stream) { writer = new BufferedWriter(new OutputStreamWriter(stream)); } public void print(int i) throws IOException { writer.write(i); } public void print(String s) throws IOException { writer.write(s); } public void print(char[] c) throws IOException { writer.write(c); } public void close() throws IOException { writer.close(); } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
7ebbc9bc82f2e95698132f3efde730f3
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.awt.MultipleGradientPaint.ColorSpaceType; import java.io.*; import java.sql.PreparedStatement; import java.util.*; public class cp { static int mod=(int)1e9+7; // static Reader sc=new Reader(); static FastReader sc=new FastReader(System.in); static int[] sp; static int size=(int)1e6; static int[] arInt; static long[] arLong; static long ans; public static void main(String[] args) throws IOException { // long tc=sc.nextLong(); // Scanner sc=new Scanner(System.in); int tc=1; // print_int(pre); // primeSet=new HashSet<>(); // primeCnt=new int[(int)1e9]; // sieveOfEratosthenes((int)1e9); // factorial(mod); // InverseofNumber(mod); // InverseofFactorial(mod); while(tc-->0) { int n=sc.nextInt(); int p=sc.nextInt(); Set<Integer> a=new HashSet<>(); for(int i=0;i<n;i++) { a.add(sc.nextInt()); } Set<Integer> v=new HashSet<>(); for(Integer each:a) { int k=each; boolean can=false; while((k>1 && k%2!=0) || (k>=4 && k%4==0)) { if (k % 4 == 0) k /= 4; else k /= 2; if(a.contains(k)) { can=true; break; } } if (!can) { v.add(each); } } long dp[]=new long[p+1]; long ans=0; for(int i=0;i<p;i++) { if(i<35) { a.clear(); int k=(1<<(i+1)); if(v.size()>0) for(Integer each:v) { if(each<k) { dp[i]++; a.add(each); } } for(Integer e:a) v.remove(e); } if (i>0) { dp[i]+=dp[i-1]; } if (i>1) { dp[i]+=dp[i-2]; } dp[i]=dp[i]%mod; ans=(ans+dp[i])%mod; } out.println(ans); } // System.out.flush(); out.flush(); out.close(); System.gc(); } /* ...SOLUTION ENDS HERE...........SOLUTION ENDS HERE... */ static class Node{ int a; ArrayList<Pair> adj; public Node(int a) { this.a=a; this.adj=new ArrayList<>(); } } static void dijkstra(Node[] g,int dist[],int parent[],int src) { Arrays.fill(dist, int_max); Arrays.fill(parent, -1); boolean vis[]=new boolean[dist.length]; // vis[1]=true; PriorityQueue<Pair> q=new PriorityQueue<>(); q.add(new Pair(1, 0)); dist[1]=0; while(!q.isEmpty()) { Pair curr=q.poll(); vis[curr.x]=true; for(Pair edge:g[curr.x].adj) { if (vis[edge.x]) { continue; } if (dist[edge.x]>dist[curr.x]+edge.y) { dist[edge.x]=dist[curr.x]+edge.y; parent[edge.x]=curr.x; q.add(new Pair(edge.x, dist[edge.x])); } } } } static void mapping(int a[]) { Pair[] temp=new Pair[a.length]; for (int i = 0; i < temp.length; i++) { temp[i]=new Pair(a[i], i); } Arrays.sort(temp); int k=0; for (int i = 0; i < temp.length; i++) { a[temp[i].y]=k++; } } static boolean palin(String s) { for(int i=0;i<s.length()/2;i++) if(s.charAt(i)!=s.charAt(s.length()-i-1)) return false; return true; } static class temp implements Comparable<temp>{ int x; int y; int sec; public temp(int x,int y,int l) { // TODO Auto-generated constructor stub this.x=x; this.y=y; this.sec=l; } @Override public int compareTo(cp.temp o) { // TODO Auto-generated method stub return this.sec-o.sec; } } // static class Node{ // int x; // int y; // ArrayList<Integer> edges; // public Node(int x,int y) { // // TODO Auto-generated constructor stub // this.x=x; // this.y=y; // this.edges=new ArrayList<>(); // } // } static int lis(int arr[],int n) { int ans=0; int dp[]=new int[n+1]; Arrays.fill(dp, int_max); dp[0]=int_min; for(int i=0;i<n;i++) { int j=UpperBound(dp,arr[i]); if(dp[j-1]<=arr[i] && arr[i]<dp[j]) dp[j]=arr[i]; } for(int i=0;i<=n;i++) { if(dp[i]<int_max) ans=i; } return ans; } static long get(long n) { return n*(n+1)/2L; } static boolean go(ArrayList<Pair> caves,int k) { for(Pair each:caves) { if(k<=each.x) return false; k+=each.y; } return true; } static String revString(String s) { char arr[]=s.toCharArray(); int n=s.length(); for(int i=0;i<n/2;i++) { char temp=arr[i]; arr[i]=arr[n-i-1]; arr[n-i-1]=temp; } return String.valueOf(arr); } // Fuction return the number of set bits in n static int SetBits(int n) { int cnt=0; while(n>0) { if((n&1)==1) { cnt++; } n=n>>1; } return cnt; } static boolean isPowerOfTwo(int n) { return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2))))); } static void arrInt(int n) throws IOException { arInt=new int[n]; for (int i = 0; i < arInt.length; i++) { arInt[i]=sc.nextInt(); } } static void arrLong(int n) throws IOException { arLong=new long[n]; for (int i = 0; i < arLong.length; i++) { arLong[i]=sc.nextLong(); } } static ArrayList<Integer> add(int id,int c) { ArrayList<Integer> newArr=new ArrayList<>(); for(int i=0;i<id;i++) newArr.add(arInt[i]); newArr.add(c); for(int i=id;i<arInt.length;i++) { newArr.add(arInt[i]); } return newArr; } // function to find first index >= y static int upper(ArrayList<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (h-l>1) { int mid = (l + h) / 2; if (arr.get(mid) <= x) l=mid+1; else { h=mid; } } if(arr.get(l)>x) { return l; } if(arr.get(h)>x) return h; return -1; } static int upper(ArrayList<Long> arr, int n, long x) { int l = 0, h = n - 1; while (h-l>1) { int mid = (l + h) / 2; if (arr.get(mid) <= x) l=mid+1; else { h=mid; } } if(arr.get(l)>x) { return l; } if(arr.get(h)>x) return h; return -1; } static int lower(ArrayList<Integer> arr, int n, int x) { int l = 0, h = n - 1; while (h-l>1) { int mid = (l + h) / 2; if (arr.get(mid) < x) l=mid+1; else { h=mid; } } if(arr.get(l)>=x) { return l; } if(arr.get(h)>=x) return h; return -1; } static int N = (int)2e5+5; // Array to store inverse of 1 to N static long[] factorialNumInverse = new long[N + 1]; // Array to precompute inverse of 1! to N! static long[] naturalNumInverse = new long[N + 1]; // Array to store factorial of first N numbers static long[] fact = new long[N + 1]; // Function to precompute inverse of numbers public static void InverseofNumber(int p) { naturalNumInverse[0] = naturalNumInverse[1] = 1; for(int i = 2; i <= N; i++) naturalNumInverse[i] = naturalNumInverse[p % i] * (long)(p - p / i) % p; } // Function to precompute inverse of factorials public static void InverseofFactorial(int p) { factorialNumInverse[0] = factorialNumInverse[1] = 1; // Precompute inverse of natural numbers for(int i = 2; i <= N; i++) factorialNumInverse[i] = (naturalNumInverse[i] * factorialNumInverse[i - 1]) % p; } // Function to calculate factorial of 1 to N public static void factorial(int p) { fact[0] = 1; // Precompute factorials for(int i = 1; i <= N; i++) { fact[i] = (fact[i - 1] * (long)i) % p; } } // Function to return nCr % p in O(1) time public static long Binomial(int N, int R, int p) { // n C r = n!*inverse(r!)*inverse((n-r)!) long ans = ((fact[N] * factorialNumInverse[R]) % p * factorialNumInverse[N - R]) % p; return ans; } static String tr(String s) { int now = 0; while (now + 1 < s.length() && s.charAt(now)== '0') ++now; return s.substring(now); } static boolean isPrime(long 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 ArrayList<Integer> commDiv(int a, int b) { // find gcd of a, b int n = gcd(a, b); // Count divisors of n. ArrayList<Integer> Div=new ArrayList<>(); for (int i = 1; i <= Math.sqrt(n); i++) { // if 'i' is factor of n if (n % i == 0) { // check if divisors are equal if (n / i == i) Div.add(i); else { Div.add(i); Div.add(n/i); } } } return Div; } static HashSet<Integer> factors(int x) { HashSet<Integer> a=new HashSet<Integer>(); for(int i=2;i*i<=x;i++) { if(x%i==0) { a.add(i); a.add(x/i); } } return a; } static void primeFactors(int n,HashSet<Integer> factors) { // Print the number of 2s that divide n while (n%2==0) { factors.add(2); n /= 2; } // n must be odd at this point. So we can // skip one element (Note i = i +2) for (int i = 3; i <= Math.sqrt(n); i+= 2) { // While i divides n, print i and divide n while (n%i == 0) { factors.add(i); n /= i; } } // This condition is to handle the case when // n is a prime number greater than 2 if (n > 2) factors.add(n); } static class Tuple{ int a; int b; int c; public Tuple(int a,int b,int c) { this.a=a; this.b=b; this.c=c; } } //function to find prime factors of n static HashMap<Long,Long> findFactors(long n2) { HashMap<Long,Long> ans=new HashMap<>(); if(n2%2==0) { ans.put(2L, 0L); // cnt++; while((n2&1)==0) { n2=n2>>1; ans.put(2L, ans.get(2L)+1); // } } for(long i=3;i*i<=n2;i+=2) { if(n2%i==0) { ans.put((long)i, 0L); // cnt++; while(n2%i==0) { n2=n2/i; ans.put((long)i, ans.get((long)i)+1); } } } if(n2!=1) { ans.put(n2, ans.getOrDefault(n2, (long) 0)+1); } return ans; } //fenwick tree implementaion static class fwt { int n; long BITree[]; fwt(int n) { this.n=n; BITree=new long[n+1]; } fwt(int arr[], int n) { this.n=n; BITree=new long[n+1]; for(int i = 0; i < n; i++) updateBIT(n, i, arr[i]); } long getSum(int index) { long sum = 0; index = index + 1; while(index>0) { sum += BITree[index]; index -= index & (-index); } return sum; } void updateBIT(int n, int index,int val) { index = index + 1; while(index <= n) { BITree[index] += val; index += index & (-index); } } long sum(int l, int r) { return getSum(r) - getSum(l - 1); } void print() { for(int i=0;i<n;i++) out.print(getSum(i)+" "); out.println(); } } static class sparseTable{ int n; long[][]dp; int log2[]; int P; void buildTable(long[] arr) { n=arr.length; P=(int)Math.floor(Math.log(n)/Math.log(2)); log2=new int[n+1]; log2[0]=log2[1]=0; for(int i=2;i<=n;i++) { log2[i]=log2[i/2]+1; } dp=new long[P+1][n]; for(int i=0;i<n;i++) { dp[0][i]=arr[i]; } for(int p=1;p<=P;p++) { for(int i=0;i+(1<<p)<=n;i++) { long left=dp[p-1][i]; long right=dp[p-1][i+(1<<(p-1))]; dp[p][i]=Math.min(left, right); } } } long maxQuery(int l,int r) { int len=r-l+1; int p=(int)Math.floor(log2[len]); long left=dp[p][l]; long right=dp[p][r-(1<<p)+1]; return Math.min(left,right); } } //Function to find number of set bits static int setBitNumber(long n) { if (n == 0) return 0; int msb = 0; n = n / 2; while (n != 0) { n = n / 2; msb++; } return msb; } static int getFirstSetBitPos(long n) { return (int)((Math.log10(n & -n)) / Math.log10(2)) + 1; } static ArrayList<Integer> primes; static HashSet<Integer> primeSet; static boolean prime[]; static int primeCnt[]; static void sieveOfEratosthenes(int n) { prime= new boolean[n + 1]; for (int i = 2; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers for (int i = 2; i <= n; i++) { if (prime[i] == true) primeCnt[i]=primeCnt[i-1]+1; } } static long mod(long a, long b) { long c = a % b; return (c < 0) ? c + b : c; } static void swap(long arr[],int i,int j) { long temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } static boolean util(int a,int b,int c) { if(b>a)util(b, a, c); while(c>=a) { c-=a; if(c%b==0) return true; } return (c%b==0); } static void flag(boolean flag) { out.println(flag ? "YES" : "NO"); out.flush(); } static void print(int a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print(long a[]) { int n=a.length; for(int i=0;i<n;i++) { out.print(a[i]+" "); } out.println(); out.flush(); } static void print_int(ArrayList<Integer> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void print_long(ArrayList<Long> al) { int si=al.size(); for(int i=0;i<si;i++) { out.print(al.get(i)+" "); } out.println(); out.flush(); } static void printYesNo(boolean condition) { if (condition) { out.println("YES"); } else { out.println("NO"); } } static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } static int lowerIndex(int arr[], int n, int x) { int l = 0, h = n - 1; while (l<=h) { int mid = (l + h) / 2; if (arr[mid] >= x) h = mid -1 ; else l = mid + 1; } return l; } // function to find last index <= y static int upperIndex(int arr[], int n, int y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int upperIndex(long arr[], int n, long y) { int l = 0, h = n - 1; while (l <= h) { int mid = (l + h) / 2; if (arr[mid] <= y) l = mid + 1; else h = mid - 1; } return h; } static int UpperBound(int a[], int x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static int UpperBound(long a[], long x) {// x is the key or target value int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]<=x) l=m; else r=m; } return l+1; } static class DisjointUnionSets { int[] rank, parent; int n; // Constructor public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } // Creates n sets with single item in each 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]; } // Unites the set that includes x and the set // that includes x void union(int x, int y) { int xRoot = find(x), yRoot = find(y); if (xRoot == yRoot) return; if (rank[xRoot] < rank[yRoot]) parent[xRoot] = yRoot; else if (rank[yRoot] < rank[xRoot]) parent[yRoot] = xRoot; else // if ranks are the same { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } // if(xRoot!=yRoot) // parent[y]=x; } int connectedComponents() { int cnt=0; for(int i=0;i<n;i++) { if(parent[i]==i) cnt++; } return cnt; } } // static class Graph // { // int v; // ArrayList<Integer> list[]; // Graph(int v) // { // this.v=v; // list=new ArrayList[v+1]; // for(int i=1;i<=v;i++) // list[i]=new ArrayList<Integer>(); // } // void addEdge(int a, int b) // { // this.list[a].add(b); // } // // // // } static class Pair implements Comparable<Pair> { int x; int y; Pair(int x,int y) { this.x=x; this.y=y; } @Override public int compareTo(Pair o) { // TODO Auto-generated method stub return this.y-o.y; } } static class PairL implements Comparable<PairL> { long x; long y; PairL(long x,long y) { this.x=x; this.y=y; } @Override public int compareTo(PairL o) { // TODO Auto-generated method stub return (this.y>o.y)?1:-1; } } static long sum_array(int a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static long sum_array(long a[]) { int n=a.length; long sum=0; for(int i=0;i<n;i++) sum+=a[i]; return sum; } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void sort(long[] a) { ArrayList<Long> l=new ArrayList<>(); for (long i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static void reverse_array(int a[]) { int n=a.length; int i,t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } static void reverse_array(long a[]) { int n=a.length; int i; long t; for (i = 0; i < n / 2; i++) { t = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = t; } } // static long modInverse(long a, long m) // { // long g = gcd(a, m); // // return power(a, m - 2, m); // // } static long power(long x, long y) { long res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static int power(int x, int y) { int res = 1; x = x % mod; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % mod; y = y >> 1; // y = y/2 x = (x * x) % mod; } return res; } static long gcd(long a, long b) { if (a == 0) return b; //cnt+=a/b; return gcd(b%a,a); } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } static class FastReader{ byte[] buf = new byte[2048]; int index, total; InputStream in; FastReader(InputStream is) { in = is; } int scan() throws IOException { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) { return -1; } } return buf[index++]; } String next() throws IOException { int c; for (c = scan(); c <= 32; c = scan()); StringBuilder sb = new StringBuilder(); for (; c > 32; c = scan()) { sb.append((char) c); } return sb.toString(); } int nextInt() throws IOException { int c, val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } long nextLong() throws IOException { int c; long val = 0; for (c = scan(); c <= 32; c = scan()); boolean neg = c == '-'; if (c == '-' || c == '+') { c = scan(); } for (; c >= '0' && c <= '9'; c = scan()) { val = (val << 3) + (val << 1) + (c & 15); } return neg ? -val : val; } } 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 readLine() 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(); } } static PrintWriter out=new PrintWriter(System.out); static int int_max=Integer.MAX_VALUE; static int int_min=Integer.MIN_VALUE; static long long_max=Long.MAX_VALUE; static long long_min=Long.MIN_VALUE; }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
b6e047c4c84d0d6388735ceb407a9ec9
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef {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; } } // static int mod = 998244353 ; // static int N = 200005; // static long factorial_num_inv[] = new long[N+1]; // static long natual_num_inv[] = new long[N+1]; // static long fact[] = new long[N+1]; // static void InverseofNumber() //{ // natual_num_inv[0] = 1; // natual_num_inv[1] = 1; // for (int i = 2; i <= N; i++) // natual_num_inv[i] = natual_num_inv[mod % i] * (mod - mod / i) % mod; //} //static void InverseofFactorial() //{ // factorial_num_inv[0] = factorial_num_inv[1] = 1; // for (int i = 2; i <= N; i++) // factorial_num_inv[i] = (natual_num_inv[i] * factorial_num_inv[i - 1]) % mod; //} //static long nCrModP(long N, long R) //{ // long ans = ((fact[(int)N] * factorial_num_inv[(int)R]) % mod * factorial_num_inv[(int)(N - R)]) % mod; // return ans%mod; //} //static boolean prime[]; //static void sieveOfEratosthenes(int n) //{ // prime = new boolean[n+1]; // for (int i = 0; i <= n; i++) // prime[i] = true; // for (int p = 2; p * p <= n; p++) // { // // If prime[p] is not changed, then it is a // // prime // if (prime[p] == true) // { // // Update all multiples of p // for (int i = p * p; i <= n; i += p) // prime[i] = false; // } // } //} public static void main (String[] args) throws java.lang.Exception { // InverseofNumber(); // InverseofFactorial(); // fact[0] = 1; // for (long i = 1; i <= 2*100000; i++) // { // fact[(int)i] = (fact[(int)i - 1] * i) % mod; // } FastReader scan = new FastReader(); PrintWriter pw = new PrintWriter(System.out); int n= scan.nextInt(); int p = scan.nextInt(); TreeSet<Long> ts = new TreeSet<>(); long count[] = new long[31+p]; for(int i=0;i<n;i++){ long x = scan.nextLong(); ts.add(x); } for(long x:ts){ boolean flag = true; long start = x; while(x>0){ if(x%2==1){ x = x-1; x = x/2; } else if(x%4==0){ x = x/4; } else{ break; } if(ts.contains(x)){ flag = false; break; } } if(flag){ int y = (int)(Math.log(start)/Math.log(2)); count[y]++; } } long dp[] = new long[p+1]; long mod = 1000000007; dp[0] = count[0]%mod; dp[1] = (dp[0]+count[1])%mod; for(int i=2;i<p;i++){ dp[i] = (dp[i-2]%mod+dp[i-1]%mod+count[i]%mod)%mod; } long sum = 0; for(int i=0;i<p;i++){ sum = (sum%mod+dp[i]%mod)%mod; } pw.println(sum); pw.flush(); } //static long bin_exp_mod(long a,long n){ // long res = 1; // while(n!=0){ // if(n%2==1){ // res = ((res)*(a)); // } // n = n/2; // a = ((a)*(a)); // } // return res; //} //static long bin_exp_mod(long a,long n){ // long mod = 1000000007; // long res = 1; // // while(n!=0){ // if(n%2==1){ // res = ((res%mod)*(a%mod))%mod; // } // n = n/2; // a = ((a%mod)*(a%mod))%mod; // } // res = res%mod; // return res; //} // static long gcd(long a,long b){ // if(a==0) // return b; // return gcd(b%a,a); // } // static long lcm(long a,long b){ // return (a/gcd(a,b))*b; // } } class Pair{ int x,y; Pair(int x,int y){ this.x = x; this.y = y; } //public boolean equals(Object obj) { // // TODO Auto-generated method stub // if(obj instanceof Pair) // { // Pair temp = (Pair) obj; // if(this.x.equals(temp.x) && this.y.equals(temp.y)) // return true; // } // return false; //} //@Override //public int hashCode() { // // TODO Auto-generated method stub // return (this.x.hashCode() + this.y.hashCode()); //} } class Compar implements Comparator<Pair>{ public int compare(Pair p1,Pair p2){ if(p1.y==p2.y) return 0; else if(p1.y<p2.y) return -1; else return 1; } } //class DisjointSet{ // Map<Long,Node> map = new HashMap<>(); // class Node{ // long data; // Node parent; // int rank; // } // public void makeSet(long data){ // Node node = new Node(); // node.data = data; // node.parent = node; // node.rank = 0; // map.put(data,node); // } // //here we just need the rank of parent to be exact // public void union(long data1,long data2){ // Node node1 = map.get(data1); // Node node2 = map.get(data2); // Node parent1 = findSet(node1); // Node parent2 = findSet(node2); // if(parent1.data==parent2.data){ // return; // } // if(parent1.rank>=parent2.rank){ // parent1.rank = (parent1.rank==parent2.rank)?parent1.rank+1:parent1.rank; // parent2.parent = parent1; // } // else{ // parent1.parent = parent2; // } // } // public long findSet(long data){ // return findSet(map.get(data)).data; // } // private Node findSet(Node node){ // Node parent = node.parent; // if(parent==node){ // return parent; // } // node.parent = findSet(node.parent); // return node.parent; // } // }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
6ba18db50f42027aff2ed3815885ba8d
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.StringTokenizer; import java.util.TreeSet; public class TaskD { final long mod = 1_000_000_007; public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); TaskD sol = new TaskD(); sol.solve(in, out); out.flush(); } void solve(FastScanner in, PrintWriter out) { int n = in.nextInt(), p = in.nextInt(); int a[] = new int[n]; for (int i = 0; i < n; i++) a[i] = in.nextInt(); Arrays.sort(a); TreeSet<Integer> s = new TreeSet<>(); for (int i = 0; i < n; i++) { int val = a[i]; boolean bad = false; while (true) { if (val % 2 == 0) { if (val % 4 == 0) { val /= 4; bad |= s.contains(val); if (val == 0) break; } else { break; } } else { val = (val-1) / 2; bad |= s.contains(val); } } if (!bad) { s.add(a[i]); } } ArrayList<ArrayList<Integer>> b = new ArrayList<>(); for (int i = 0; i < 35; i++) b.add(new ArrayList<>()); for (int x : s) { b.get(binaryLength(x)).add(x); } long dp[] = new long[p+1]; for (int l = 1; l <= p; l++) { dp[l] = dp[l-1]; if (l >= 2) dp[l] = (dp[l] + dp[l-2]) % mod; if (l <= 30) dp[l] = (dp[l] + b.get(l).size()) % mod; } long ans = 0; for (int i = 1; i <= p; i++) ans = (ans + dp[i]) % mod; out.println(ans); } int binaryLength(int n) { int ans = 0; for (int i = 0; i < 30; i++) { if (getBit(n, i) == 1) { ans = i; } } return ans+1; } int getBit(int n, int i) { return (n >> i) & 1; } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!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()); } float nextFloat() { return Float.parseFloat(next()); } double nextDouble() { return Double.parseDouble(next()); } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
64f5cd9f2f5ac27af15b0cd2362e2720
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
//package infiniteset; import java.util.*; import java.io.*; public class infiniteset { public static void main(String[] args) throws IOException { BufferedReader fin = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(fin.readLine()); int n = Integer.parseInt(st.nextToken()); int p = Integer.parseInt(st.nextToken()) + 1; int mod = (int) (1e9 + 7); int[] dp = new int[p]; dp[0] = 1; for(int i = 0; i < p; i++) { if(i + 1 < p) { dp[i + 1] += dp[i]; dp[i + 1] %= mod; } if(i + 2 < p) { dp[i + 2] += dp[i]; dp[i + 2] %= mod; } } for(int i = 1; i < p; i++) { dp[i] += dp[i - 1]; dp[i] %= mod; //System.out.println(dp[i]); } int[] nums = new int[n]; HashSet<Integer> v = new HashSet<Integer>(); st = new StringTokenizer(fin.readLine()); for(int i = 0; i < n; i++) { nums[i] = Integer.parseInt(st.nextToken()); v.add(nums[i]); } ArrayList<Integer> valid = new ArrayList<Integer>(); for(int i = 0; i < n; i++) { int next = nums[i]; while(true) { if(next % 2 == 1) { next /= 2; } else if(next % 4 == 0) { next /= 4; } else { valid.add(nums[i]); break; } if(v.contains(next)) { break; } if(next == 0) { valid.add(nums[i]); break; } } } //System.out.println(valid); int ans = 0; for(int i : valid) { int length = Integer.toBinaryString(i).length(); //System.out.println(i + " " + length); if(p - 1 - length >= 0) { ans += dp[p - 1 - length]; ans %= mod; } } System.out.println(ans); } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
b2d08d6ce9b78bcc06ae0c273cc1dc5f
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
// package c1635; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Queue; import java.util.Random; import java.util.Set; import java.util.StringTokenizer; import java.util.TreeMap; // // Codeforces Round #772 (Div. 2) 2022-02-20 06:35 // D. Infinite Set // https://codeforces.com/contest/1635/problem/D // time limit per test 2 seconds; memory limit per test 256 megabytes // public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*' // // You are given an array a consisting of n positive integers. // // Let's consider an infinite integer set S which contains all integers x that satisfy at least one // of the following conditions: // 1. x = a_i for some 1 <= i <= n. // 2. x = 2y + 1 and y is in S. // 3. x = 4y and y is in S. // // For example, if a = [1,2] then the 10 smallest elements in S will be {1,2,3,4,5,7,8,9,11,12}. // // Find the number of elements in S that are strictly smaller than 2^p. Since this number may be too // large, print it modulo 10^9 + 7. // // Input // // The first line contains two integers n and p (1 <= n, p <= 2 * 10^5). // // The second line contains n integers a_1,a_2,...,a_n (1 <= a_i <= 10^9). // // It is guaranteed that all the numbers in a are distinct. // // Output // // Print a single integer, the number of elements in S that are strictly smaller than 2^p. Remember // to print it modulo 10^9 + 7. // // Example /* input: 2 4 6 1 output: 9 input: 4 7 20 39 5 200 output: 14 input: 2 200000 48763 1000000000 output: 448201910 */ // Note // // In the first example, the elements smaller than 2^4 are {1, 3, 4, 6, 7, 9, 12, 13, 15}. // // In the second example, the elements smaller than 2^7 are // {5,11,20,23,39,41,44,47,79,80,83,89,92,95}. // public class C1635D { static final int MOD = 1000000007; static final Random RAND = new Random(); static int[] TSIZES = computeTreeSizes(); static int solve(int[] a, int p) { sort(a); Set<Integer> seen = new HashSet<>(); long ans = 0; for (int v : a) { if (isUnderSubtree(v, seen)) { continue; } seen.add(v); int size = getTreeSize(v, p); ans = (ans + size) % MOD; // System.out.format(" v:%4d size:%4d ans:%4d\n", v, size, ans); } return (int) ans; } // check whether v is under a subtree already seen static boolean isUnderSubtree(int v, Set<Integer> seen) { int w = v; while (w > 0) { if (seen.contains(w)) { return true; } if (w % 2 == 1) { w = (w - 1) / 2; } else if (w % 4 == 0) { w /= 4; } else { break; } } return false; } public static void sort(int[] arr) { for (int i = 0; i < arr.length; i++) { int r = RAND.nextInt(arr.length); int temp = arr[i]; arr[i] = arr[r]; arr[r] = temp; } Arrays.sort(arr); } static int[] computeTreeSizes() { int m = 200005; int[] sizes = new int[m]; sizes[0] = 1; int[] fib = new int[m]; fib[0] = 1; fib[1] = 1; for (int i = 2; i < fib.length; i++) { fib[i] = (fib[i-2] + fib[i-1]) % MOD; } for (int i = 1; i < m; i++) { sizes[i] = (sizes[i-1] + fib[i]) % MOD; } return sizes; } // Get count of numbers < 2^p in tree with root r. static int getTreeSize(int r, int p) { /* depth size #trees root_b root_e 0 1 524288 2097154 4194302 1 2 262144 1048578 2097150 2 4 131072 524290 1048574 3 7 65536 262146 524286 4 12 32768 131074 262142 5 20 16384 65538 131070 6 33 8192 32770 65534 7 54 4096 16386 32766 8 88 2048 8194 16382 9 143 1024 4098 8190 10 232 512 2050 4094 11 376 256 1026 2046 12 609 128 514 1022 13 986 64 258 510 14 1596 32 130 254 15 2583 16 66 126 16 4180 8 34 62 17 6764 4 18 30 18 10945 2 10 14 19 17710 1 6 6 20 28656 1 2 2 21 46367 1 1 1 */ // The increment of tree size forms a Fibonacci sequence int d = getTreeDepth(r,p); return d < 0 ? 0 : TSIZES[d]; } // Get the depth of the root with root r and with number < 2^p. // If r >= 2^p, return -1; // If there is only the root, return 0. static int getTreeDepth(int r, int p) { if (r == 1) { return p - 1; } int len = Integer.toBinaryString(r).length(); // For example: // 10111010 = 186 (len 8) // 100000000 = 2^8 return len <= p ? p - len : -1; } static int getRoot(int v) { while (v > 1) { if (v % 2 == 1) { v = (v - 1) / 2; } else if (v % 4 == 0) { v /= 4; } else { break; } } return v; } static int solveNaive(int[] a, int p) { int m = 1 << p; boolean[] used = new boolean[m]; Queue<Integer> q = new LinkedList<>(); int ans = 0; for (int v : a) { if (v < m && !used[v]) { q.add(v); used[v] = true; ans++; } } while (!q.isEmpty()) { int v = q.poll(); int x = v * 2 + 1; if (x < m && !used[x]) { used[x] = true; q.add(x); ans++; } int y = v * 4; if (y < m && !used[y]) { used[y] = true; q.add(y); ans++; } } return ans; } static boolean test = false; static void doTest() { if (!test) { return; } // Map from root the size of the root TreeMap<Integer, Integer> rcm = new TreeMap<>(); int p = 20; for (int i = 1; i < (1 << p); i++) { int r = getRoot(i); rcm.put(r, rcm.getOrDefault(r, 0) + 1); } // total number of trees is 1 + 2^(p-2) System.out.format("p:%d #trees:%d\n", p, rcm.size()); // Map from tree-size to list of roots TreeMap<Integer, List<Integer>> stm = new TreeMap<>(); for (Map.Entry<Integer, Integer> e : rcm.entrySet()) { int count = e.getValue(); stm.computeIfAbsent(count, x -> new ArrayList<>()).add(e.getKey()); } System.out.format("%6s %8s %8s %8s %8s\n", "depth", "size", "#trees", "root_b", "root_e"); int depth = -1; for (Map.Entry<Integer, List<Integer>> e : stm.entrySet()) { depth++; List<Integer> roots = e.getValue(); System.out.format("%6d %8d %8d %8d %8d\n", depth, e.getKey(), roots.size(), roots.get(0), roots.get(roots.size() - 1)); } // Verify getTreeSize in each root range is expected for (Map.Entry<Integer, List<Integer>> e : stm.entrySet()) { int size = e.getKey(); List<Integer> roots = e.getValue(); int rb = roots.get(0); int re = roots.get(roots.size() - 1); myAssert(size == getTreeSize(rb, p)); myAssert(size == getTreeSize(re, p)); } { int n = 10; p = 10; int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = 1 + RAND.nextInt(1000 + (1 << p)); } int ans = solve(a, p); int exp = solveNaive(a, p); System.out.format("%s -> %d %s\n", trace(a), ans, ans == exp ? "":" Expected " + exp); } { int n = 2; p = 200000; int[] a = new int[] {48763, 1000000000}; int ans = solve(a, p); int exp = 448201910; System.out.format("%s -> %d %s\n", trace(a), ans, ans == exp ? "":" Expected " + exp); } long t0 = System.currentTimeMillis(); System.out.format("%d msec\n", System.currentTimeMillis() - t0); System.exit(0); } public static String traceMap(Map<Integer, Integer> m) { StringBuilder sb = new StringBuilder(); sb.append(m.size()); for (Map.Entry<Integer, Integer> e : m.entrySet()) { sb.append(String.format(" (%d,%d)", e.getKey(), e.getValue())); } return sb.toString(); } static String trace(int[] a) { return Arrays.toString(a).replace('[', '{').replace(']', '}').replace(" ", ""); } public static void main(String[] args) { doTest(); MyScanner in = new MyScanner(); int n = in.nextInt(); int p = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } int ans = solve(a, p); System.out.println(ans); } static void output(int[] a) { if (a == null) { System.out.println("-1"); return; } StringBuilder sb = new StringBuilder(); for (int v : a) { sb.append(v); sb.append(' '); if (sb.length() > 500) { System.out.print(sb.toString()); sb.setLength(0); } } System.out.println(sb.toString()); } static void myAssert(boolean cond) { if (!cond) { throw new RuntimeException("Unexpected"); } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName(); cname = cname.lastIndexOf('.') > 0 ? cname.substring(0, cname.lastIndexOf('.')) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
0fe87a9a5bac8a079e1c3ac985c2be37
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashSet; import java.util.Set; import java.util.StringTokenizer; public class D_Inf { public static void main(String[] args) throws IOException { BufferedScanner input = new BufferedScanner(); BufferedOutput output = new BufferedOutput(); int numsCount = input.nextInt(); int maxPow = input.nextInt(); Set<Long> nums = new HashSet<>(); for (int i = 0; i < numsCount; i++) { long number = input.nextLong(); if (log2(number) <= maxPow) { nums.add(number); } } Set<Long> filteredNums = new HashSet<>(); for (Long number : nums) { long reduced = number; boolean matched = false; while (reduce(reduced) != reduced) { reduced = reduce(reduced); if (nums.contains(reduced)) { matched = true; break; } } if (!matched) { filteredNums.add(number); } } long[] freqs = new long[maxPow + 3]; for (Long number : filteredNums) { int idx = (int) Math.floor(log2(number)) + 1; freqs[idx] = freqs[idx] + 1; } long count = 0; long mod = (long) Math.pow(10, 9) + 7; for (int i = 1; i <= maxPow; i++) { count = (count + freqs[i]) % mod; freqs[i + 1] = (freqs[i + 1] + freqs[i]) % mod; freqs[i + 2] = (freqs[i + 2] + freqs[i]) % mod; } output.println(count % mod); output.flush(); } private static double log2(long number) { return Math.log(number) / Math.log(2); } private static long reduce(long number) { if (number % 4 == 0) { return number / 4; } if(number % 2 == 1) { return (number - 1) / 2; } return number; } private static class BufferedScanner { private final BufferedReader reader; private StringTokenizer tokenizer; public BufferedScanner() { reader = new BufferedReader(new InputStreamReader(System.in)); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public String nextLine() throws IOException { tokenizer = null; return reader.readLine(); } private String nextToken() throws IOException { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } return tokenizer.nextToken(); } } private static class BufferedOutput { private final StringBuilder result; public BufferedOutput() { result = new StringBuilder(); } public void print(Object object) { result.append(object); } public void println(Object object) { result.append(object).append("\n"); } public void flush() { System.out.println(result); System.out.flush(); } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
3a23b98c45a1e653333f95b3d05b7bfd
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class D { public static FastScanner s = new FastScanner(); public static PrintWriter out = new PrintWriter(System.out); public static void main(String[] args) { solve(s.nextInt(), s.nextInt()); out.close(); } static long mod = 1000000007; public static void solve(int n, int p) { int[] a = s.readArray(n); Arrays.sort(a); long[] dp = new long[Math.max(p + 1, 31)]; HashSet<Integer> pre = new HashSet<>(); int t; boolean c; for (int i = 0; i < n; i++) { t = a[i]; c = false; while (t % 2 == 1 || t % 4 == 0) { if (t % 2 == 1) t /= 2; else t /= 4; if (pre.contains(t)) c = true; if (t == 0) break; } if (!c) pre.add(a[i]); } for (int e: pre) { for (int i = 0; i <= 30; i++) { if (1 << i > e) { dp[i]++; break; } } } for (int i = 2; i <= p; i++) { dp[i] += dp[i - 1] + dp[i - 2]; dp[i] %= mod; } long s = 0; for (int i = 0; i <= p; i++) { s += dp[i]; s %= mod; } System.out.println(s); } static class FastScanner {//copied from secondthread BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
32d506109a074d9d65461fad1425027c
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.util.*; import java.io.*; public class Main { static FastScanner sc = new FastScanner(System.in); static PrintWriter pw = new PrintWriter(System.out); static StringBuilder sb = new StringBuilder(); static long mod = (long) 1e9 + 7; public static void main(String[] args) throws Exception { solve(); pw.flush(); } public static void solve() { int N = sc.nextInt(); int p = sc.nextInt(); HashSet<Integer> set = new HashSet<>(); ArrayList<Integer> arr = new ArrayList<>(); for(int i = 0; i < N; i++){ arr.add(sc.nextInt()); } for(int i = 0; i < N; i++){ set.add(arr.get(i)); } boolean[] used = new boolean[N]; for(int v : arr){ int tmp = v; while(tmp > 1){ if(tmp % 2 == 1){ tmp >>= 1; if(set.contains(tmp)){ set.remove(v); break; } }else if(tmp % 4 == 0){ tmp >>= 2; if(set.contains(tmp)){ set.remove(v); break; } }else{ break; } } } long[] digit = new long[p+1]; for(int v : set){ int d = 0; while(v > 1){ v >>= 1; d++; } if(p > d){ digit[d]++; } } for(int i = 0; i < p; i++){ if(i+1 < p) digit[i+1] += digit[i]; if(i+2 < p) digit[i+2] += digit[i]; if(i+1 < p) digit[i+1] %= mod; if(i+2 < p) digit[i+2] %= mod; } for(int i = 0; i < p; i++){ digit[i+1] += digit[i]; digit[i+1] %= mod; } pw.println(digit[p-1]); } static class GeekInteger { public static void save_sort(int[] array) { shuffle(array); Arrays.sort(array); } public static int[] shuffle(int[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); int randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } public static void save_sort(long[] array) { shuffle(array); Arrays.sort(array); } public static long[] shuffle(long[] array) { int n = array.length; Random random = new Random(); for (int i = 0, j; i < n; i++) { j = i + random.nextInt(n - i); long randomElement = array[j]; array[j] = array[i]; array[i] = randomElement; } return array; } } } class Combination { final static long mod = (long) 1e9 + 7; private static long[] fact, ifact; public Combination(int n) { fact = new long[n + 1]; ifact = new long[n + 1]; fact[0] = 1; long ln = n; for (long i = 1; i <= ln; ++i) { int ii = (int) i; fact[ii] = fact[ii - 1] % mod * i % mod; } ifact[n] = pow(fact[n], this.mod - 2); for (int i = n; i >= 1; --i) { int ii = (int) i; ifact[ii - 1] = ifact[ii] % mod * i % mod; } } public static long comb(int n, int k) { if (k < 0 || k > n) return 0; return fact[n] % mod * ifact[k] % mod * ifact[n - k] % mod; } public static long perm(int n, int k) { return comb(n, k) * fact[k] % mod; } public static long pow(long a, long b) { long ret = 1; long tmp = a; while (b > 0) { if ((b & 1) == 1) { ret = (ret * tmp) % mod; } tmp = (tmp * tmp) % mod; b = b >> 1; } return ret; } } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); tokenizer = null; } public FastScanner(FileReader in) { reader = new BufferedReader(in); tokenizer = null; } public String next() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public String nextLine() { if (tokenizer == null || !tokenizer.hasMoreTokens()) { try { return reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken("\n"); } public long nextLong() { return Long.parseLong(next()); } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String[] nextArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) a[i] = next(); return a; } 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; } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
c2aa6a94af7d75cb75ec8cf6f6c17dac
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.*; import java.util.*; public class Main { static class Scanner { Scanner(InputStream in) { this.in = in; } InputStream in; byte[] bb = new byte[1 << 15]; int i, n; byte getc() { if (i == n) { i = n = 0; try { n = in.read(bb); } catch (IOException e) {} } return i < n ? bb[i++] : 0; } int nextInt() { byte c = 0; while (c <= ' ') c = getc(); boolean negative = false; if (c == '-') { negative = true; c = getc(); } int a = 0; while (c > ' ') { a = a * 10 + c - '0'; c = getc(); } if (negative) a = -a; return a; } long nextLong() { byte c = 0; while (c <= ' ') c = getc(); long a = 0; while (c > ' ') { a = a * 10 + c - '0'; c = getc(); } return a; } } private static int len(int num) { int length = 0; while (num > 0) { length++; num /= 2; } return length; } public static void main(String[] args) throws Exception { Scanner in = new Scanner(System.in); //BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int N = in.nextInt(); int P = in.nextInt(); int[] A = new int[N+1]; Set<Integer> present = new HashSet<>(3 * N); for (int i=1; i<=N; i++) { A[i] = in.nextInt(); present.add(A[i]); } int[] lengths = new int[31]; for (int i=1; i<=N; i++) { int num = A[i]; boolean contains = false; while (num > 0) { if (num % 2 == 1) { num /= 2; } else if (num % 4 == 0) { num /= 4; } else { break; } if (present.contains(num)) { contains = true; break; } } if (!contains) { //out.write(A[i] + " " + len(A[i]) + "\n"); lengths[len(A[i])]++; } } long[] dp = new long[P+1]; dp[1] = 1; dp[0] = 1; final int REM = 1000000007; for (int i=2; i<=P; i++) dp[i] = (dp[i-2] + dp[i-1]) % REM; long res = 0; //out.write("P = " + P + "\n"); for (int k=P; k>=1; k--) { //out.write("k-> " + k + "\n"); for (int l=1; l<=Math.min(30, P-k+1); l++) { //out.write("k, l -> " + k + " " + l + "\n"); res = res + (dp[P-k-l+1] * lengths[l] % REM); res %= REM; } } out.write(res + "\n"); //in.close(); out.close(); } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
df10a8e4adaf7bb9e88447e80d3014e4
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.*; import java.util.*; public class D { static long MOD = (long) (1e9+7); static HashSet<Integer> bt = new HashSet<Integer>(); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int p = Integer.parseInt(st.nextToken()); int[] ar = new int[n]; st = new StringTokenizer(br.readLine()); for(int i = 0; i < n; i++) { ar[i] = Integer.parseInt(st.nextToken()); bt.add(ar[i]); } for(int i = 0; i < n; i++) { int tv = ar[i]; while(tv > 2) { if(tv % 2 == 1) { tv = (tv-1)/2; }else if(tv % 4 == 0) { tv /= 4; }else break; if(bt.contains(tv)) { ar[i] = -1; break; } } } long[] dp = new long[p]; for(int i = 0; i < n; i++) { if(ar[i] == -1) continue; int cv = ar[i]; int ind = 0; while(cv > 1) { cv >>= 1; ind++; } if(ind < p) dp[ind]++; } for(int i = 0; i < p; i++) { if(i + 1 < p) dp[i+1] = (dp[i+1] + dp[i]) % MOD; if(i + 2 < p) dp[i+2] = (dp[i+2] + dp[i]) % MOD; } long sum = 0; for(long l : dp) sum = (sum + l) % MOD; pw.println(sum); pw.close(); } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
fc2ff0d5fa0daab6f9453ca7922c801c
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.*; import java.util.*; public class CF1635D extends PrintWriter { CF1635D() { super(System.out); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF1635D o = new CF1635D(); o.main(); o.flush(); } static final int MD = 1000000007; void main() { int n = sc.nextInt(); int p = sc.nextInt(); int[] aa = new int[n]; for (int i = 0; i < n; i++) aa[i] = sc.nextInt(); aa = Arrays.stream(aa).boxed().sorted().mapToInt($->$).toArray(); int[] dp = new int[p + 1]; dp[0] = dp[1] = 1; for (int k = 2; k <= p; k++) dp[k] = (dp[k - 1] + dp[k - 2]) % MD; for (int k = 1; k <= p; k++) dp[k] = (dp[k] + dp[k - 1]) % MD; HashSet<Integer> hs = new HashSet<>(); int ans = 0; out: for (int i = 0; i < n; i++) { for (int a = aa[i]; a > 0; ) { if (hs.contains(a)) continue out; if ((a & 1) == 1) a >>= 1; else if ((a & 3) == 0) a >>= 2; else break; } hs.add(aa[i]); int k = p; for (int a = aa[i]; a > 0; a >>= 1) k--; if (k < 0) break; ans = (ans + dp[k]) % MD; } println(ans); } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
d36878f660c2983ad865910e784a5335
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { static int MOD = 1000000007; // After writing solution, quick scan for: // array out of bounds // special cases e.g. n=1? // npe, particularly in maps // // Big numbers arithmetic bugs: // int overflow // if (x : long) and (y : int), [y = x] does not compile, but [y += x] does // sorting, or taking max, after MOD // // Interactive problems: don't forget to flush between test cases void solve() throws IOException { int[] np = ril(2); int n = np[0]; int p = np[1]; // interesting: p <= 2 * 10^5 int[] a = ril(n); sort(a); // It would be good to know the "minimal set" i.e. remove redundancies in initial // input, to prevent overcounting. // We can make original number // with any number of trailing 1s // and any number of 0s wherever, as long as they are consecutive 0s. Set<Integer> bases = new HashSet<>(); for (int ai : a) { int orig = ai; // Try chopping all suffixes of ai that have consecutive even 0s. // If the prefix is present in base, kill it. if (bases.contains(ai)) continue; if (p < 60 && orig >= (1l << p)) continue; boolean ok = true; while (ai > 0) { if (ai % 2 == 1) { ai /= 2; if (bases.contains(ai)) { ok = false; break; } continue; } else { // need 2 adjacent. ai /= 2; if (ai % 2 == 0) { ai /= 2; } else { break; } if (bases.contains(ai)) { ok = false; break; } } } if (ok) bases.add(orig); } long[] dp = new long[p+10]; dp[0] = 1; dp[1] = 1; dp[2] = 2; for (int i = 3; i < dp.length; i++) { dp[i] = dp[i-1] + dp[i-2]; if (dp[i] >= MOD) dp[i] -= MOD; } for (int i = 1; i < dp.length; i++) { dp[i] = dp[i-1] + dp[i]; if (dp[i] >= MOD) dp[i] -= MOD; } // pw.println(bases); long ans = 0; for (int base : bases) { int len = Integer.numberOfTrailingZeros(Integer.highestOneBit(base)) + 1; int upto = p - len; // pw.println(base + " " + len + " " + p); // pw.flush(); // Fix the length which is 0 <= i <= upto // How many ways to add 1s? ans = ans + dp[upto]; if (ans >= MOD) ans -= MOD; } pw.println(ans); } // IMPORTANT // DID YOU CHECK THE COMMON MISTAKES ABOVE? // Template code below BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); public static void main(String[] args) throws IOException { Main m = new Main(); m.solve(); m.close(); } void close() throws IOException { pw.flush(); pw.close(); br.close(); } int ri() throws IOException { return Integer.parseInt(br.readLine().trim()); } long rl() throws IOException { return Long.parseLong(br.readLine().trim()); } int[] ril(int n) throws IOException { int[] nums = new int[n]; int c = 0; for (int i = 0; i < n; i++) { int sign = 1; c = br.read(); int x = 0; if (c == '-') { sign = -1; c = br.read(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } nums[i] = x * sign; } while (c != '\n' && c != -1) c = br.read(); return nums; } long[] rll(int n) throws IOException { long[] nums = new long[n]; int c = 0; for (int i = 0; i < n; i++) { int sign = 1; c = br.read(); long x = 0; if (c == '-') { sign = -1; c = br.read(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } nums[i] = x * sign; } while (c != '\n' && c != -1) c = br.read(); return nums; } int[] rkil() throws IOException { int c = br.read(); int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } return ril(x); } long[] rkll() throws IOException { int c = br.read(); int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = br.read(); } return rll(x); } char[] rs() throws IOException { return br.readLine().toCharArray(); } void sort(int[] A) { Random r = new Random(); for (int i = A.length-1; i > 0; i--) { int j = r.nextInt(i+1); int temp = A[i]; A[i] = A[j]; A[j] = temp; } Arrays.sort(A); } void printDouble(double d) { pw.printf("%.16f", d); } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
d146a8a2f3d6c033ab1d70b2793d250f
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.HashSet; import java.util.Set; 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 null */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Input in = new Input(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { private void solve(Input in, PrintWriter out) throws Exception { int n = in.readInt(); int p = in.readInt(); Set<Integer> s = new HashSet<>(); for (int i = 0; i < n; i++) { s.add(in.readInt()); } long mod = 1000000007; long[] d = new long[p + 1]; d[0] = 1; d[1] = 1; for (int i = 2; i < d.length; i++) { d[i] = (d[i - 2] + d[i - 1]) % mod; } long[] sum = new long[p + 1]; sum[0] = d[0]; for (int i = 1; i < sum.length; i++) { sum[i] = (sum[i - 1] + d[i]) % mod; } long ans = 0; for (int x : s) { int t = x; boolean found = false; while (t > 0) { if ((t < x) && s.contains(t)) { found = true; break; } if (t % 2 == 1) { t = t / 2; } else if (t % 4 == 0) { t = t / 4; } else { break; } } if (!found) { t = x; int bits = 0; while (t > 0) { t >>= 1; bits++; } if (bits <= p) { ans = (ans + sum[p - bits]) % mod; } } } out.println(ans); } public void solve(int testNumber, Input in, PrintWriter out) { try { solve(in, out); } catch (Exception e) { throw new RuntimeException(e); } } } static class Input { public final BufferedReader reader; private String line = ""; private int pos = 0; public Input(InputStream inputStream) { reader = new BufferedReader(new InputStreamReader(inputStream)); } private boolean isSpace(char ch) { return ch <= 32; } public String readWord() throws IOException { skip(); int start = pos; while (pos < line.length() && !isSpace(line.charAt(pos))) { pos++; } return line.substring(start, pos); } public int readInt() throws IOException { return Integer.parseInt(readWord()); } private void skip() throws IOException { while (true) { if (pos >= line.length()) { line = reader.readLine(); pos = 0; } while (pos < line.length() && isSpace(line.charAt(pos))) { pos++; } if (pos < line.length()) { return; } } } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 11
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
3d38130533414d6dffd394a0f6fd3aad
train_108.jsonl
1645367700
You are given an array $$$a$$$ consisting of $$$n$$$ distinct positive integers.Let's consider an infinite integer set $$$S$$$ which contains all integers $$$x$$$ that satisfy at least one of the following conditions: $$$x = a_i$$$ for some $$$1 \leq i \leq n$$$. $$$x = 2y + 1$$$ and $$$y$$$ is in $$$S$$$. $$$x = 4y$$$ and $$$y$$$ is in $$$S$$$.For example, if $$$a = [1,2]$$$ then the $$$10$$$ smallest elements in $$$S$$$ will be $$$\{1,2,3,4,5,7,8,9,11,12\}$$$.Find the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Since this number may be too large, print it modulo $$$10^9 + 7$$$.
256 megabytes
import java.util.*; import java.io.*; public class C { static long mod = (long) 1e9 + 7; static boolean dfs(long n, HashSet<Long> set){ if(n == 1) return false; if(n%2 == 1){ if(set.contains(n/2)) return true; return dfs(n / 2, set); } if(n%4 == 0){ if(set.contains(n/4)) return true; return dfs(n/4, set); } return false; } static int countBitLength(long n){ String s = Long.toBinaryString(n); return s.length(); } public static void main(String[] args) throws IOException { Soumit sc = new Soumit(); int n = sc.nextInt(); int p = sc.nextInt(); long[] a = sc.nextLongArray(n); HashSet<Long> set = new HashSet<>(); for(int i=0;i<n;i++){ set.add(a[i]); } for(int i=0;i<n;i++){ long v = a[i]; if(dfs(v, set)){ set.remove(a[i]); } } long[] dp = new long[Math.max(31, p+10)]; for(long i: set){ int bitLength = countBitLength(i); if(bitLength <= 30) dp[bitLength]++; } for(int i=0;i<dp.length;i++){ if(i > 0){ dp[i] = (dp[i] + dp[i-1]) % mod; if(i > 1){ dp[i] = (dp[i] + dp[i-2]) % mod; } } } long ans = 0; for(int i=0;i<=p;i++){ ans = (ans + dp[i]) % mod; } System.out.println(ans); sc.close(); } static class Soumit { final private int BUFFER_SIZE = 1 << 18; final private DataInputStream din; final private byte[] buffer; private PrintWriter pw; private int bufferPointer, bytesRead; StringTokenizer st; public Soumit() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public Soumit(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public void streamOutput(String file) throws IOException { FileWriter fw = new FileWriter(file); BufferedWriter bw = new BufferedWriter(fw); pw = new PrintWriter(bw); } public void println(String a) { pw.println(a); } public void print(String a) { pw.print(a); } public String readLine() throws IOException { byte[] buf = new byte[3000064]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') break; buf[cnt++] = (byte) c; } return new String(buf, 0, cnt); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public void sort(int[] arr) { ArrayList<Integer> arlist = new ArrayList<>(); for (int i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public void sort(long[] arr) { ArrayList<Long> arlist = new ArrayList<>(); for (long i : arr) arlist.add(i); Collections.sort(arlist); for (int i = 0; i < arr.length; i++) arr[i] = arlist.get(i); } public int[] nextIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } public long[] nextLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } public double[] nextDoubleArray(int n) throws IOException { double[] arr = new double[n]; for (int i = 0; i < n; i++) { arr[i] = nextDouble(); } return arr; } 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;*/ if (din != null) din.close(); if (pw != null) pw.close(); } } }
Java
["2 4\n6 1", "4 7\n20 39 5 200", "2 200000\n48763 1000000000"]
2 seconds
["9", "14", "448201910"]
NoteIn the first example, the elements smaller than $$$2^4$$$ are $$$\{1, 3, 4, 6, 7, 9, 12, 13, 15\}$$$.In the second example, the elements smaller than $$$2^7$$$ are $$$\{5,11,20,23,39,41,44,47,79,80,83,89,92,95\}$$$.
Java 17
standard input
[ "bitmasks", "dp", "math", "matrices", "number theory", "strings" ]
fbb6d21b757d8d56396af1253ec9e719
The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 2 \cdot 10^5)$$$. The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9)$$$. It is guaranteed that all the numbers in $$$a$$$ are distinct.
1,800
Print a single integer, the number of elements in $$$S$$$ that are strictly smaller than $$$2^p$$$. Remember to print it modulo $$$10^9 + 7$$$.
standard output
PASSED
d55da6cc918d75668f6996877fec257c
train_108.jsonl
1645367700
There are $$$n$$$ weighted points on the $$$OX$$$-axis. The coordinate and the weight of the $$$i$$$-th point is $$$x_i$$$ and $$$w_i$$$, respectively. All points have distinct coordinates and positive weights. Also, $$$x_i &lt; x_{i + 1}$$$ holds for any $$$1 \leq i &lt; n$$$. The weighted distance between $$$i$$$-th point and $$$j$$$-th point is defined as $$$|x_i - x_j| \cdot (w_i + w_j)$$$, where $$$|val|$$$ denotes the absolute value of $$$val$$$.You should answer $$$q$$$ queries, where the $$$i$$$-th query asks the following: Find the minimum weighted distance among all pairs of distinct points among the points in subarray $$$[l_i,r_i]$$$.
256 megabytes
import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Map.Entry; import java.util.Random; import java.util.TreeSet; public final class CF_772_D2_F { static boolean verb=true; static void log(Object X){if (verb) System.err.println(X);} static void log(Object[] X){if (verb) {for (Object U:X) System.err.print(U+" ");System.err.println("");}} static void log(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(int[] X){if (verb) {for (int U:X) System.err.print(U+" ");}} static void log(int[] X,int L){if (verb) {for (int i=0;i<L;i++) System.err.print(X[i]+" ");System.err.println("");}} static void log(long[] X){if (verb) {for (long U:X) System.err.print(U+" ");System.err.println("");}} static void logWln(Object X){if (verb) System.err.print(X);} static void info(Object o){ System.out.println(o);} static void output(Object o){outputWln(""+o+"\n"); } static void outputFlush(Object o){try {out.write(""+ o+"\n");out.flush();} catch (Exception e) {}} static void outputWln(Object o){try {out.write(""+ o);} catch (Exception e) {}} // Global vars static BufferedWriter out; static InputReader reader; static void test() { log("testing"); Random r=new Random(); int NTESTS=1000000; int NMAX=20; int VMAX=10; int XMAX=10; int WMAX=10; for (int t=0;t<NTESTS;t++) { int n=r.nextInt(NMAX)+2; long[] x=new long[n]; long[] w=new long[n]; long cur=0; for (int i=0;i<n;i++) { cur+=r.nextInt(XMAX)+1; x[i]=cur; w[i]=r.nextInt(WMAX)+1; } int Q=(n*(n-1))/2; int[] u=new int[Q]; int[] v=new int[Q]; int st=0; for (int i=0;i+1<n;i++) for (int j=i+1;j<n;j++) { u[st]=i; v[st]=j; st++; } long[] ans1=solveBourrin(x,w,u,v); long[] ans2=solveSubtil(x,w,u,v); for (int e=0;e<Q;e++) { if (ans1[e]!=ans2[e]) { log("Error"); log(n+" "+Q); for (int i=0;i<n;i++) { log(x[i]+" "+w[i]); } for (int i=0;i<Q;i++) { log((u[i]+1)+" "+(v[i]+1)); } return; } } } log("testing done"); } static long MAXX=Long.MAX_VALUE; static long MINX=Long.MIN_VALUE; static class SegmentTree_MinMax_Long { long[] val; long[] min; long[] max; long[] mintmp; long[] maxtmp; int[] tmp; // ofsset is to add to the guys below // min and max already include the offset (no need to add it again) int TS; int N; public SegmentTree_MinMax_Long(int n){ N=n; TS=1; while (TS<N) TS*=2; //log("TS:"+TS+" N:"+N); min=new long[TS*2]; max=new long[TS*2]; mintmp=new long[TS*2]; maxtmp=new long[TS*2]; Arrays.fill(mintmp,MAXX); Arrays.fill(maxtmp,MINX); } public void insertTree(int a,long x){ insertTree(1,0,TS-1,a,x); } void insertTree(int cur,int l,int r,int a,long x){ if (a<l || a>r) return; if (l==r) { max[cur]=x; min[cur]=x; return; } int mid=(l+r)/2; insertTree(cur*2,l,mid,a,x); insertTree(cur*2+1,mid+1,r,a,x); long M=Math.max(max[cur*2],max[cur*2+1]); long m=Math.min(min[cur*2],min[cur*2+1]); min[cur]=m; max[cur]=M; } public long[] getMinMax(int a,int b) { getMinMax(1,0,TS-1,a,b); return new long[] {mintmp[1],maxtmp[1]}; } void getMinMax(int cur,int l,int r,int a,int b){ if (b<l || a>r) { //logWln("case 1 // get min // cur:"+cur+" l:"+l+" r:"+r+" a:"+a+" b:"+b+" // "); //log("return nothing"); mintmp[cur]=MAXX; maxtmp[cur]=MINX; return; } if (a<=l && b>=r) { //logWln("case 2 // get min // cur:"+cur+" l:"+l+" r:"+r+" a:"+a+" b:"+b+" // "); //log("return "+min[cur]+" "+max[cur]); mintmp[cur]=min[cur]; maxtmp[cur]=max[cur]; return; } int mid=(l+r)/2; getMinMax(cur*2,l,mid,a,b); getMinMax(cur*2+1,mid+1,r,a,b); mintmp[cur]=Math.min(mintmp[2*cur],mintmp[2*cur+1]); maxtmp[cur]=Math.max(maxtmp[2*cur],maxtmp[2*cur+1]); //logWln("case 3// get min // cur:"+cur+" l:"+l+" r:"+r+" a:"+a+" b:"+b+" // "); //log("return :"+u+" "+v); } } static class Composite implements Comparable<Composite>{ int x; int typ; int idx; public int compareTo(Composite X) { if (x!=X.x) return x-X.x; if (typ!=X.typ) return typ-X.typ; return idx-X.idx; } public Composite(int x, int typ, int idx) { this.x = x; this.typ = typ; this.idx = idx; } } static int STARTQUERRY=1; static int ENDPOINTL=2; static int ENDPOINTR=3; static int ENDQUERRY=4; static long[] solveBourrin(long[] x,long[] w,int[] u,int v[]) { int n=x.length; int Q=u.length; long[] ans=new long[Q]; for (int e=0;e<Q;e++) { long best=MAXX; for (int i=u[e];i<v[e];i++) for (int j=i+1;j<=v[e];j++) { best=Math.min(best, (x[j]-x[i])*(w[i]+w[j])); } ans[e]=best; } return ans; } static long[] solveSubtil(long[] x,long[] w,int[] u,int[] v) { int n=x.length; int Q=u.length; int[] L=new int[n]; int[] R=new int[n]; Arrays.fill(L, -1); Arrays.fill(R, n); int[] stack=new int[n]; int st=0; for (int i=0;i<n;i++) { long a=w[i]; while (st>0 && w[stack[st-1]]>=a) { int j=stack[st-1]; R[j]=i; st--; } stack[st++]=i; } st=0; for (int i=n-1;i>=0;i--) { long a=w[i]; while (st>0 && w[stack[st-1]]>=a) { int j=stack[st-1]; L[j]=i; st--; } stack[st++]=i; } long[] lb=new long[n]; long[] rb=new long[n]; Arrays.fill(lb,MAXX); Arrays.fill(rb,MAXX); for (int i=0;i<n;i++) { if (L[i]>=0) { lb[i]=(x[i]-x[L[i]])*(w[i]+w[L[i]]); } if (R[i]<n) { rb[i]=(x[R[i]]-x[i])*(w[i]+w[R[i]]); } } //log(lb); //log(rb); Composite[] ar=new Composite[2*n+Q*2]; st=0; for (int i=0;i<n;i++) { if (L[i]>=0) ar[st++]=new Composite(i,ENDPOINTL,i); if (R[i]<n) ar[st++]=new Composite(R[i],ENDPOINTR,i); } for (int e=0;e<Q;e++) { //ar[st++]=new Composite(u[e],STARTQUERRY,e); ar[st++]=new Composite(v[e],ENDQUERRY,e); } Arrays.sort(ar,0,st); long[] val=new long[n]; Arrays.fill(val, MAXX); SegmentTree_MinMax_Long tree=new SegmentTree_MinMax_Long(n); for (int i=0;i<n;i++) tree.insertTree(i,MAXX); long[] ans=new long[Q]; for (int t=0;t<st;t++) { Composite X=ar[t]; int i=X.idx; if (X.typ==ENDPOINTL) { //log("ENDPOINT L targeting "+L[i]); if (val[L[i]]>lb[i]) { val[L[i]]=lb[i]; tree.insertTree(L[i], lb[i]); //log("updating at :"+L[i]+" with :"+lb[i]); } } else if (X.typ==ENDPOINTR) { //log("ENDPOINT R targeting :"+i); if (val[i]>rb[i]) { tree.insertTree(i,rb[i]); val[i]=rb[i]; //log("updating at :"+i+" with :"+rb[i]); } } else if (X.typ==STARTQUERRY) { // something to do ?? } else if (X.typ==ENDQUERRY) { int src=u[i]; //log("src:"+src); long[] tm=tree.getMinMax(src, n-1); //log("=========== val"); //log(val); ans[i]=tm[0]; //log(ans[i]); } } return ans; } static long mod=1000000007; static void process() throws Exception { out = new BufferedWriter(new OutputStreamWriter(System.out)); reader = new InputReader(System.in); //test(); int n=reader.readInt(); int Q=reader.readInt(); long[] x=new long[n]; long[] w=new long[n]; for (int i=0;i<n;i++) { x[i]=reader.readInt(); w[i]=reader.readInt(); } int[] u=new int[Q]; int[] v=new int[Q]; for (int e=0;e<Q;e++) { u[e]=reader.readInt()-1; v[e]=reader.readInt()-1; } long[] ans=solveSubtil(x,w,u,v); StringBuffer sb=new StringBuffer(); for (long xx:ans) sb.append(xx+" "); output(sb); //log(solveSubtil(x,w,u,v)); //log(solveBourrin(x,w,u,v)); try { out.close(); } catch (Exception Ex) { } } public static void main(String[] args) throws Exception { process(); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final String readString() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.append((char) c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final String readString(int L) throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(L); do { res.append((char) c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public final int readInt() throws IOException { int c = read(); boolean neg = false; while (isSpaceChar(c)) { c = read(); } char d = (char) c; // log("d:"+d); if (d == '-') { neg = true; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); // log("res:"+res); if (neg) return -res; return res; } public final long readLong() throws IOException { int c = read(); boolean neg = false; while (isSpaceChar(c)) { c = read(); } char d = (char) c; // log("d:"+d); if (d == '-') { neg = true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); // log("res:"+res); if (neg) return -res; return res; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } } /* 3 3 6 2 7 3 13 3 1 2 1 3 2 3 4 6 1 6 10 6 19 8 24 8 1 2 1 3 1 4 2 3 2 4 3 4 */
Java
["5 5\n-2 2\n0 10\n1 1\n9 2\n12 7\n1 3\n2 3\n1 5\n3 5\n2 4"]
3 seconds
["9\n11\n9\n24\n11"]
NoteFor the first query, the minimum weighted distance is between points $$$1$$$ and $$$3$$$, which is equal to $$$|x_1 - x_3| \cdot (w_1 + w_3) = |-2 - 1| \cdot (2 + 1) = 9$$$.For the second query, the minimum weighted distance is between points $$$2$$$ and $$$3$$$, which is equal to $$$|x_2 - x_3| \cdot (w_2 + w_3) = |0 - 1| \cdot (10 + 1) = 11$$$.For the fourth query, the minimum weighted distance is between points $$$3$$$ and $$$4$$$, which is equal to $$$|x_3 - x_4| \cdot (w_3 + w_4) = |1 - 9| \cdot (1 + 2) = 24$$$.
Java 8
standard input
[ "data structures", "greedy" ]
046d0c0c73aad1a5dc295af1a43e0fc6
The first line contains 2 integers $$$n$$$ and $$$q$$$ $$$(2 \leq n \leq 3 \cdot 10^5; 1 \leq q \leq 3 \cdot 10^5)$$$ — the number of points and the number of queries. Then, $$$n$$$ lines follows, the $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$w_i$$$ $$$(-10^9 \leq x_i \leq 10^9; 1 \leq w_i \leq 10^9)$$$ — the coordinate and the weight of the $$$i$$$-th point. It is guaranteed that the points are given in the increasing order of $$$x$$$. Then, $$$q$$$ lines follows, the $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 \leq l_i &lt; r_i \leq n)$$$ — the given subarray of the $$$i$$$-th query.
2,800
For each query output one integer, the minimum weighted distance among all pair of distinct points in the given subarray.
standard output
PASSED
aa183aa7e73d73295f80a8efb8b35716
train_108.jsonl
1645367700
There are $$$n$$$ weighted points on the $$$OX$$$-axis. The coordinate and the weight of the $$$i$$$-th point is $$$x_i$$$ and $$$w_i$$$, respectively. All points have distinct coordinates and positive weights. Also, $$$x_i &lt; x_{i + 1}$$$ holds for any $$$1 \leq i &lt; n$$$. The weighted distance between $$$i$$$-th point and $$$j$$$-th point is defined as $$$|x_i - x_j| \cdot (w_i + w_j)$$$, where $$$|val|$$$ denotes the absolute value of $$$val$$$.You should answer $$$q$$$ queries, where the $$$i$$$-th query asks the following: Find the minimum weighted distance among all pairs of distinct points among the points in subarray $$$[l_i,r_i]$$$.
256 megabytes
// package c1635; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeSet; // // Codeforces Round #772 (Div. 2) 2022-02-20 06:35 // F. Closest Pair // https://codeforces.com/contest/1635/problem/F // time limit per test 3 seconds; memory limit per test 256 megabytes // public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*' // // There are n weighted points on the OX-axis. The coordinate and the weight of the i-th point is // x_i and w_i, respectively. All points have distinct coordinates and positive weights. Also, x_i < // x_{i + 1} holds for any 1 <= i < n. // // The weighted distance between i-th point and j-th point is defined as |x_i - x_j| * (w_i + w_j), // where |val| denotes the absolute value of val. // // You should answer q queries, where the i-th query asks the following: Find the weighted distance // among all pairs of distinct points among the points in subarray [l_i,r_i]. // // Input // // The first line contains 2 integers n and q (2 <= n <= 3 * 10^5; 1 <= q <= 3 * 10^5) -- the number // of points and the number of queries. // // Then, n lines follows, the i-th of them contains two integers x_i and w_i (-10^9 <= x_i <= 10^9; // 1 <= w_i <= 10^9) -- the coordinate and the weight of the i-th point. // // It is guaranteed that the points are given in the increasing order of x. // // Then, q lines follows, the i-th of them contains two integers l_i and r_i (1 <= l_i < r_i <= n) // -- the given subarray of the i-th query. // // Output // // For each query output one integer, the weighted distance among all pair of distinct points in the // given subarray. // // Example /* input: 5 5 -2 2 0 10 1 1 9 2 12 7 1 3 2 3 1 5 3 5 2 4 output: 9 11 9 24 11 */ // Note // // For the first query, the minimum weighted distance is between points 1 and 3, which is equal to // |x_1 - x_3| * (w_1 + w_3) = |-2 - 1| * (2 + 1) = 9. // // For the second query, the minimum weighted distance is between points 2 and 3, which is equal to // |x_2 - x_3| * (w_2 + w_3) = |0 - 1| * (10 + 1) = 11. // // For the fourth query, the minimum weighted distance is between points 3 and 4, which is equal to // |x_3 - x_4| * (w_3 + w_4) = |9 - 12| * (2 + 7) = 24. // public class C1635F { static final int MOD = 998244353; static final Random RAND = new Random(); static long[] solve(int[][] points, int[][] queries) { int n = points.length; int q = queries.length; TailPrefixMinTree tpm = new TailPrefixMinTree(n); List<List<int[]>> qa = new ArrayList<>(); for (int i = 0; i < n; i++) { qa.add(new ArrayList<>()); } for (int i = 0; i < q; i++) { int l = queries[i][0]; int r = queries[i][1]; qa.get(l).add(new int[] {r, i}); } List<List<Integer>> ra = new ArrayList<>(); for (int i = 0; i < n; i++) { ra.add(new ArrayList<>()); } // stack of index based on array and the index of its top (0 indicates empty). int[] s = new int[n]; int it = 0; for (int i = 0; i < n; i++) { while (it >= 1 && points[s[it]][1] >= points[i][1]) { ra.get(s[it]).add(i); it--; } ra.get(s[it]).add(i); s[++it] = i; // System.out.format(" i:%d it:%d ra:%s\n", i, it, Utils.traceListListInt(ra)); } // System.out.format(" ra:%s\n", Utils.traceListListInt(ra)); long[] ans = new long[q]; for (int i = n - 2; i >= 0; i--) { tpm.merge(i + 1, getwd(points, i, i+1)); for (int j : ra.get(i)) { if (j != i) { tpm.merge(j, getwd(points, i, j)); } } for (int[] e : qa.get(i)) { int r = e[0]; int idx = e[1]; ans[idx] = tpm.getTailPrefixMin(r); // System.out.format(" ans[%d]=%d\n", y, ans[y]); } } return ans; } static class TailPrefixMinTree { int n; long[] f; public TailPrefixMinTree(int n) { this.n = n; this.f = new long[n]; Arrays.fill(f, Long.MAX_VALUE); } // merge a new candidate value of v at index x public void merge(int x, long v) { for (; x < n; x += (-x & x)) { f[x] = Math.min(f[x], v); if (x == 0) { break; } } } // Get minimum value in prefix [0,x] public long getTailPrefixMin(int x) { long v = Math.min(Long.MAX_VALUE, f[0]); while (x != 0) { v = Math.min(v, f[x]); // subtract the lowest set bit from x x -= (-x & x); } return v; } } static long getwd(int[][] points, int i, int j) { myAssert(i < j); return (long)(points[j][0] - points[i][0]) * (points[i][1] + points[j][1]); } static long getwd(int xi, int wi, int xj, int wj) { myAssert(xi < xj); return (long)(xj - xi) * (wi + wj); } static long[] solveNaive(int[][] points, int[][] queries) { int q = queries.length; long[] ans = new long[q]; Arrays.fill(ans, Long.MAX_VALUE); for (int i = 0; i < q; i++) { int l = queries[i][0]; int r = queries[i][1]; for (int j = l; j < r; j++) { for (int k = j + 1; k <= r; k++) { ans[i] = Math.min(ans[i], (long)(points[k][0] - points[j][0]) * (points[k][1] + points[j][1])); } } } return ans; } public static String trace(int[][] points) { StringBuilder sb = new StringBuilder(); sb.append('{'); int h = 6; int m = points.length; for (int i = 0; i < m; i++) { if (m > (h << 1)) { if (i == h) { sb.append(",..."); } if (i >= h && i < m - h) { continue; } } if (i > 0) { sb.append(','); } sb.append('{'); int n = points[i].length; for (int j = 0; j < n; j++) { // If more than 32 cols, we trace the first 16 and last 16 if (n > (h << 1)) { if (j == h) { sb.append(",..."); } if (j >= h && j < n - h) { continue; } } if (j > 0) { sb.append(','); } sb.append(points[i][j]); } sb.append('}'); } sb.append('}'); return sb.toString(); } public static String trace(long[] values) { StringBuilder sb = new StringBuilder(); sb.append('{'); int h = 20; int m = values.length; for (int i = 0; i < m; i++) { if (m > (h << 1)) { if (i == h) { sb.append(",..."); } if (i >= h && i < m - h) { continue; } } if (i > 0) { sb.append(','); } sb.append(values[i]); } sb.append('}'); return sb.toString(); } static void test(int[][] points, int[][] queries) { int n = points.length; int q = queries.length; System.out.format(" points: %s\n", trace(points)); System.out.format("queries: %s\n", trace(queries)); long[] ans = solve(points, queries); long[] exp = n <= 1000 ? solveNaive(points, queries) : ans; System.out.format(" ans: %s\n", trace(ans)); for (int i = 0; i < q; i++) { if (ans[i] != exp[i]) { System.out.format(" exp: %s\n", trace(exp)); System.out.format(" i:%d [%d,%d] ans:%d exp:%d\n", i, queries[i][0], queries[i][1], ans[i], exp[i]); } myAssert(ans[i] == exp[i]); } } static boolean test = false; static void doTest() { if (!test) { return; } long t0 = System.currentTimeMillis(); for (int t = 0; t < 1; t++) { int n = 10; int q = 10; int xmax = 9; int wmax = 9; int[][] points = new int[n][2]; TreeSet<Integer> coordinates = new TreeSet<>(); while (coordinates.size() < n) { coordinates.add(RAND.nextInt(xmax * 2) - xmax); } List<Integer> xs = new ArrayList<>(coordinates); for (int i = 0; i < n; i++) { points[i][0] = xs.get(i); points[i][1] = 1 + RAND.nextInt(wmax); } int[][] queries = new int[q][2]; for (int i = 0; i < q; i++) { int u = RAND.nextInt(n); int v = u; while (v == u) { v = RAND.nextInt(n); } queries[i][0] = Math.min(u, v); queries[i][1] = Math.max(u, v); } points = new int[][] {{-9,3},{-8,9},{-3,3},{-2,4},{0,1},{2,6},{3,2},{4,7},{5,4},{6,6}}; // queries = new int[][] {{1,8},{2,8},{0,3},{5,8},{0,6},{1,2},{1,7},{6,8},{4,5},{0,5}}; test(points, queries); } System.out.format("%d msec\n", System.currentTimeMillis() - t0); System.exit(0); } public static void main(String[] args) { doTest(); MyScanner in = new MyScanner(); int n = in.nextInt(); int q = in.nextInt(); int[][] points = new int[n][2]; for (int i = 0; i < n; i++) { points[i][0] = in.nextInt(); points[i][1] = in.nextInt(); } int[][] queries = new int[q][2]; for (int i = 0; i < q; i++) { queries[i][0] = in.nextInt() - 1; queries[i][1] = in.nextInt() - 1; } long[] ans = solve(points, queries); output(ans); } static void output(long[] ans) { StringBuilder sb = new StringBuilder(); for (long v : ans) { sb.append(v); sb.append('\n'); if (sb.length() > 500) { System.out.print(sb.toString()); sb.setLength(0); } } System.out.print(sb.toString()); } static void myAssert(boolean cond) { if (!cond) { throw new RuntimeException("Unexpected"); } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName(); cname = cname.lastIndexOf('.') > 0 ? cname.substring(0, cname.lastIndexOf('.')) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["5 5\n-2 2\n0 10\n1 1\n9 2\n12 7\n1 3\n2 3\n1 5\n3 5\n2 4"]
3 seconds
["9\n11\n9\n24\n11"]
NoteFor the first query, the minimum weighted distance is between points $$$1$$$ and $$$3$$$, which is equal to $$$|x_1 - x_3| \cdot (w_1 + w_3) = |-2 - 1| \cdot (2 + 1) = 9$$$.For the second query, the minimum weighted distance is between points $$$2$$$ and $$$3$$$, which is equal to $$$|x_2 - x_3| \cdot (w_2 + w_3) = |0 - 1| \cdot (10 + 1) = 11$$$.For the fourth query, the minimum weighted distance is between points $$$3$$$ and $$$4$$$, which is equal to $$$|x_3 - x_4| \cdot (w_3 + w_4) = |1 - 9| \cdot (1 + 2) = 24$$$.
Java 11
standard input
[ "data structures", "greedy" ]
046d0c0c73aad1a5dc295af1a43e0fc6
The first line contains 2 integers $$$n$$$ and $$$q$$$ $$$(2 \leq n \leq 3 \cdot 10^5; 1 \leq q \leq 3 \cdot 10^5)$$$ — the number of points and the number of queries. Then, $$$n$$$ lines follows, the $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$w_i$$$ $$$(-10^9 \leq x_i \leq 10^9; 1 \leq w_i \leq 10^9)$$$ — the coordinate and the weight of the $$$i$$$-th point. It is guaranteed that the points are given in the increasing order of $$$x$$$. Then, $$$q$$$ lines follows, the $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 \leq l_i &lt; r_i \leq n)$$$ — the given subarray of the $$$i$$$-th query.
2,800
For each query output one integer, the minimum weighted distance among all pair of distinct points in the given subarray.
standard output
PASSED
33ba097980cbd8ad310289e7853132e1
train_108.jsonl
1645367700
There are $$$n$$$ weighted points on the $$$OX$$$-axis. The coordinate and the weight of the $$$i$$$-th point is $$$x_i$$$ and $$$w_i$$$, respectively. All points have distinct coordinates and positive weights. Also, $$$x_i &lt; x_{i + 1}$$$ holds for any $$$1 \leq i &lt; n$$$. The weighted distance between $$$i$$$-th point and $$$j$$$-th point is defined as $$$|x_i - x_j| \cdot (w_i + w_j)$$$, where $$$|val|$$$ denotes the absolute value of $$$val$$$.You should answer $$$q$$$ queries, where the $$$i$$$-th query asks the following: Find the minimum weighted distance among all pairs of distinct points among the points in subarray $$$[l_i,r_i]$$$.
256 megabytes
// package c1635; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; import java.lang.invoke.MethodHandles; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; import java.util.StringTokenizer; import java.util.TreeSet; // // Codeforces Round #772 (Div. 2) 2022-02-20 06:35 // F. Closest Pair // https://codeforces.com/contest/1635/problem/F // time limit per test 3 seconds; memory limit per test 256 megabytes // public class Pseudo for 'Source should satisfy regex [^{}]*public\s+(final)?\s*class\s+(\w+).*' // // There are n weighted points on the OX-axis. The coordinate and the weight of the i-th point is // x_i and w_i, respectively. All points have distinct coordinates and positive weights. Also, x_i < // x_{i + 1} holds for any 1 <= i < n. // // The weighted distance between i-th point and j-th point is defined as |x_i - x_j| * (w_i + w_j), // where |val| denotes the absolute value of val. // // You should answer q queries, where the i-th query asks the following: Find the weighted distance // among all pairs of distinct points among the points in subarray [l_i,r_i]. // // Input // // The first line contains 2 integers n and q (2 <= n <= 3 * 10^5; 1 <= q <= 3 * 10^5) -- the number // of points and the number of queries. // // Then, n lines follows, the i-th of them contains two integers x_i and w_i (-10^9 <= x_i <= 10^9; // 1 <= w_i <= 10^9) -- the coordinate and the weight of the i-th point. // // It is guaranteed that the points are given in the increasing order of x. // // Then, q lines follows, the i-th of them contains two integers l_i and r_i (1 <= l_i < r_i <= n) // -- the given subarray of the i-th query. // // Output // // For each query output one integer, the weighted distance among all pair of distinct points in the // given subarray. // // Example /* input: 5 5 -2 2 0 10 1 1 9 2 12 7 1 3 2 3 1 5 3 5 2 4 output: 9 11 9 24 11 */ // Note // // For the first query, the minimum weighted distance is between points 1 and 3, which is equal to // |x_1 - x_3| * (w_1 + w_3) = |-2 - 1| * (2 + 1) = 9. // // For the second query, the minimum weighted distance is between points 2 and 3, which is equal to // |x_2 - x_3| * (w_2 + w_3) = |0 - 1| * (10 + 1) = 11. // // For the fourth query, the minimum weighted distance is between points 3 and 4, which is equal to // |x_3 - x_4| * (w_3 + w_4) = |9 - 12| * (2 + 7) = 24. // public class C1635F { static final int MOD = 998244353; static final Random RAND = new Random(); /* int i,j,k,n,m,t,s[300500],it; ll a[300500],b[300500],f[300500],res[300500]; vector<int> r[300500]; vector<pair<int,int> >q[300500]; void add(int x,ll y){for(;x<=n;x+=(-x&x)){f[x]=min(f[x],y);}} ll get(int x,ll y=5e18){for(;x;x-=(-x&x)){y=min(y,f[x]);}return y;} void Add(int x,int y){add(y,(b[x]+b[y])*(a[y]-a[x]));} int main() { ios::sync_with_stdio(0); cin>>n>>t; for(i=1;i<=n;i++){cin>>a[i]>>b[i];f[i]=5e18;} for(i=1;i<=t;i++){int l,r;cin>>l>>r;q[l].push_back({r,i});} for(i=1;i<=n;i++){ while(it&&b[s[it]]>=b[i]){r[s[it]].push_back(i);it--;} r[s[it]].push_back(i); s[++it]=i; } for(i=n-1;i>=1;i--){ Add(i,i+1); for(auto j:r[i])Add(i,j); for(auto [x,y]:q[i])res[y]=get(x); } for(i=1;i<=t;i++)cout<<res[i]<<'\n'; } */ static long[] solve(int[][] points, int[][] queries) { int n = points.length; int q = queries.length; long[] f = new long[n]; Arrays.fill(f, (long)5e18); List<List<int[]>> qa = new ArrayList<>(); for (int i = 0; i < n; i++) { qa.add(new ArrayList<>()); } for (int i = 0; i < q; i++) { int l = queries[i][0]; int r = queries[i][1]; qa.get(l).add(new int[] {r, i}); } int[] s = new int[n]; int it = 0; List<List<Integer>> ra = new ArrayList<>(); for (int i = 0; i < n; i++) { ra.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { while (it >= 1 && points[s[it]][1] >= points[i][1]) { ra.get(s[it]).add(i); it--; } ra.get(s[it]).add(i); s[++it] = i; } long[] ans = new long[q]; for (int i = n - 2; i >= 0; i--) { Add(i, i + 1, points, f); for (int j : ra.get(i)) { Add(i, j, points, f); } for (int[] e : qa.get(i)) { int x = e[0]; int y = e[1]; ans[y] = get(x, (long)5e18, f); // System.out.format(" ans[%d]=%d\n", y, ans[y]); } } return ans; } static void add(int x, long y, long[] f) { int n = f.length; for(; x < n; x += (-x&x)) { f[x] = Math.min(f[x], y); if (x == 0) break; } } static long get(int x, long y, long[] f) { while (true) { y = Math.min(y,f[x]); x -= (-x & x); if (x == 0) { break; } } return y; } static void Add(int x, int y, int[][] points, long[] f) { add(y, ((long)(points[x][1] + points[y][1])) * (points[y][0] - points[x][0]), f); } // Time limit exceeded on test 3 static long[] solveA(int[][] points, int[][] queries) { int n = points.length; int q = queries.length; int m = getRank(n); if (test) System.out.format(" n:%d r:%d\n", n, m); List<RangeMinSegmentTree> sts = new ArrayList<>(); sts.add(null); for (int i = 1; i <= m; i++) { sts.add(new RangeMinSegmentTree(n)); } RangeMinIndexSegmentTree st0 = new RangeMinIndexSegmentTree(n); // rank 0, weight itself for (int i = 0; i < n; i++) { st0.set(i, points[i][1]); } // rank 1, two consecutive neighbors RangeMinSegmentTree st1 = sts.get(1); for (int i = 0; i < n - 1; i++) { st1.set(i, getwd(points, i, i + 1)); } long idx = 0; for (int r = 2; r <= m; r++) { int len = 1 << r; RangeMinSegmentTree stc = sts.get(r); int end = n - (1 << r); for (int i = 0; i <= end; i++) { idx++; stc.set(i, getRangeMin(i, i + len - 1, true, points, sts, st0)); if (idx % 100000 == 0) { if (test) System.out.format(" r:%2d idx:%d\n", r, idx); } } } if (test) System.out.format(" *************************\n"); long[] ans = new long[q]; for (int i = 0; i < q; i++) { int l = queries[i][0]; int r = queries[i][1]; ans[i] = getRangeMin(l, r, false, points, sts, st0); } if (test) { System.out.format("total loop %d\n", loop); } return ans; } static int loop = 0; static int[] mine = new int[3]; static long getRangeMin(int l, int r, boolean prepare, int[][] points, List<RangeMinSegmentTree> sts, RangeMinIndexSegmentTree st0) { int rank = getRank(r - l + 1); int h = 1 << rank; int e = r - l + 1 - h; if (prepare) { myAssert(e == 0); rank--; h = 1 << rank; e = h; } RangeMinSegmentTree stc = sts.get(rank); // Example of l = 0; r = 13, rank = 3, h = 8, e = 6 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 // * * * * * * * * * * * * * * // ^ ^ // l r // 0: --------------- // 1: --------------- // 2: --------------- // 3: --------------- // 4: --------------- // 5: --------------- // 6: --------------- // rank r range at i is for: [i, i + 2 * h -1] long value = stc.getRangeMin(l, l + e); st0.getRangeMinIndex(l, r, mine); int i0 = -1; if (mine[1] > l && mine[1] < r) { i0 = mine[1]; } else if (mine[2] > l && mine[2] < r) { i0 = mine[2]; } if (i0 >= l + e && i0 <= l + h) { // minimal range won't across the minimal index i0 } else { for (int j = l; j < l + e; j++) { int minw = st0.getRangeMin(j + h, r); long minv = getwd(points[j][0], points[j][1], points[j+h][0], minw); if (minv >= value) { continue; } loop++; boolean reduced = false; for (int k = j + h; k <= r; k++) { long z = getwd(points, j, k); if (z < value) { value = z; reduced = true; } } if (test) { System.out.format(" loop %4d r:%2d i:%5d j:%5d minw:%9d minv:%17d ans:%17d %s\n", loop, rank, l, j, minw, minv, value, reduced ? "reduced" : ""); } } } return value; } static long getwd(int[][] points, int i, int j) { myAssert(i < j); return (long)(points[j][0] - points[i][0]) * (points[i][1] + points[j][1]); } static long getwd(int xi, int wi, int xj, int wj) { myAssert(xi < xj); return (long)(xj - xi) * (wi + wj); } // Get largest m such that 2^m <= v static int getRank(int v) { int m = 0; int w = 1; while (w * 2 <= v) { w *= 2; m++; } return m; } static class RangeMinSegmentTree { int m; long[] arr; public RangeMinSegmentTree(int n) { // smallest power of 2 >= n this.m = 1; while (m < n) { m <<= 1; } arr = new long[m * 2]; } public RangeMinSegmentTree(long[] nums) { this(nums.length); for (int i = 0; i < nums.length; i++) { set(i, nums[i]); } } public long get(int index) { return arr[m + index]; } public void set(int index, long value) { int i = m + index; arr[i] = value; i /= 2; while (i > 0) { arr[i] = Math.min(arr[2 * i], arr[2 * i + 1]); i /= 2; } } public void add(int index, long increment) { set(index, get(index) + increment); } public long getRangeMin(int b, int e) { if (b == e) { return arr[m + b]; } int ib = m + b; int ie = m + e; long v = Math.min(arr[ib], arr[ie]); while (ib / 2 != ie / 2) { if (ib % 2 == 0) { // If ib is left child of its parent, include the value of sibling v = Math.min(v, arr[ib + 1]); } if (ie % 2 == 1) { // If ie is right child of its parent, use the value on parent v = Math.min(v, arr[ie - 1]); } ib /= 2; ie /= 2; } return v; } } static class RangeMinIndexSegmentTree { int m; int[] arr; int[] ila; int[] ira; public RangeMinIndexSegmentTree(int n) { // smallest power of 2 >= n this.m = 1; while (m < n) { m <<= 1; } arr = new int[m * 2]; ila = new int[m * 2]; ira = new int[m * 2]; Arrays.fill(arr, Integer.MAX_VALUE); } public RangeMinIndexSegmentTree(int[] nums) { this(nums.length); for (int i = 0; i < nums.length; i++) { set(i, nums[i]); } } public int get(int index) { return arr[m + index]; } public void set(int index, int value) { int i = m + index; arr[i] = value; ila[i] = index; ira[i] = index; i /= 2; while (i > 0) { int v = Math.min(arr[2 * i], arr[2 * i + 1]); if (v == arr[2 * i] && v == arr[2 * i + 1]) { ila[i] = ila[2 * i]; ira[i] = ira[2 * i + 1]; } else if (v == arr[2 * i]) { ila[i] = ila[2 * i]; ira[i] = ira[2 * i]; } else { ila[i] = ila[2 * i + 1]; ira[i] = ira[2 * i + 1]; } arr[i] = v; i /= 2; } } public void add(int index, int increment) { set(index, get(index) + increment); } // Get the minimum value and the first and last indexes of such value in [b,e]. // // 1 // 2 3 // 4 5 6 7 // * * * * * * * * // * * * * * * * * * * * * * * * * // ^ ^ // b e public int getRangeMinIndex(int b, int e, int[] output) { myAssert(b <= e); output[0] = arr[m + b]; output[0] = b; output[1] = e; if (b == e) { return output[0]; } int ib = m + b; int ie = m + e; int il = b; int ir = e; int v = Math.min(arr[ib], arr[ie]); if (v == arr[ib] && v != arr[ie]) { ir = b; } else if (v != arr[ib] && v == arr[ie]) { il = e; } int enter = 0; while (ib / 2 != ie / 2) { myAssert(++enter < 30); if (ib % 2 == 0) { // If ib is left child of its parent, include the value of its right sibling if (arr[ib + 1] < v) { v = arr[ib + 1]; il = ila[ib + 1]; ir = ira[ib + 1]; } else if (arr[ib + 1] == v) { ir = Math.max(ir, ira[ib + 1]); il = Math.min(il, ila[ib + 1]); } } if (ie % 2 == 1) { // If ie is right child of its parent, include the value of its left sibling if (arr[ie - 1] < v) { v = arr[ie - 1]; il = ila[ie - 1]; ir = ira[ie - 1]; } else if (arr[ie - 1] == v) { il = Math.min(il, ila[ie - 1]); ir = Math.max(ir, ira[ie - 1]); } } ib /= 2; ie /= 2; } output[0] = v; output[1] = il; output[2] = ir; return v; } public int getRangeMin(int b, int e) { myAssert(b <= e); if (b == e) { return arr[m + b]; } int ib = m + b; int ie = m + e; int v = Math.min(arr[ib], arr[ie]); int enter = 0; while (ib / 2 != ie / 2) { myAssert(++enter < 30); if (ib % 2 == 0) { // If ib is left child of its parent, include the value of its right sibling v = Math.min(v, arr[ib + 1]); } if (ie % 2 == 1) { // If ie is right child of its parent, include the value of its left sibling v = Math.min(v, arr[ie - 1]); } ib /= 2; ie /= 2; } return v; } } static long[] solveNaive(int[][] points, int[][] queries) { int q = queries.length; long[] ans = new long[q]; Arrays.fill(ans, Long.MAX_VALUE); for (int i = 0; i < q; i++) { int l = queries[i][0]; int r = queries[i][1]; for (int j = l; j < r; j++) { for (int k = j + 1; k <= r; k++) { ans[i] = Math.min(ans[i], (long)(points[k][0] - points[j][0]) * (points[k][1] + points[j][1])); } } } return ans; } public static String trace(int[][] points) { StringBuilder sb = new StringBuilder(); sb.append('{'); int h = 6; int m = points.length; for (int i = 0; i < m; i++) { if (m > (h << 1)) { if (i == h) { sb.append(",..."); } if (i >= h && i < m - h) { continue; } } if (i > 0) { sb.append(','); } sb.append('{'); int n = points[i].length; for (int j = 0; j < n; j++) { // If more than 32 cols, we trace the first 16 and last 16 if (n > (h << 1)) { if (j == h) { sb.append(",..."); } if (j >= h && j < n - h) { continue; } } if (j > 0) { sb.append(','); } sb.append(points[i][j]); } sb.append('}'); } sb.append('}'); return sb.toString(); } public static String trace(long[] values) { StringBuilder sb = new StringBuilder(); sb.append('{'); int h = 20; int m = values.length; for (int i = 0; i < m; i++) { if (m > (h << 1)) { if (i == h) { sb.append(",..."); } if (i >= h && i < m - h) { continue; } } if (i > 0) { sb.append(','); } sb.append(values[i]); } sb.append('}'); return sb.toString(); } static void test(int[][] points, int[][] queries) { int n = points.length; int q = queries.length; System.out.format(" points: %s\n", trace(points)); System.out.format("queries: %s\n", trace(queries)); long[] ans = solve(points, queries); long[] exp = n <= 1000 ? solveNaive(points, queries) : ans; System.out.format(" ans: %s\n", trace(ans)); for (int i = 0; i < q; i++) { if (ans[i] != exp[i]) { System.out.format(" exp: %s\n", trace(exp)); System.out.format(" i:%d [%d,%d] ans:%d exp:%d\n", i, queries[i][0], queries[i][1], ans[i], exp[i]); } myAssert(ans[i] == exp[i]); } } static boolean test = false; static void doTest() { if (!test) { return; } long t0 = System.currentTimeMillis(); for (int t = 0; t < 1; t++) { int n = 300000; int q = 300000; int xmax = 1000000000; int wmax = 1000000000; int[][] points = new int[n][2]; TreeSet<Integer> coordinates = new TreeSet<>(); while (coordinates.size() < n) { coordinates.add(RAND.nextInt(xmax * 2) - xmax); } List<Integer> xs = new ArrayList<>(coordinates); for (int i = 0; i < n; i++) { points[i][0] = xs.get(i); points[i][1] = 1 + RAND.nextInt(wmax); } int[][] queries = new int[q][2]; for (int i = 0; i < q; i++) { int u = RAND.nextInt(n); int v = u; while (v == u) { v = RAND.nextInt(n); } queries[i][0] = Math.min(u, v); queries[i][1] = Math.max(u, v); } // points = new int[][] {{-87,48},{-79,38},{-21,96},{-14,2},{0,29},{17,8},{42,53},{80,50},{85,51},{93,6}}; // queries = new int[][] {{1,8},{2,8},{0,3},{5,8},{0,6},{1,2},{1,7},{6,8},{4,5},{0,5}}; test(points, queries); } System.out.format("%d msec\n", System.currentTimeMillis() - t0); System.exit(0); } public static void main(String[] args) { doTest(); MyScanner in = new MyScanner(); int n = in.nextInt(); int q = in.nextInt(); int[][] points = new int[n][2]; for (int i = 0; i < n; i++) { points[i][0] = in.nextInt(); points[i][1] = in.nextInt(); } int[][] queries = new int[q][2]; for (int i = 0; i < q; i++) { queries[i][0] = in.nextInt() - 1; queries[i][1] = in.nextInt() - 1; } long[] ans = solve(points, queries); output(ans); } static void output(long[] ans) { StringBuilder sb = new StringBuilder(); for (long v : ans) { sb.append(v); sb.append('\n'); if (sb.length() > 500) { System.out.print(sb.toString()); sb.setLength(0); } } System.out.print(sb.toString()); } static void myAssert(boolean cond) { if (!cond) { throw new RuntimeException("Unexpected"); } } static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { try { final String USERDIR = System.getProperty("user.dir"); String cname = MethodHandles.lookup().lookupClass().getCanonicalName(); cname = cname.lastIndexOf('.') > 0 ? cname.substring(0, cname.lastIndexOf('.')) : cname; final File fin = new File(USERDIR + "/io/c" + cname.substring(1,5) + "/" + cname + ".in"); br = new BufferedReader(new InputStreamReader(fin.exists() ? new FileInputStream(fin) : System.in)); } catch (Exception e) { br = new BufferedReader(new InputStreamReader(System.in)); } } public String next() { try { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { throw new RuntimeException(e); } } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
Java
["5 5\n-2 2\n0 10\n1 1\n9 2\n12 7\n1 3\n2 3\n1 5\n3 5\n2 4"]
3 seconds
["9\n11\n9\n24\n11"]
NoteFor the first query, the minimum weighted distance is between points $$$1$$$ and $$$3$$$, which is equal to $$$|x_1 - x_3| \cdot (w_1 + w_3) = |-2 - 1| \cdot (2 + 1) = 9$$$.For the second query, the minimum weighted distance is between points $$$2$$$ and $$$3$$$, which is equal to $$$|x_2 - x_3| \cdot (w_2 + w_3) = |0 - 1| \cdot (10 + 1) = 11$$$.For the fourth query, the minimum weighted distance is between points $$$3$$$ and $$$4$$$, which is equal to $$$|x_3 - x_4| \cdot (w_3 + w_4) = |1 - 9| \cdot (1 + 2) = 24$$$.
Java 11
standard input
[ "data structures", "greedy" ]
046d0c0c73aad1a5dc295af1a43e0fc6
The first line contains 2 integers $$$n$$$ and $$$q$$$ $$$(2 \leq n \leq 3 \cdot 10^5; 1 \leq q \leq 3 \cdot 10^5)$$$ — the number of points and the number of queries. Then, $$$n$$$ lines follows, the $$$i$$$-th of them contains two integers $$$x_i$$$ and $$$w_i$$$ $$$(-10^9 \leq x_i \leq 10^9; 1 \leq w_i \leq 10^9)$$$ — the coordinate and the weight of the $$$i$$$-th point. It is guaranteed that the points are given in the increasing order of $$$x$$$. Then, $$$q$$$ lines follows, the $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ $$$(1 \leq l_i &lt; r_i \leq n)$$$ — the given subarray of the $$$i$$$-th query.
2,800
For each query output one integer, the minimum weighted distance among all pair of distinct points in the given subarray.
standard output
PASSED
90046677827cfb27823b30416dc749c8
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { //br = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter("output.txt"); int t = 1; t = nextInt(); while (t-- != 0) { int n = nextInt(); long k = nextLong(); long ans = 0; long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } long q = 0; for (int i = 0; i < a.length - 1; i++) { long y = a[i + 1] - a[i]; long u = 0; for (int j = 0; j < y; j++) { u *= 10; u += 9; } if (q + u > k || i == a.length - 2) { if (q + u > k) { if (ans == 0) ans = k - q + 1; else ans = Long.parseLong((k - q + 1) + "" + ans); } else { if (ans == 0) ans = Long.parseLong((k - q - u + 1) + "" + u); else ans = Long.parseLong((k - q - u + 1) + "" + u + "" + ans); } break; } else { k -= u; if (ans == 0) ans = u; else ans = Long.parseLong(u + "" + ans); } } if (ans == 0) { out.println(k + 1); } else out.println(ans); } out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer in = new StringTokenizer(""); public static boolean hasNext() throws IOException { if (in.hasMoreTokens()) return true; String s; while ((s = br.readLine()) != null) { in = new StringTokenizer(s); if (in.hasMoreTokens()) return true; } return false; } public static String nextToken() throws IOException { while (!in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public static long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
141170eea1076ffbdd54283ee425e4d5
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { //br = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter("output.txt"); int t = 1; t = nextInt(); while (t-- != 0) { int n = nextInt(); long k = nextLong(); long ans = 0; long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } long q = 0; for (int i = 0; i < a.length - 1; i++) { long y = a[i + 1] - a[i]; long u = 0; for (int j = 0; j < y; j++) { u *= 10; u += 9; } if (u > k || i == a.length - 2) { if (u > k) { if (ans == 0) ans = k + 1; else ans = Long.parseLong((k + 1) + "" + ans); } else { if (ans == 0) ans = Long.parseLong((k - u + 1) + "" + u); else ans = Long.parseLong((k - u + 1) + "" + u + "" + ans); } break; } else { k -= u; if (ans == 0) ans = u; else ans = Long.parseLong(u + "" + ans); } } if (ans == 0) { out.println(k + 1); } else out.println(ans); } out.close(); } static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer in = new StringTokenizer(""); public static boolean hasNext() throws IOException { if (in.hasMoreTokens()) return true; String s; while ((s = br.readLine()) != null) { in = new StringTokenizer(s); if (in.hasMoreTokens()) return true; } return false; } public static String nextToken() throws IOException { while (!in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public static long nextLong() throws IOException { return Long.parseLong(nextToken()); } public static double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
a68d6d68248d6b661387768ff82b226c
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long k=sc.nextLong(); long a[]=new long[n]; for(int i=0;i<n;i++) { int x=sc.nextInt(); a[i]=(long)Math.pow(10,x); } long sum=0; int i=0; for(i=0;i<n-1;i++) { long x=(a[i+1]/a[i])-1; if(x>k) { sum=sum+(k+1)*a[i]; k=-1; break; } else { sum=sum+(x*a[i]); k=k-x; } } if(k>=0){ sum+=((k+1)*a[i]);} System.out.println(sum); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
13478e1f5fc4b1fdbb4d4911e4d03b47
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; //import static com.sun.tools.javac.jvm.ByteCodes.swap; public class fastTemp { static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while (t-->0) { // WRITE BELOW int n = fs.nextInt(); int k = fs.nextInt(); int[] arr = new int[n]; HashSet<Integer> h = new HashSet<>(); for(int i=0;i<n;i++){ arr[i]= fs.nextInt(); } int nu = 0; int i = 0; int j = 1; int z = 1; long sum = 0L; for( i = 0;i<n-1;i++){ int y = arr[i+1]-arr[i]; long x =(long)Math.pow(10,y)-1; if(x>k){ sum += (k+1)*(long)Math.pow(10,arr[i]); k=-2; break; }else{ sum += x*(long)Math.pow(10,arr[i]); k = k-(int)x; } } if(k>=0){ sum += (k+1)*(long)Math.pow(10,arr[i]); } out.println(sum); } out.println(); out.close(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } // ------------------------------------------swap---------------------------------------------------------------------- static void swap(int arr[],int i,int j) { int temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } //-------------------------------------------seiveOfEratosthenes---------------------------------------------------- static boolean prime[]; static void sieveOfEratosthenes(int n) { // Create a boolean array // "prime[0..n]" and // initialize all entries // it as true. A value in // prime[i] will finally be // false if i is Not a // prime, else true. prime= new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a // prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } // Print all prime numbers // for (int i = 2; i <= n; i++) // { // if (prime[i] == true) // System.out.print(i + " "); // } } // ---------------------------------------- power------------------------------------------------------------------ public static long power(int a , int b) { if (b == 0) { return 1; } else if (b == 1) { return a; } else { long R = power(a, b / 2); if (b % 2 != 0) { return (((power(a, b / 2))) * a * ((power(a, b / 2)))); } else { return ((power(a, b / 2))) * ((power(a, b / 2))); } } } //--------------------------------------lower bound---------------------------------------------------------- static int LowerBound(int a[], int x) { // x is the target value or key int l=-1,r=a.length; while(l+1<r) { int m=(l+r)>>>1; if(a[m]>=x) r=m; else l=m; } return r; } //-------------------------------------------------------------------------------------------------------------- public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } //---------------------------------------EXTENDED EUCLID ALGO-------------------------------------------------------- public static class Pair{ int x; int y; public Pair(int x,int y){ this.x = x; this.y = y ; } } public static Pair Euclid(int a,int b){ if(b==0){ return new Pair(1,0); // answer of x and y } Pair dash = Euclid(b,a%b); return new Pair(dash.y , dash.x - (a/b)*dash.y); } //--------------------------------GCD------------------GCD-----------GCD-------------------------------------------- public static long gcd(long a,long b){ if(b==0){ return a; } return gcd(b,a%b); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
16ad153e7acc760e4fc9939d4490f259
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); while (n-- > 0) { long ans = 0; int m = sc.nextInt(); int k = sc.nextInt(); int a[] = new int[m]; for (int i = 0; i < m; i++) { a[i] = (int) Math.pow(10, sc.nextInt()); } int index = -1; for (int i = 0; i < m - 1; i++) { index = i; int j = a[i + 1] / a[i] - 1; if (k > j) { ans += 1L*(j) * a[i]; k -= j; } else if (k < j) { ans += 1L*(k + 1) * a[i]; k = 0; } else if (k == j) { ans += 1L*(j) * a[i]; ans += a[i + 1]; k=0; } if (k == 0) { break; } } if (k == 0) System.out.println(ans); else { ans += 1L*(k + 1) * a[index + 1]; System.out.println(ans); } } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
05df9c4c633c13ee7f33287ff1736a3b
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
//package CodeforcesContests; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class C { public static void main (String[] Z) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder op = new StringBuilder(); StringTokenizer stz; int T = Integer.parseInt(br.readLine()); while(T-- > 0) { stz = new StringTokenizer(br.readLine()); long n = Long.parseLong(stz.nextToken()); long k = Long.parseLong(stz.nextToken()); long[] arr = new long[(int)n]; long ten = 10; stz = new StringTokenizer(br.readLine()); for (int i = 0; i < n; i++) { long pow = Long.parseLong(stz.nextToken()); long num = 1; for (int j = 0; j < pow; j++) { num *= ten; } arr[i] = num; } long[] diffs = new long[(int)n]; diffs[0] = 0; Arrays.sort(arr); for (int i = 1 ; i < n; i++) { long now = arr[i]; long prev = arr[i-1]; long tadd = now/prev; --tadd; diffs[i] = diffs[i-1] + tadd; } // System.out.println(Arrays.toString(arr)); // System.out.println(Arrays.toString(diffs)); long ans = 1; if(arr[0] > k) { op.append("1\n"); continue; } for (int i = 0; i < n; i++) { if(k < diffs[i]) { if(i == 0) { ans = 1; break; } long rem = k - diffs[i-1] + 2 ; rem *= arr[i-1]; rem -= 1; ans = rem; break; } else if(i == n-1) { long rem = k - diffs[i] + 2 ; rem *= arr[i]; rem -= 1; ans = rem; break; } } op.append(ans + "\n"); } System.out.println(op); // END OF CODE } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
2ff72360b2fd30dbc42d82990e506666
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; import static java.lang.Math.*; public class Practice { static StringBuilder sb = new StringBuilder(); static Scanner scn = new Scanner(System.in); public static void main(String[] HastaLaVistaLa) { int t = scn.nextInt(); // int t = 1; for(int tests = 0; tests < t; tests++) solve(); System.out.println(sb); } public static void solve() { int n = scn.nextInt(); int k = scn.nextInt() + 1; int[] a = new int[n]; for(int i = 0; i < n; i++) a[i] = (int) pow(10, scn.nextInt()); List<Integer> d = new ArrayList<>(); int i = 0; while(i + 1 < n) { d.add((a[i + 1] - a[i]) / a[i]); i++; } long res = 0; for(i = 0; i + 1 < n && k > 0; i++) { res += (a[i] * (long) min(k, d.get(i))); k -= d.get(i); } if(k > 0) res += (a[n - 1] * (long) k); sb.append(res); sb.append("\n"); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
cc80b9b0c3a1cbbe8935e5807f47cd14
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; import java.math.*; /** * @author Naitik * */ public class Main { static FastReader sc=new FastReader(); static int dp[][][]; //static int v[][]; static int mod=998244353;; // static int mod=1000000007; static long max; static long bit[]; //static long bit1[]; // static long seg[]; //static long fact[]; // static long A[]; // static TreeMap<Integer,Integer> map; //static StringBuffer sb=new StringBuffer(""); static HashMap<Long,Integer> map; static PrintWriter out=new PrintWriter(System.out); public static void main(String[] args) { // StringBuffer sb=new StringBuffer(""); int ttt=1; ttt =i(); outer :while (ttt-- > 0) { int n=i(); long k=l(); int A[]=input(n); long B[]=new long[10]; B[0]=1; HashMap<Integer,Integer> map=hash(A); for(int i=1;i<10;i++) { if(!map.containsKey(i)) { B[i]=B[i-1]; } else { B[i]=power(10, i); } } long D[]=new long[19]; for(int i=0;i<19;i++) { D[i]=power(10, i); } long y=0; int c=0; while(true) { y=9*D[c]+y; long op1=go(y, B,D); if(op1>k) break; c++; } char C[]=(y+"").toCharArray(); n=C.length-1; long ans=Long.MAX_VALUE; for(int i=0;i<C.length;i++) { ans=y; for(int j=9;j>=0;j--) { long op1=y-j*D[n-i]; long go=go(op1, B,D); if(go>k) { ans=op1; break; } } y=ans; } out.println(y); } //System.out.println(sb.toString()); out.close(); //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 //CHECK FOR M=0 //CHECK FOR N=1 } static long go(long n,long A[],long C[]) { long ans=0; int c=0; while(n>0) { long y=n%10; n/=10; long r=y*C[c]; ans+=r/A[min(c,9)]; c++; } return ans; } static class Pair implements Comparable<Pair> { long x; int y; int z; Pair(long x,int y){ this.x=x; this.y=y; // this.z=z; } @Override public int compareTo(Pair o) { if(this.x>o.x) return +1; else if(this.x<o.x) return -1; else { if(this.y>o.y) return 1; else if(this.y<o.y) return -1; else return 0; } } // public int hashCode() // { // final int temp = 14; // int ans = 1; // ans =x*31+y*13; // return ans; // } // @Override // public boolean equals(Object o) // { // if (this == o) { // return true; // } // if (o == null) { // return false; // } // if (this.getClass() != o.getClass()) { // return false; // } // Pair other = (Pair)o; // if (this.x != other.x || this.y!=other.y) { // return false; // } // return true; // } // /* FOR TREE MAP PAIR USE */ // public int compareTo(Pair o) { // if (x > o.x) { // return 1; // } // if (x < o.x) { // return -1; // } // if (y > o.y) { // return 1; // } // if (y < o.y) { // return -1; // } // return 0; // } } //static int find(int A[],int a) { // if(A[a]<0) // return a; // return A[a]=find(A, A[a]); //} static int find(int A[],int a) { if(A[a]==a) return a; return find(A, A[a]); } //FENWICK TREE static void update(int i, int x){ for(; i < bit.length; i += (i&-i)) bit[i] += x; } static int sum(int i){ int ans = 0; for(; i > 0; i -= (i&-i)) ans += bit[i]; return ans; } //END static void add(long v) { if(!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v)+1); } } static void remove(long v) { if(map.containsKey(v)) { map.put(v, map.get(v)-1); if(map.get(v)==0) map.remove(v); } } public static int upper(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { ans=mid; l=mid+1; } else { u=mid-1; } } return ans; } public static int lower(int A[],int k,int si,int ei) { int l=si; int u=ei; int ans=-1; while(l<=u) { int mid=(l+u)/2; if(A[mid]<=k) { l=mid+1; } else { ans=mid; u=mid-1; } } return ans; } static int[] copy(int A[]) { int B[]=new int[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static long[] copy(long A[]) { long B[]=new long[A.length]; for(int i=0;i<A.length;i++) { B[i]=A[i]; } return B; } static int[] input(int n) { int A[]=new int[n]; for(int i=0;i<n;i++) { A[i]=sc.nextInt(); } return A; } static long[] inputL(int n) { long A[]=new long[n]; for(int i=0;i<n;i++) { A[i]=sc.nextLong(); } return A; } static String[] inputS(int n) { String A[]=new String[n]; for(int i=0;i<n;i++) { A[i]=sc.next(); } return A; } static long sum(int A[]) { long sum=0; for(int i : A) { sum+=i; } return sum; } static long sum(long A[]) { long sum=0; for(long i : A) { sum+=i; } return sum; } static void reverse(long A[]) { int n=A.length; long B[]=new long[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void reverse(int A[]) { int n=A.length; int B[]=new int[n]; for(int i=0;i<n;i++) { B[i]=A[n-i-1]; } for(int i=0;i<n;i++) A[i]=B[i]; } static void input(int A[],int B[]) { for(int i=0;i<A.length;i++) { A[i]=sc.nextInt(); B[i]=sc.nextInt(); } } static int[][] input(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]=i(); } } return A; } static char[][] charinput(int n,int m){ char A[][]=new char[n][m]; for(int i=0;i<n;i++) { String s=s(); for(int j=0;j<m;j++) { A[i][j]=s.charAt(j); } } return A; } static int max(int A[]) { int max=Integer.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static int min(int A[]) { int min=Integer.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long max(long A[]) { long max=Long.MIN_VALUE; for(int i=0;i<A.length;i++) { max=Math.max(max, A[i]); } return max; } static long min(long A[]) { long min=Long.MAX_VALUE; for(int i=0;i<A.length;i++) { min=Math.min(min, A[i]); } return min; } static long [] prefix(long A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] prefix(int A[]) { long p[]=new long[A.length]; p[0]=A[0]; for(int i=1;i<A.length;i++) p[i]=p[i-1]+A[i]; return p; } static long [] suffix(long A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static long [] suffix(int A[]) { long p[]=new long[A.length]; p[A.length-1]=A[A.length-1]; for(int i=A.length-2;i>=0;i--) p[i]=p[i+1]+A[i]; return p; } static void fill(int dp[]) { Arrays.fill(dp, -1); } static void fill(int dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(int dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(int dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static void fill(long dp[]) { Arrays.fill(dp, -1); } static void fill(long dp[][]) { for(int i=0;i<dp.length;i++) Arrays.fill(dp[i], -1); } static void fill(long dp[][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { Arrays.fill(dp[i][j],-1); } } } static void fill(long dp[][][][]) { for(int i=0;i<dp.length;i++) { for(int j=0;j<dp[0].length;j++) { for(int k=0;k<dp[0][0].length;k++) { Arrays.fill(dp[i][j][k],-1); } } } } static int min(int a,int b) { return Math.min(a, b); } static int min(int a,int b,int c) { return Math.min(a, Math.min(b, c)); } static int min(int a,int b,int c,int d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static int max(int a,int b) { return Math.max(a, b); } static int max(int a,int b,int c) { return Math.max(a, Math.max(b, c)); } static int max(int a,int b,int c,int d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long min(long a,long b) { return Math.min(a, b); } static long min(long a,long b,long c) { return Math.min(a, Math.min(b, c)); } static long min(long a,long b,long c,long d) { return Math.min(a, Math.min(b, Math.min(c, d))); } static long max(long a,long b) { return Math.max(a, b); } static long max(long a,long b,long c) { return Math.max(a, Math.max(b, c)); } static long max(long a,long b,long c,long d) { return Math.max(a, Math.max(b, Math.max(c, d))); } static long power(long x, long y, long p) { if(y==0) return 1; if(x==0) return 0; long res = 1; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static long power(long x, long y) { if(y==0) return 1; if(x==0) return 0; long res = 1; while (y > 0) { if (y % 2 == 1) res = (res * x); y = y >> 1; x = (x * x); } return res; } static void print(int A[]) { for(int i : A) { System.out.print(i+" "); } System.out.println(); } static void print(long A[]) { for(long i : A) { System.out.print(i+" "); } System.out.println(); } static long mod(long x) { return ((x%mod + mod)%mod); } static String reverse(String s) { StringBuffer p=new StringBuffer(s); p.reverse(); return p.toString(); } static int i() { return sc.nextInt(); } static String s() { return sc.next(); } static long l() { return sc.nextLong(); } static void sort(int[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static void sort(long[] A){ int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i){ long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } static String sort(String s) { Character ch[]=new Character[s.length()]; for(int i=0;i<s.length();i++) { ch[i]=s.charAt(i); } Arrays.sort(ch); StringBuffer st=new StringBuffer(""); for(int i=0;i<s.length();i++) { st.append(ch[i]); } return st.toString(); } static HashMap<Integer,Integer> hash(int A[]){ HashMap<Integer,Integer> map=new HashMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static HashMap<Long,Integer> hash(long A[]){ HashMap<Long,Integer> map=new HashMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Integer,Integer> tree(int A[]){ TreeMap<Integer,Integer> map=new TreeMap<Integer, Integer>(); for(int i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static TreeMap<Long,Integer> tree(long A[]){ TreeMap<Long,Integer> map=new TreeMap<Long, Integer>(); for(long i : A) { if(map.containsKey(i)) { map.put(i, map.get(i)+1); } else { map.put(i, 1); } } return map; } static boolean prime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static boolean prime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) return b; return gcd(b % a, a); } 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; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
cc2e7d7eecf94984530386b062cb7f63
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; public class c { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); int tc = Integer.parseInt(br.readLine()); for (int tt = 0; tt < tc; tt++) { st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); int k = Integer.parseInt(st.nextToken()); st = new StringTokenizer(br.readLine()); List<Integer> list = new ArrayList<>(); for (; st.hasMoreTokens(); ) { list.add(Integer.parseInt(st.nextToken())); } long[] powers = new long[10]; powers[0] = 1; for (int i = 1; i < 10; i++) { powers[i] = powers[i - 1] * 10; } // if (n == 1) { // //System.out.println(powers[list.get(0)] * k + 1); // if (list.get(0) != 0) { // System.out.println(1); // } else { // System.out.println(k + 1); // } // continue; // } long now = 0; //psum //long[] cnt = new long[10]; k++; for (int idx = 0; k > 0; idx++) { long herePower = powers[list.get(idx)]; if (now + 1 < herePower) { //빠뜨림 1 break; } if (idx == n - 1) { now += 1L * k * herePower; //now += herePower; k = 0; break; } long nxtPower = powers[list.get(idx + 1)]; long diff = nxtPower - now - 1; long need = diff / herePower; //need for next level if (diff % herePower != 0) need++; if (need > k) { now += 1L * k * herePower; //now += herePower; k -= need; break; } else { now += 1L * need * herePower; k -= need; } } //빠뜨림 2 if (k > 0) { for (int i = n - 1; i >= 0; i--) { if (powers[i] > now + 1) continue; else { now += powers[i] * k; // now += powers[i]; //빠뜨림 3 break; } } } if (now == 0) now = 1; System.out.println(now); } } private static int lowerBound(List<Long> list, long value) { int start = 0, end = list.size(); for (; start < end; ) { int mid = (start + end) / 2; if (list.get(mid) >= value) { end = mid; } else { start = mid + 1; } } return end; } // private static final int MAX = (int) 2e5 + 10; // private static final int mod = 998244353; // // private static long fastPow(long x, long e) { // if (e == 0) return 1; // if (e % 2 == 1) return (fastPow(x, e - 1) * x) % mod; // else { // long half = fastPow(x, e / 2) % mod; // return (half * half) % mod; // } // } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
4f49e99087c551bfe7442525ad0f5bc2
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Templ { static StreamTokenizer in; static int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } public static void main(String[] args) throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); long k = sc.nextLong(); long[] arr = new long[n]; for (int j = 0; j < n; j++) { int p = sc.nextInt(); arr[j] = 1; for (int l = 0; l < p; l++) { arr[j] *= 10; } } if (n == 1) { System.out.println(k + 1); continue; } Long time = null; long[] times = new long[n - 1]; for (int j = 0; j < n - 1; j++) { if (j > 0) times[j] = times[j - 1]; times[j] += arr[j + 1] / arr[j] - 1; if (times[j] > k) { time = calc(new long[]{k+1}, arr, j); break; } } if (time == null) { time = calc(new long[]{k+1}, arr, n - 1); } System.out.println(time); } } private static Long calc(long[] k, long[] arr, int i) { if (i == 0) { Long val = Math.min(k[0], arr[1] - 1); k[0] -= val; return val; } long times = 1; k[0]--; Long res = calc(k, arr, i - 1); if (i == arr.length - 1) { return res + arr[i] * (1 + k[0]); } times += Math.min(k[0], arr[i + 1] / arr[i] - 2); k[0] -= Math.max(0, times - 1); return res + times * arr[i]; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
c5088fa056743010d39343ef6a90b5af
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Templ { static StreamTokenizer in; static int nextInt() throws IOException { in.nextToken(); return (int) in.nval; } public static void main(String[] args) throws IOException { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); long k = sc.nextLong(); long[] arr = new long[n]; for (int j = 0; j < n; j++) { int p = sc.nextInt(); arr[j] = 1; for (int l = 0; l < p; l++) { arr[j] *= 10; } } if (n == 1) { System.out.println(k + 1); continue; } BigInteger time = null; long[] times = new long[n - 1]; for (int j = 0; j < n - 1; j++) { if (j > 0) times[j] = times[j - 1]; times[j] += arr[j + 1] / arr[j] - 1; if (times[j] > k) { time = calc(new long[]{k+1}, arr, j); break; } } if (time == null) { time = calc(new long[]{k+1}, arr, n - 1); } System.out.println(time); } } private static BigInteger calc(long[] k, long[] arr, int i) { if (i == 0) { Long val = Math.min(k[0], arr[1] - 1); k[0] -= val; return BigInteger.valueOf(val); } long times = 1; k[0]--; BigInteger res = calc(k, arr, i - 1); if (i == arr.length - 1) { return res.add(BigInteger.valueOf(arr[i] * (1 + k[0]))); } times += Math.min(k[0], arr[i + 1] / arr[i] - 2); k[0] -= Math.max(0, times - 1); return res.add(BigInteger.valueOf(times * arr[i])); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
e3914366dc08310fe85dbed2fcfb84fb
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class solution { public static void main(String[] args) { FScanner sc = new FScanner(); //Arrays.fill(prime, true); //sieve(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long k=sc.nextInt(); k++; long arr[]=new long[n]; for(int i=0;i<n;i++) { int x=sc.nextInt(); long val=1; while(x>0) { val=val*10; x--; } arr[i]=val; } long ans=0; for(int i=1;i<n;i++) { if( arr[i]/arr[i-1]-1 <=k ) { ans+=arr[i-1]*(arr[i]/arr[i-1] -1); k-=arr[i]/arr[i-1] -1; } else { ans+=arr[i-1]*k; k=0; } } if(k>0) ans+=arr[n-1]*k; System.out.println(ans); } } /* public static void sieve() { prime[0]=prime[1]=false; int n=1000000; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } */ } class pair { int x;int y; public pair(int f,int ind) { x=f; y=ind; } } class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(next()); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
cd85d4c3454b1f349e7c0cfe35c90ff4
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class solution { public static void main(String[] args) { FScanner sc = new FScanner(); //Arrays.fill(prime, true); //sieve(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long k=sc.nextInt(); k++; long arr[]=new long[n]; for(int i=0;i<n;i++) { int x=sc.nextInt(); long val=1; while(x>0) { val=val*10; x--; } arr[i]=val; } long ans=0; for(int i=0;i<n;i++) { long cnt=k; if( i+1<n ) { cnt=Math.min(cnt,arr[i+1]/arr[i] -1 ); } ans+=arr[i]*cnt; k=k-cnt; } System.out.println(ans); } } /* public static void sieve() { prime[0]=prime[1]=false; int n=1000000; for(int p = 2; p*p <=n; p++) { if(prime[p] == true) { for(int i = p*p; i <= n; i += p) prime[i] = false; } } */ } class pair { int x;int y; public pair(int f,int ind) { x=f; y=ind; } } class FScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer sb = new StringTokenizer(""); String next(){ while(!sb.hasMoreTokens()){ try{ sb = new StringTokenizer(br.readLine()); } catch(IOException e){ } } return sb.nextToken(); } String nextLine(){ try{ return br.readLine(); } catch(IOException e) { } return ""; } int nextInt(){ return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } int[] readArray(int n) { int a[] = new int[n]; for(int i=0;i<n;i++) a[i] = nextInt(); return a; } float nextFloat(){ return Float.parseFloat(next()); } double nextDouble(){ return Double.parseDouble(next()); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
43a94683b6e6dd267e1e5a0204025a89
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.awt.Container; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class Main { public static void main(String[] args) { FastScanner input = new FastScanner(); int tc = input.nextInt(); work: while (tc-- > 0) { String ans = ""; int n = input.nextInt(); int k = input.nextInt(); int a[]= new int[n]; for (int i = 0; i < n; i++) { a[i] = input.nextInt(); } int temp =a[0]; while(temp-->0) { ans+="0"; } for (int i = 0; i <n; i++) { if(i+1<n&&k>0) { String s =""; int nine = a[i+1]-a[i]; while(nine-->0) { s+="9"; } long value = Long.parseLong(s); if(value<=k) { ans = value+ans; k-=value; if(k==0) { ans = "1"+ans; System.out.println(ans); continue work; } } else { ans = (k+1)+ans; System.out.println(ans); continue work; } } else if(k>0) { ans = (k+1)+ans; k=0; } } System.out.println(ans); } } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() throws IOException { return br.readLine(); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
679efdbb9a374a00daaed7cd1374e236
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
//package com.company; import java.util.*; import java.io.*; public class Banknotes { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int testcases = Integer.parseInt(br.readLine()); for(int tests = 0; tests < testcases; tests++) { StringTokenizer st = new StringTokenizer(br.readLine()); int sizes = Integer.parseInt(st.nextToken()); int minNotes = Integer.parseInt(st.nextToken()) + 1; int[] nSizes = new int[sizes]; st = new StringTokenizer(br.readLine()); for(int i = 0; i < sizes; i++) { nSizes[i] = Integer.parseInt(st.nextToken()); } int[] pTen = {1, 10, 100, 1000, 10000, 100000, 1_000_000, 10_000_000, 100_000_000, 1_000_000_000}; int lSize = -1; int lCount = -1; for(int i = 0; i < sizes - 1; i++) { if(pTen[nSizes[i + 1] - nSizes[i]] - 1 < minNotes) { minNotes -= (pTen[nSizes[i + 1] - nSizes[i]] - 1); } else { lSize = nSizes[i]; lCount = minNotes; break; } } if(lCount == -1) { lCount = minNotes; lSize = nSizes[sizes - 1]; } StringBuilder ans = new StringBuilder(); if(lCount != 0) ans.append(lCount); for(int i = 0; i < lSize; i++) { ans.append("9"); } System.out.println(ans.toString()); } br.close(); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
5a6ca97946e93ad4c04a32247e864a37
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; public class Task { 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) { long[] powers = {1L, 10L, 100L, 1_000L, 10_000L, 100_000L, 1_000_000L, 10_000_000L, 100_000_000L, 1_000_000_000L}; FastReader reader = new FastReader(); int tests = reader.nextInt(); for (int T = 0; T < tests; T++) { int n = reader.nextInt(); long k = reader.nextLong() + 1; int lastPower = reader.nextInt(); long result = 0L; for (int i = 1; i < n; i++) { int curPower = reader.nextInt(); long limit = powers[curPower - lastPower] - 1L; if (k > limit) { k -= limit; result += powers[lastPower] * limit; } else { result += powers[lastPower] * k; k = 0; } lastPower = curPower; } result += powers[lastPower] * k; System.out.println(result); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
bb5e09dd20f25903485ce62c85b8cbf6
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
//package Div2.C; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Banknotes { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t>0){ String []nk=br.readLine().split(" "); int n=Integer.parseInt(nk[0]); int k=Integer.parseInt(nk[1]); String []str=br.readLine().split(" "); int []a=new int[n]; for(int i=0;i<n;i++) a[i]=Integer.parseInt(str[i]); long s=0; int bk = k+1;//bank notes to represent for(int i=0;i<n && bk>0;i++){ long dmv = (long)Math.pow(10,a[i]); //if there is higher denomination available if(i+1<n){ //max bank notes of a[i] denomination int mbn = (int)Math.pow(10,a[i+1]-a[i])-1; //denomination value s += Integer.min(bk, mbn) * dmv; bk -= Integer.min(bk, mbn); }else{ s += bk * dmv; bk = 0; } } System.out.println(s); t--; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
e6e51a38597037616a20aa319e9a5135
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
//package Div2.C; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Banknotes { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t>0){ String []nk=br.readLine().split(" "); int n=Integer.parseInt(nk[0]); int k=Integer.parseInt(nk[1]); String []str=br.readLine().split(" "); int []a=new int[n]; for(int i=0;i<n;i++) a[i]=Integer.parseInt(str[i]); long s=0; int bk = k+1;//bank notes to represent for(int i=0;i<n && bk>0;i++){ long dmv = (long)Math.pow(10,a[i]); //if there is higher denomination available if(i+1<n){ //max bank notes of a[i] denomination int mbn = (int)Math.pow(10,a[i+1]-a[i])-1; //denomination value s += Integer.min(bk, mbn) * dmv; bk -= Integer.min(bk, mbn); }else{ s += bk * dmv; k = 0; } } System.out.println(s); t--; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
eddd5ac8d56cbbd83a5bb1fe83aa8a8b
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
//package Div2.C; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Banknotes { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()); while(t>0){ String []nk=br.readLine().split(" "); int n=Integer.parseInt(nk[0]); int k=Integer.parseInt(nk[1]); String []str=br.readLine().split(" "); int []a=new int[n]; for(int i=0;i<n;i++) a[i]=Integer.parseInt(str[i]); long s=0; int bk = k+1;//bank notes to represent for(int i=0;i<n && bk>0;i++){ long dmv = (long)Math.pow(10,a[i]); //if there is higher denomination available if(i+1<n){ //max bank notes of a[i] denomination int mbn = (int)Math.pow(10,a[i+1]-a[i])-1; //denomination value s += Integer.min(bk, mbn) * dmv; bk -= Long.min(bk, mbn); }else{ s += bk * dmv; k = 0; } } System.out.println(s); t--; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
e5b85e7d0efff4ba2043d4d8770b9657
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
// Problem: C. Banknotes // Contest: Codeforces - Educational Codeforces Round 116 (Rated for Div. 2) // URL: https://codeforces.com/contest/1606/problem/C // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) // Problem: D. Frog Traveler // Contest: Codeforces - Codeforces Round #751 (Div. 2) // URL: https://codeforces.com/contest/1602/problem/D // Memory Limit: 512 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org) import java.io.*; import java.util.*; import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; /** * zxz * problem link: **/ public class Main { //put some data here /** * do some clean action before next testcase */ private void clear() { } private void solve() throws Exception { rf(); int n = ri(); int k = ri(); long ans = 0; rf(); long [] fac = new long[n]; long [] t = {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; for(int i = 0; i < n; i++){ fac[i] = t[ri()]; } for(int i = 0; i < n - 1 && k >= 0; i++){ if(fac[i] * (k + 1) < fac[i + 1] - 1){ ans += fac[i] * (k + 1); // System.out.println(k + 1 + " " + fac[i]); k = -1; }else{ ans += fac[i] * ((fac[i + 1] - 1) / fac[i]); k -= (fac[i + 1] - 1) / fac[i]; // System.out.println(fac[i + 1] - 1 + " " + fac[i] + " " + k); } } if(k >= 0){ ans += fac[n-1] * (k + 1); } addAns(ans); } private void run() throws Exception { int T = 1; rf(); T = ri(); while (T-- > 0) { solve(); if (T != 0) { clear(); } } printAns(); } public static void main(String[] args) throws Exception { new Main().run(); } StringBuilder sb = new StringBuilder(); BufferedReader infile = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer strT; private void addAns(int a){ sb.append(a + "\n"); } private void addAns(long a){ sb.append(a + "\n"); } private void addAns(String s){ sb.append(s + "\n"); } private void rf() throws IOException { strT = new StringTokenizer(infile.readLine()); } private int ri() { return Integer.parseInt(strT.nextToken()); } private long rl() { return Long.parseLong(strT.nextToken()); } private char [] rs2c(){ return strT.nextToken().toCharArray(); } private String rs(){ return strT.nextToken(); } private void printAns() { System.out.println(sb); } private int[] readArr(int N) throws Exception { int[] arr = new int[N]; rf(); for (int i = 0; i < N; i++) { arr[i] = Integer.parseInt(strT.nextToken()); } return arr; } private void print(int[] arr) { //for debugging only for (int x : arr) { System.out.print(x + " "); } System.out.println(); } private void print(long[] arr) { //for debugging only for (long x : arr) { System.out.print(x + " "); } System.out.println(); } private long[] readArr2(int N) throws Exception { long[] arr = new long[N]; strT = new StringTokenizer(infile.readLine()); for (int i = 0; i < N; i++) { arr[i] = Long.parseLong(strT.nextToken()); } return arr; } private boolean isPrime(long n) { if (n < 2) { return false; } if (n == 2 || n == 3) { return true; } if (n % 2 == 0 || n % 3 == 0) { return false; } long sqrtN = (long) Math.sqrt(n) + 1; for (long i = 6L; i <= sqrtN; i += 6) { if (n % (i - 1) == 0 || n % (i + 1) == 0) { return false; } } return true; } private long gcd(long a, long b) { if (a > b) { a = (a + b) - (b = a); } if (a == 0L) { return b; } return gcd(b % a, a); } private int gcd(int a, int b) { if (a > b) { a = (a + b) - (b = a); } if (a == 0) { return b; } return gcd(b % a, a); } private double pow(double x, long N) { double ans = 1.0; // 贡献的初始值为 x double x_contribute = x; // 在对 N 进行二进制拆分的同时计算答案 while (N > 0) { if (N % 2 == 1) { // 如果 N 二进制表示的最低位为 1,那么需要计入贡献 ans *= x_contribute; } // 将贡献不断地平方 x_contribute *= x_contribute; // 舍弃 N 二进制表示的最低位,这样我们每次只要判断最低位即可 N /= 2; } return ans; } long pow_mod(long x, long i, long mod){ if(i == 0){ return 1; } long temp = pow_mod(x, i >> 1, mod); temp = temp * temp % mod; if((i & 1 )!= 0){ temp = temp * x % mod; } return temp; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
16bd97687d44e28cc8ca9a86f099ad23
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.*; public class banknotes { public static void main(String[] args) { // Scanner sc = new Scanner(System.in); FastReader sc = new FastReader(); int t = sc.nextInt(); for(int o = 0 ; o<t;o++) { int n = sc.nextInt(); int k = sc.nextInt(); k++; int[] arr = new int[n]; for(int i = 0 ; i<n;i++) { arr[i] = sc.nextInt(); } long[] val = new long[n]; for(int i = 0 ; i<n;i++){ val[i] = (long)Math.pow(10, arr[i]); } long ans = 0; String s1 = ""; // long yy = 9 + 90 + 999 + 9999 + 99999 + 999999+ 9999999 + 99999999; // System.out.println(yy); // long v = 999999920999999999l; // System.out.println(v); long [] dp = new long[n+1]; long rem = k; // BigInteger bg = new BigInteger("0"); for(int i = 0 ; i<n-1;i++) { // int x= i + 1; // long temp = dp[i] + val[i] * (rem+1); // // // BigInteger bg = new BigInteger("0"); // // if(x == n) { // ans = temp; // break; // } //// System.out.println(temp); // if(temp<=val[x] && temp>0) { // ans = temp; // break; // }else{ // long num = val[i+1]-1; // dp[x] = num*val[i] + dp[i]; // rem -= num; //// System.out.println(dp[x]); //// System.out.println(dp[x]); // } // long v = (val[i + 1]/val[i]) - 1; // v = Math.min(v, rem); v = Math.min(rem, v); ans += val[i]*v; rem-=v; if(rem== 0) { break; } } if(rem>0) { ans += (rem) * val[n-1]; } System.out.println(ans); } } } 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; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
99e2a81946c9f038ff5c28d0863cdd51
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; public class C1606{ static FastScanner fs = null; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = fs.nextInt(); while(t-->0){ int n = fs.nextInt(); long k = fs.nextLong(); int count[] = new int[12]; int max = 0; for(int i=0;i<n;i++){ int num = fs.nextInt(); count[num]+=1; max = Math.max(num,num); } long ans = 0; int prev = 0; int c = 0; for(int i=0;i<12;i++){ if(count[i]==0){ if(i>max){ ans+=((k+1)*(long)Math.pow(10,i-1)); break; } else{ long mul = (long)9*(long)Math.pow(10,c); if(mul>=(k+(long)1)){ ans+=((k+(long)1)*(long)Math.pow(10,i-c)); break; } else{ ans+=((mul)*(long)Math.pow(10,i-c)); k-=mul; } } c+=1; } else{ long ci = (long)9; if((k+(long)1)<=ci){ ans+=((k+(long)1)*(long)Math.pow(10,i)); break; } k-=(long)9; ans+=(ci*(long)Math.pow(10,i)); prev = i; c = 1; } } out.println(ans); } out.println(); out.close(); } static void sort(int[] a) { ArrayList<Integer> l=new ArrayList<>(); for (int i:a) l.add(i); Collections.sort(l); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a=new int[n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
dfe312d20b9bb0b86e781811ec4a8e05
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
/* || श्री राम समर्थ || || जय जय रघुवीर समर्थ || */ import java.io.*; import java.math.BigInteger; import java.util.*; import static java.util.Arrays.sort; public class CodeforcesTemp { static Reader scan = new Reader(); static FastPrinter out = new FastPrinter(); public static void main(String[] args) throws IOException { int tt = scan.nextInt(); // int tt = 1; StringBuilder sb=new StringBuilder(); outer: for (int tc = 1; tc <= tt; tc++) { int n=scan.nextInt(); int k= scan.nextInt(); int[] arr=scan.nextIntArray(n); int[] diff=new int[n-1]; for (int i = 0; i < n-1; i++) { diff[i]= (int) (Math.pow(10,arr[i+1]-arr[i]) - 1); } int cnt=0; int i; for ( i = 0; i < n-1; i++) { cnt+=diff[i]; if(cnt>k)break ; } long ans; if(i>=n-1){ long extra=(k+1)-cnt; ans = (long) (extra*1L*Math.pow(10,arr[n-1])); for (int j = 0; j < n-1; j++) { ans += (long) (diff[j]*1L*Math.pow(10,arr[j])); } }else{ long extra=(k+1)-(cnt-diff[i]); ans = (long) (extra*1L*Math.pow(10,arr[i])); for (int j = 0; j < i; j++) { ans += (long) (diff[j]*1L*Math.pow(10,arr[j])); } } sb.append(ans);sb.append("\n"); } out.println(sb.toString()); out.flush(); out.close(); } static class Reader { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public Reader(InputStream in) { this.in = in; } public Reader() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("not number")); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("not number")); } } } } throw new ArithmeticException( String.format(" overflows long.")); } n = n * 10 + digit; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(long length) { long[] array = new long[(int) length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public Integer[] nextIntegerArray(int length) { Integer[] array = new Integer[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[][] nextIntArrayMulti(int length, int width) { int[][] arrays = new int[width][length]; for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) arrays[j][i] = this.nextInt(); } return arrays; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } static class FastPrinter extends PrintWriter { public FastPrinter(PrintStream stream) { super(stream); } public FastPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printArray(int[] array, String separator) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n - 1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n - 1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public void printMatrix(int[][] arr) { for (int i = 0; i < arr.length; i++) { this.printArray(arr[i]); } } public void printCharMatrix(char[][] arr, int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { this.print(arr[i][j] + " "); } this.println(); } } } static Random __r = new Random(); static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] arr, int i, int j) { while (i < j) { char temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; i++; j--; } } static void reverse(int[] arr, int i, int j) { while (i < j) { int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; i++; j--; } } static void reverse(long[] arr, int i, int j) { while (i < j) { long temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; i++; j--; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } private static int getlowest(int l) { if (l >= 1000000000) return 1000000000; if (l >= 100000000) return 100000000; if (l >= 10000000) return 10000000; if (l >= 1000000) return 1000000; if (l >= 100000) return 100000; if (l >= 10000) return 10000; if (l >= 1000) return 1000; if (l >= 100) return 100; if (l >= 10) return 10; return 1; } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
bf1256634fa0b03bc4b2cb7b5d0b960c
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
/* || श्री राम समर्थ || || जय जय रघुवीर समर्थ || */ import java.io.*; import java.math.BigInteger; import java.util.*; import static java.util.Arrays.sort; public class CodeforcesTemp { static Reader scan = new Reader(); static FastPrinter out = new FastPrinter(); public static void main(String[] args) throws IOException { int tt = scan.nextInt(); // int tt = 1; StringBuilder sb=new StringBuilder(); outer: for (int tc = 1; tc <= tt; tc++) { int n=scan.nextInt(); int k= scan.nextInt(); int[] arr=scan.nextIntArray(n); int[] diff=new int[n-1]; for (int i = 0; i < n-1; i++) { diff[i]= (int) (Math.pow(10,arr[i+1]-arr[i]) - 1); } int cnt=0; int i; for ( i = 0; i < n-1; i++) { cnt+=diff[i]; if(cnt>k)break ; } BigInteger ans; if(i>=n-1){ int extra=(k+1)-cnt; ans=BigInteger.valueOf(extra).multiply(BigInteger.TEN.pow(arr[n-1])); for (int j = 0; j < n-1; j++) { BigInteger val=BigInteger.valueOf(diff[j]).multiply(BigInteger.TEN.pow(arr[j])); ans= ans.add(val); } }else{ int extra=(k+1)-(cnt-diff[i]); ans=BigInteger.valueOf(extra).multiply(BigInteger.TEN.pow(arr[i])); for (int j = 0; j < i; j++) { BigInteger val=BigInteger.valueOf(diff[j]).multiply(BigInteger.TEN.pow(arr[j])); ans=ans.add(val); } } sb.append(ans);sb.append("\n"); } out.println(sb.toString()); out.flush(); out.close(); } static class Reader { private final InputStream in; private final byte[] buffer = new byte[1024]; private int ptr = 0; private int buflen = 0; private static final long LONG_MAX_TENTHS = 922337203685477580L; private static final int LONG_MAX_LAST_DIGIT = 7; private static final int LONG_MIN_LAST_DIGIT = 8; public Reader(InputStream in) { this.in = in; } public Reader() { this(System.in); } private boolean hasNextByte() { if (ptr < buflen) { return true; } else { ptr = 0; try { buflen = in.read(buffer); } catch (IOException e) { e.printStackTrace(); } if (buflen <= 0) { return false; } } return true; } private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1; } private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126; } public boolean hasNext() { while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte(); } public String next() { if (!hasNext()) throw new NoSuchElementException(); StringBuilder sb = new StringBuilder(); int b = readByte(); while (isPrintableChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } public long nextLong() { if (!hasNext()) throw new NoSuchElementException(); long n = 0; boolean minus = false; int b = readByte(); if (b == '-') { minus = true; b = readByte(); } if (b < '0' || '9' < b) { throw new NumberFormatException(); } while (true) { if ('0' <= b && b <= '9') { int digit = b - '0'; if (n >= LONG_MAX_TENTHS) { if (n == LONG_MAX_TENTHS) { if (minus) { if (digit <= LONG_MIN_LAST_DIGIT) { n = -n * 10 - digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("not number")); } } } else { if (digit <= LONG_MAX_LAST_DIGIT) { n = n * 10 + digit; b = readByte(); if (!isPrintableChar(b)) { return n; } else if (b < '0' || '9' < b) { throw new NumberFormatException( String.format("not number")); } } } } throw new ArithmeticException( String.format(" overflows long.")); } n = n * 10 + digit; } else if (b == -1 || !isPrintableChar(b)) { return minus ? -n : n; } else { throw new NumberFormatException(); } b = readByte(); } } public int nextInt() { long nl = nextLong(); if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException(); return (int) nl; } public double nextDouble() { return Double.parseDouble(next()); } public long[] nextLongArray(long length) { long[] array = new long[(int) length]; for (int i = 0; i < length; i++) array[i] = this.nextLong(); return array; } public long[] nextLongArray(int length, java.util.function.LongUnaryOperator map) { long[] array = new long[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsLong(this.nextLong()); return array; } public int[] nextIntArray(int length) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public Integer[] nextIntegerArray(int length) { Integer[] array = new Integer[length]; for (int i = 0; i < length; i++) array[i] = this.nextInt(); return array; } public int[][] nextIntArrayMulti(int length, int width) { int[][] arrays = new int[width][length]; for (int i = 0; i < length; i++) { for (int j = 0; j < width; j++) arrays[j][i] = this.nextInt(); } return arrays; } public int[] nextIntArray(int length, java.util.function.IntUnaryOperator map) { int[] array = new int[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsInt(this.nextInt()); return array; } public double[] nextDoubleArray(int length) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = this.nextDouble(); return array; } public double[] nextDoubleArray(int length, java.util.function.DoubleUnaryOperator map) { double[] array = new double[length]; for (int i = 0; i < length; i++) array[i] = map.applyAsDouble(this.nextDouble()); return array; } public long[][] nextLongMatrix(int height, int width) { long[][] mat = new long[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextLong(); } return mat; } public int[][] nextIntMatrix(int height, int width) { int[][] mat = new int[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextInt(); } return mat; } public double[][] nextDoubleMatrix(int height, int width) { double[][] mat = new double[height][width]; for (int h = 0; h < height; h++) for (int w = 0; w < width; w++) { mat[h][w] = this.nextDouble(); } return mat; } public char[][] nextCharMatrix(int height, int width) { char[][] mat = new char[height][width]; for (int h = 0; h < height; h++) { String s = this.next(); for (int w = 0; w < width; w++) { mat[h][w] = s.charAt(w); } } return mat; } } static class FastPrinter extends PrintWriter { public FastPrinter(PrintStream stream) { super(stream); } public FastPrinter() { super(System.out); } private static String dtos(double x, int n) { StringBuilder sb = new StringBuilder(); if (x < 0) { sb.append('-'); x = -x; } x += Math.pow(10, -n) / 2; sb.append((long) x); sb.append("."); x -= (long) x; for (int i = 0; i < n; i++) { x *= 10; sb.append((int) x); x -= (int) x; } return sb.toString(); } @Override public void print(float f) { super.print(dtos(f, 20)); } @Override public void println(float f) { super.println(dtos(f, 20)); } @Override public void print(double d) { super.print(dtos(d, 20)); } @Override public void println(double d) { super.println(dtos(d, 20)); } public void printArray(int[] array, String separator) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(int[] array) { this.printArray(array, " "); } public void printArray(int[] array, String separator, java.util.function.IntUnaryOperator map) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(map.applyAsInt(array[i])); super.print(separator); } super.println(map.applyAsInt(array[n - 1])); } public void printArray(int[] array, java.util.function.IntUnaryOperator map) { this.printArray(array, " ", map); } public void printArray(long[] array, String separator) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(array[i]); super.print(separator); } super.println(array[n - 1]); } public void printArray(long[] array) { this.printArray(array, " "); } public void printArray(long[] array, String separator, java.util.function.LongUnaryOperator map) { int n = array.length; for (int i = 0; i < n - 1; i++) { super.print(map.applyAsLong(array[i])); super.print(separator); } super.println(map.applyAsLong(array[n - 1])); } public void printArray(long[] array, java.util.function.LongUnaryOperator map) { this.printArray(array, " ", map); } public void printMatrix(int[][] arr) { for (int i = 0; i < arr.length; i++) { this.printArray(arr[i]); } } public void printCharMatrix(char[][] arr, int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { this.print(arr[i][j] + " "); } this.println(); } } } static Random __r = new Random(); static int randInt(int min, int max) { return __r.nextInt(max - min + 1) + min; } static void reverse(int[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { int swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(long[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { long swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(double[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { double swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] a) { for (int i = 0, n = a.length, half = n / 2; i < half; ++i) { char swap = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = swap; } } static void reverse(char[] arr, int i, int j) { while (i < j) { char temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; i++; j--; } } static void reverse(int[] arr, int i, int j) { while (i < j) { int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; i++; j--; } } static void reverse(long[] arr, int i, int j) { while (i < j) { long temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; i++; j--; } } static void shuffle(int[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); int swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void shuffle(long[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); long swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } private static int countDigits(int l) { if (l >= 1000000000) return 10; if (l >= 100000000) return 9; if (l >= 10000000) return 8; if (l >= 1000000) return 7; if (l >= 100000) return 6; if (l >= 10000) return 5; if (l >= 1000) return 4; if (l >= 100) return 3; if (l >= 10) return 2; return 1; } private static int getlowest(int l) { if (l >= 1000000000) return 1000000000; if (l >= 100000000) return 100000000; if (l >= 10000000) return 10000000; if (l >= 1000000) return 1000000; if (l >= 100000) return 100000; if (l >= 10000) return 10000; if (l >= 1000) return 1000; if (l >= 100) return 100; if (l >= 10) return 10; return 1; } static void shuffle(double[] a) { int n = a.length - 1; for (int i = 0; i < n; ++i) { int ind = randInt(i, n); double swap = a[i]; a[i] = a[ind]; a[ind] = swap; } } static void rsort(int[] a) { shuffle(a); sort(a); } static void rsort(long[] a) { shuffle(a); sort(a); } static void rsort(double[] a) { shuffle(a); sort(a); } static int[] copy(int[] a) { int[] ans = new int[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static long[] copy(long[] a) { long[] ans = new long[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static double[] copy(double[] a) { double[] ans = new double[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } static char[] copy(char[] a) { char[] ans = new char[a.length]; for (int i = 0; i < a.length; ++i) ans[i] = a[i]; return ans; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
649d5c1d76a1d7461a10b6e95f64357d
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.Scanner; public class C1606 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = in.nextInt(); for (int t = 0; t < T; t++) { int N = in.nextInt(); int K = in.nextInt() + 1; int[] A = new int[N + 1]; for (int n = 0; n < N; n++) { A[n] = in.nextInt(); } A[N] = 18; System.out.println(solve(K, A)); } } private static String solve(int K, int[] A) { String answer = ""; int idx = 0; long max; while (true) { max = 1; for (int i = A[idx]; i < A[idx + 1]; i++) { max *= 10; } idx++; if (max > K) { answer = K + answer; break; } else { K -= max - 1; answer = (max - 1) + answer; } } return answer; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
5ae4e4d7bcb6a3469092f54c5aae5b18
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class Sum { static BufferedReader bf; static PrintWriter out; static Scanner sc; static StringTokenizer st; public static void main (String[] args)throws IOException { bf = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new Scanner(System.in); int t = Integer.parseInt(bf.readLine()); while(t-->0){ solve(); } } public static void solve()throws IOException { int n = nextInt(); int k = nextInt(); long[]arr = new long[n]; for(int i = 0;i<n;i++){ arr[i] =(long) Math.pow(10,nextInt()); } long ans = 0; for(int i =0;i<n-1;i++){ if((arr[i+1]/arr[i]) - 1 <= k){ if(i == 0){ k -=(arr[i+1]/arr[i])-2; ans += (arr[i+1]/arr[i]) -2; } else{ ans += (((arr[i+1]/arr[i]) - 1)*arr[i]); // if(i == 1){ // println("next" + arr[i+1]); // println("cur"+arr[i]); // println(arr[i+1]/arr[i]); // } k -= ((arr[i+1]/arr[i]) -1); } } else{ ans += (arr[i]*k); k = 0; break; } // println(ans); } if(k > 0){ ans += (k * arr[n-1]); } println(ans+1); } // code for input public static void print(String s ){ System.out.print(s); } public static void print(int num ){ System.out.print(num); } public static void print(long num ){ System.out.print(num); } public static void println(String s){ System.out.println(s); } public static void println(int num){ System.out.println(num); } public static void println(long num){ System.out.println(num); } public static void println(){ System.out.println(); } public static int Int(String s){ return Integer.parseInt(s); } public static long Long(String s){ return Long.parseLong(s); } public static String[] nextStringArray()throws IOException{ return bf.readLine().split(" "); } public static String nextString()throws IOException{ return bf.readLine(); } public static long[] nextLongArray(int n)throws IOException{ String[]str = bf.readLine().split(" "); long[]arr = new long[n]; for(int i =0;i<n;i++){ arr[i] = Long.parseLong(str[i]); } return arr; } public static int[][] newIntMatrix(int r,int c)throws IOException{ int[][]arr = new int[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Integer.parseInt(str[j]); } } return arr; } public static long[][] newLongMatrix(int r,int c)throws IOException{ long[][]arr = new long[r][c]; for(int i =0;i<r;i++){ String[]str = bf.readLine().split(" "); for(int j =0;j<c;j++){ arr[i][j] = Long.parseLong(str[j]); } } return arr; } public static long gcd(long a,long b){ if(b == 0)return a; return gcd(b,a%b); } public static long lcm(long a,long b){ return (a*b)/(gcd(a,b)); } public static boolean isPalindrome(String s){ int i = 0; int j = s.length()-1; while(i<=j){ if(s.charAt(i) != s.charAt(j)){ return false; } i++; j--; } return true; } // these functions are to calculate the number of smaller elements after self public static void sort(int[]arr,int l,int r){ if(l < r){ int mid = (l+r)/2; sort(arr,l,mid); sort(arr,mid+1,r); smallerNumberAfterSelf(arr, l, mid, r); } } public static void smallerNumberAfterSelf(int[]arr,int l,int mid,int r){ int n1 = mid - l +1; int n2 = r - mid; int []a = new int[n1]; int[]b = new int[n2]; for(int i = 0;i<n1;i++){ a[i] = arr[l+i]; } for(int i =0;i<n2;i++){ b[i] = arr[mid+i+1]; } int i = 0; int j =0; int k = l; while(i<n1 && j < n2){ if(a[i] < b[j]){ arr[k++] = a[i++]; } else{ arr[k++] = b[j++]; } } while(i<n1){ arr[k++] = a[i++]; } while(j<n2){ arr[k++] = b[j++]; } } public static String next(){ while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bf.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } static int nextInt() { return Integer.parseInt(next()); } static long nextLong() { return Long.parseLong(next()); } static double nextDouble() { return Double.parseDouble(next()); } static String nextLine(){ String str = ""; try { str = bf.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } // use some math tricks it might help // sometimes just try to think in straightforward plan. In A and B problems don't always complecate the questions with thinking too much differently // always use long number to do 10^9+7 modulo // if a problem is related to binary string it could also be related to parenthesis // *****try to use binary search(it is a very beautiful thing it can work in some of the very unexpected problems ) in the question it might work****** // try sorting // try to think in opposite direction of question it might work in your way // if a problem is related to maths try to relate some of the continuous subarray with variables like - > a+b+c+d or a,b,c,d in general // if the question is to much related to left and/or right side of any element in an array then try monotonic stack it could work. // in range query sums try to do binary search it could work // analyse the time complexity of program thoroughly // anylyse the test cases properly // if we divide any number by 2 till it gets 1 then there will be (number - 1) operation required // try to do the opposite operation of what is given in the problem //think about the base cases properly //If a question is related to numbers try prime factorisation or something related to number theory // keep in mind unique strings //you can calculate the number of inversion in O(n log n) // in a matrix you could sometimes think about row and cols indenpendentaly. // in questions related to bitwise operations take the LS B and MSB into consideration. // give some more thinking to the corner cases of your thinking if your code is not working. // if you have to find the triples(i,j,k) in any question try to fix any one of the value and then enumerating the other two values.
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
917f683e8b1c626342846014e4acb411
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; public class SolnC { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for(int i=0;i<t;i++) { int n=sc.nextInt(); int k=sc.nextInt(); k++; int[] a = new int[n]; for(int j=0;j<n;j++) { a[j]=sc.nextInt(); } long res = 0L; for(int j=0;j<n-1;j++) { if(k>0) { long ans = (long) (Math.pow(10, a[j+1]-a[j])-1); long best = Math.min(ans, k); res=(long) Math.pow(10, a[j])*best+res; k-=best; } } if(k>0) { res=res+(long) Math.pow(10, a[n-1])*k; } System.out.println(res); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
bf7eb3c4cba7fb738d3022c3e7698492
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws Exception { int tc = io.nextInt(); for (int i = 0; i < tc; i++) { solve(); } io.close(); } static long pow(long a, long b) { if (b == 0) return 1; long res = pow(a, b / 2); res = res * res; if (b % 2 == 1) { res = res * a; } return res; } private static void solve() throws Exception { int n = io.nextInt(); int k = io.nextInt() + 1; int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = io.nextInt(); } long result = 0; for (int i = 0; i < n; i++) { long range = k; if (i < n - 1) { range = Math.min(range, pow(10, a[i + 1] - a[i]) - 1); } if (range == 0) break; result += range * pow(10, a[i]); k -= range; } io.println(result); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(a.length); for (long i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } //-----------PrintWriter for faster output--------------------------------- public static FastIO io = new FastIO(); //-----------MyScanner class for faster input---------- static class FastIO extends PrintWriter { private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar, numChars; // standard input public FastIO() { this(System.in, System.out); } public FastIO(InputStream i, OutputStream o) { super(o); stream = i; } // file input public FastIO(String i, String o) throws IOException { super(new FileWriter(o)); stream = new FileInputStream(i); } // throws InputMismatchException() if previously detected end of file private int nextByte() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars == -1) return -1; // end of file } return buf[curChar++]; } // to read in entire lines, replace c <= ' ' // with a function that checks whether c is a line break public String next() { int c; do { c = nextByte(); } while (c <= ' '); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > ' '); return res.toString(); } public String nextLine() { int c; do { c = nextByte(); } while (c < '\n'); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = nextByte(); } while (c > '\n'); return res.toString(); } public int nextInt() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } int res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } public long nextLong() { int c; do { c = nextByte(); } while (c <= ' '); int sgn = 1; if (c == '-') { sgn = -1; c = nextByte(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res = 10 * res + c - '0'; c = nextByte(); } while (c > ' '); return res * sgn; } int[] nextInts(int n) { int[] data = new int[n]; for (int i = 0; i < n; i++) { data[i] = io.nextInt(); } return data; } public double nextDouble() { return Double.parseDouble(next()); } } //-------------------------------------------------------- }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
11183acce0710a6b232aecf864e24b07
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { Scanner in = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); int t = in.nextInt(); for (int tt = 0; tt < t; tt++) { int n = in.nextInt(); int k = in.nextInt() + 1; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = (int) Math.pow(10, in.nextInt()); ArrayList<Long> ls = new ArrayList<>(); int i = 0; while ((i + 1) < n) { ls.add((long) (a[i + 1] - a[i]) / a[i]); i++; } long res = 0; for (i = 0; i + 1 < n && k > 0; i++) { res += (a[i] * (long) Math.min(k, ls.get(i))); k -= ls.get(i); } if (k > 0) res += (a[n - 1] * (long) k); pw.println(res); } pw.close(); } static void debug(Object... obj) { System.err.println(Arrays.deepToString(obj)); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
55e1f9184c0ee99a9dbbbaf2fc64689e
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class Banknotes { public static void main(String[] args) throws IOException { FastReader fr = new FastReader(); PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out)); int t = fr.nextInt(); while (t-- > 0) { int n = fr.nextInt(); int k = fr.nextInt(); int[] den = new int[n]; for (int i = 0; i < den.length; i++) { den[i] = fr.nextInt(); } long total = 0; long notes = k + 1; int i = 0; while (notes > 0 && i < n - 1) { long min = Math.min(notes, ((long)Math.pow(10,den[i+1]) / (long)Math.pow(10,den[i]) - 1)); notes -= min; total += (long) Math.pow(10, den[i]) * min; // pr.println(min + " " + total); i++; } long add = (long) Math.pow(10, den[i]) * notes; if (notes > 0) total += add; pr.println(total); } pr.close(); } static class Pair { int x, y; public Pair(int x, int y) { this.x = x; this.y = y; } } static int toInt(String s) { return Integer.parseInt(s); } // MERGE SORT IMPLEMENTATION void sort(int[] arr, int l, int r) { if (l < r) { int m = l + (r - l) / 2; sort(arr, l, m); sort(arr, m + 1, r); // call merge merge(arr, l, m, r); } } void merge(int[] arr, int l, int m, int r) { // find sizes int len1 = m - l + 1; int len2 = r - m; int[] L = new int[len1]; int[] R = new int[len2]; // push to copies for (int i = 0; i < L.length; i++) L[i] = arr[l + i]; for (int i = 0; i < R.length; i++) { R[i] = arr[m + 1 + i]; } // fill in new array int i = 0, j = 0; int k = l; while(i < len1 && j < len2) { if (L[i] < R[i]) { arr[k] = L[i]; i++; } else { arr[k] = R[i]; j++; } k++; } // add remaining elements while (i < len1) { arr[k] = L[i]; i++;k++; } while (j < len2) { arr[k] = R[j]; j++; k++; } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() throws FileNotFoundException { 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; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
5bff36811c7cc8a1cd42ef004e38bc1d
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; // import java.lang.*; // import java.math.*; public class Codeforces { static FastReader sc=new FastReader(); static PrintWriter out=new PrintWriter(System.out); static long mod=1000000007; // static long mod=998244353; static int MAX=Integer.MAX_VALUE; static int MIN=Integer.MIN_VALUE; static long MAXL=Long.MAX_VALUE; static long MINL=Long.MIN_VALUE; static ArrayList<Integer> graph[]; static long fact[]; static StringBuffer sb; public static void main (String[] args) throws java.lang.Exception { // code goes here int t=I(); outer:while(t-->0) { int n=I(); long k=L(); k++; long a[]=new long[n]; long b[]=new long[n]; for(int i=0;i<n;i++){ a[i]=L(); } for(int i=0;i<n-1;i++){ if(a[i]+1==a[i+1]){ b[i]=9; }else{ b[i]=pwr(10L,a[i+1]-a[i])-1L; } } String s=""; long p=0; for(int i=0;i<n-1;i++){ if(p+b[i]<k){ p+=b[i]; s=b[i]+s; }else{ s=(k-p)+s; p+=k-p; break; } } if(p<k){ s=(k-p)+s; } out.println(s); } out.close(); } public static class pair { long a; long b; public pair(long val,long index) { a=val; b=index; } } public static class myComp implements Comparator<pair> { //sort in ascending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return -1; // else // return 1; // } // sort in descending order. public int compare(pair p1,pair p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return 1; else return -1; } } public static long nPr(int n,int r) { long ans=divide(fact(n),fact(n-r),mod); return ans; } public static long nCr(int n,int r) { long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod); return ans; } public static long kadane(long a[],int n) //largest sum subarray { long max_sum=Long.MIN_VALUE,max_end=0; for(int i=0;i<n;i++){ max_end+=a[i]; if(max_sum<max_end){max_sum=max_end;} if(max_end<0){max_end=0;} } return max_sum; } public static void DFS(int s,boolean visited[]) { visited[s]=true; for(int i:graph[s]){ if(!visited[i]){ DFS(i,visited); } } } public static void setGraph(int n,int m) { graph=new ArrayList[n+1]; for(int i=0;i<=n;i++){ graph[i]=new ArrayList<>(); } for(int i=0;i<m;i++){ int u=I(),v=I(); graph[u].add(v); graph[v].add(u); } } public static int BS(int a[],int x,int ii,int jj) { // int n=a.length; int mid=0; int i=ii,j=jj; while(i<=j) { mid=(i+j)/2; if(a[mid]<x){ i=mid+1; }else if(a[mid]>x){ j=mid-1; }else{ return mid+1; } } return 0; } //LOWER_BOUND and UPPER_BOUND functions public static int lower_bound(int arr[],int X,int start, int end) //start=0,end=n-1 { if(start>end)return -1; if(arr[arr.length-1]<X)return end; if(arr[0]>X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X){ //Returns last index of lower bound value. if(mid<end && arr[mid+1]==X){ left=mid+1; }else{ return mid; } } // if(arr[mid]==X){ //Returns first index of lower bound value. // if(mid>start && arr[mid-1]==X){ // right=mid-1; // }else{ // return mid; // } // } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid-1; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid; }else{ left=mid+1; } } } return left; } public static int upper_bound(int arr[],int X,int start,int end) //start=0,end=n-1 { if(arr[0]>=X)return start; if(arr[arr.length-1]<X)return -1; int left=start,right=end; while(left<right){ int mid=(left+right)/2; if(arr[mid]==X){ //returns first index of upper bound value. if(mid>start && arr[mid-1]==X){ right=mid-1; }else{ return mid; } } // if(arr[mid]==X){ //returns last index of upper bound value. // if(mid<end && arr[mid+1]==X){ // left=mid+1; // }else{ // return mid; // } // } else if(arr[mid]>X){ if(mid>start && arr[mid-1]<X){ return mid; }else{ right=mid-1; } }else{ if(mid<end && arr[mid+1]>X){ return mid+1; }else{ left=mid+1; } } } return left; } //END public static boolean isSorted(int a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static boolean isSorted(long a[]) { int n=a.length; for(int i=0;i<n-1;i++){ if(a[i]>a[i+1])return false; } return true; } public static int computeXOR(int n) //compute XOR of all numbers between 1 to n. { if (n % 4 == 0) return n; if (n % 4 == 1) return 1; if (n % 4 == 2) return n + 1; return 0; } public static ArrayList<Integer> primeSieve(int n) { ArrayList<Integer> arr=new ArrayList<>(); boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } for (int i = 2; i <= n; i++) { if (prime[i] == true) arr.add(i); } return arr; } // Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n); public static class FenwickTree { int farr[]; int n; public FenwickTree(int c) { n=c+1; farr=new int[n]; } // public void update_range(int l,int r,long p) // { // update(l,p); // update(r+1,(-1)*p); // } public void update(int x,int p) { for(;x<n;x+=x&(-x)) { farr[x]+=p; } } public long get(int x) { long ans=0; for(;x>0;x-=x&(-x)) { ans=ans+farr[x]; } return ans; } } //Disjoint Set Union public static class DSU { int par[],rank[]; public DSU(int c) { par=new int[c+1]; rank=new int[c+1]; for(int i=0;i<=c;i++) { par[i]=i; rank[i]=0; } } public int find(int a) { if(a==par[a]) return a; return par[a]=find(par[a]); } public void union(int a,int b) { int a_rep=find(a),b_rep=find(b); if(a_rep==b_rep) return; if(rank[a_rep]<rank[b_rep]) par[a_rep]=b_rep; else if(rank[a_rep]>rank[b_rep]) par[b_rep]=a_rep; else { par[b_rep]=a_rep; rank[a_rep]++; } } } public static class myComp1 implements Comparator<pair1> { //sort in ascending order. public int compare(pair1 p1,pair1 p2) { if(p1.a==p2.a) return 0; else if(p1.a<p2.a) return -1; else return 1; } //sort in descending order. // public int compare(pair p1,pair p2) // { // if(p1.a==p2.a) // return 0; // else if(p1.a<p2.a) // return 1; // else // return -1; // } } public static class pair1 { long a; long b; public pair1(long val,long index) { a=val; b=index; } } public static ArrayList<pair1> mergeIntervals(ArrayList<pair1> arr) { //****************use this in main function-Collections.sort(arr,new myComp1()); ArrayList<pair1> a1=new ArrayList<>(); if(arr.size()<=1) return arr; a1.add(arr.get(0)); int i=1,j=0; while(i<arr.size()) { if(a1.get(j).b<arr.get(i).a) { a1.add(arr.get(i)); i++; j++; } else if(a1.get(j).b>arr.get(i).a && a1.get(j).b>=arr.get(i).b) { i++; } else if(a1.get(j).b>=arr.get(i).a) { long a=a1.get(j).a; long b=arr.get(i).b; a1.remove(j); a1.add(new pair1(a,b)); i++; } } return a1; } public static ArrayList<Long> primeFact(long a) { ArrayList<Long> arr=new ArrayList<>(); while(a%2==0){ arr.add(2L); a=a/2; } for(long i=3;i*i<=a;i+=2){ while(a%i==0){ arr.add(i); a=a/i; } } if(a>2)arr.add(a); return arr; } public static boolean isInteger(double N) { int X = (int)N; double temp2 = N - X; if (temp2 > 0) { return false; } return true; } public static boolean isPalindrome(String s,int n) { for(int i=0;i<=n/2;i++){ if(s.charAt(i)!=s.charAt(n-i-1)){ return false; } } return true; } public static int gcd(int a,int b) { if(b==0) return a; else return gcd(b,a%b); } public static long gcd(long a,long b) { if(b==0) return a; else return gcd(b,a%b); } public static long fact(long n) { long fact=1; for(long i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static long fact(int n) { long fact=1; for(int i=2;i<=n;i++){ fact=((fact%mod)*(i%mod))%mod; } return fact; } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; double sq=Math.sqrt(n); for (int i = 5; i <= sq; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static void printArray(long a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(int a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(char a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]); } out.println(); } public static void printArray(String a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(boolean a[]) { for(int i=0;i<a.length;i++){ out.print(a[i]+" "); } out.println(); } public static void printArray(pair a[]) { for(pair p:a){ out.println(p.a+"->"+p.b); } } public static void printArray(int a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(long a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArray(char a[][]) { for(int i=0;i<a.length;i++){ for(int j=0;j<a[i].length;j++){ out.print(a[i][j]+" "); }out.println(); } } public static void printArrayL(ArrayList<Long> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printArrayI(ArrayList<Integer> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printArrayS(ArrayList<String> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapInt(HashMap<Integer,Integer> hm){ for(Map.Entry<Integer,Integer> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMapLong(HashMap<Long,Long> hm){ for(Map.Entry<Long,Long> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } //Modular Arithmetic public static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } public static long sub(long a,long b) { a-=b; if(a<0)a+=mod; return a; } public static long mul(long a,long b) { return ((a%mod)*(b%mod))%mod; } public static long divide(long a,long b,long m) { a=mul(a,modInverse(b,m)); return a; } public static long modInverse(long a,long m) { int x=0,y=0; own p=new own(x,y); long g=gcdExt(a,m,p); if(g!=1){ out.println("inverse does not exists"); return -1; }else{ long res=((p.a%m)+m)%m; return res; } } public static long gcdExt(long a,long b,own p) { if(b==0){ p.a=1; p.b=0; return a; } int x1=0,y1=0; own p1=new own(x1,y1); long gcd=gcdExt(b,a%b,p1); p.b=p1.a - (a/b) * p1.b; p.a=p1.b; return gcd; } public static long pwr(long m,long n) { long res=1; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m); } n=n>>1; m=(m*m); } return res; } public static long modpwr(long m,long n) { long res=1; m=m%mod; if(m==0) return 0; while(n>0) { if((n&1)!=0) { res=(res*m)%mod; } n=n>>1; m=(m*m)%mod; } return res; } public static class own { long a; long b; public own(long val,long index) { a=val; b=index; } } //Modular Airthmetic public static void sort(int[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { int tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(char[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { char tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static void sort(long[] A) { int n = A.length; Random rnd = new Random(); for(int i=0; i<n; ++i) { long tmp = A[i]; int randomPos = i + rnd.nextInt(n-i); A[i] = A[randomPos]; A[randomPos] = tmp; } Arrays.sort(A); } public static int I(){return sc.I();} public static long L(){return sc.L();} public static String S(){return sc.S();} public static double D(){return sc.D();} } 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 I(){ return Integer.parseInt(next()); } long L(){ return Long.parseLong(next()); } double D(){ return Double.parseDouble(next()); } String S(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
75591d3fed803470024d2a9f7efcbc69
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Main { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int num = sc.nextInt(); long k = sc.nextLong(); int n = num; k++; long a[] = new long[n]; for(int i=0;i<n;i++)a[i] = (long)Math.pow(10,sc.nextLong()); long last = a[n-1]; long b[] = new long[n-1]; for(int i=0;i<n-1;i++)b[i] = a[i+1] / a[i] - 1; long total = 0; for(int i=0;i<n-1;i++){ long lft = Math.min(k,b[i]); total += lft * a[i]; k -= lft; } total += last * k; System.out.println(total); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
69c3020ead74fd9aa09d9a157c2f72a6
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.Scanner; public class Banknotes { public static void solve(int n,int k,int[] arr) { long ans=0; // if(k<1000000000) k=k+1; for (int i = 1; i < n; i++) { int x=(int) (Math.pow(10, arr[i])/Math.pow(10,arr[i-1])-1); // System.out.println("hi "+x); if(k-x>=0) { ans+=x*Math.pow(10,arr[i-1]); } else { ans+=k*Math.pow(10, arr[i-1]); k=(int) (k-x); break; } k=(int) (k-x); // System.out.println("hlo "+k+" "+ans); } // System.out.println(ans+" "+k); if(k>0) { long a=(long) (k*Math.pow(10, arr[n-1])); System.out.println(a+ans); return; } System.out.println(ans); } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(System.in); int t=in.nextInt(); for (int i = 0; i < t; i++) { int n=in.nextInt(); int k=in.nextInt(); int[] arr=new int[n]; for (int j = 0; j < n; j++) { arr[j] = in.nextInt(); } solve(n, k, arr); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
727aa03513a323d7cf6211f4bcababd9
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.awt.Container; import java.awt.Point; import java.io.*; import java.util.*; //import javax.naming.directory.NoSuchAttributeException; public class Main { static FastScanner scr=new FastScanner(); // static Scanner scr=new Scanner(System.in); static PrintStream out=new PrintStream(System.out); static StringBuilder sb=new StringBuilder(); static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } long gcd(long a,long b){ if(b==0) { return a; } return gcd(b,a%b); } int gcd(int a,int b) { if(a*b==0) { return a+b; } return gcd(b,a%b); } long modPow(long base,long exp) { if(exp==0) { return 1; } if(exp%2==0) { long res=(modPow(base,exp/2)); return (res*res); } return (base*modPow(base,exp-1)); } long []reverse(long[] count){ long b[]=new long[count.length]; int index=0; for(int i=count.length-1;i>=0;i--) { b[index++]=count[i]; } return b; } int []reverse(int[] count){ int b[]=new int[count.length]; int index=0; for(int i=count.length-1;i>=0;i--) { b[index++]=count[i]; } return b; } int nextInt() { return Integer.parseInt(next()); } long getMax(long a[],int n) { long max=Long.MIN_VALUE; for(int i=0;i<n;i++) { max=Math.max(a[i], max); } return max; } long[] readLongArray(int n) { long [] a=new long [n]; for (int i=0; i<n; i++) a[i]=nextLong(); return a; }int[] readArray(int n) { int [] a=new int [n]; for (int i=0; i<n; i++) a[i]=nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } boolean isPrime(long n) { if(n < 2) return false; if(n == 2 || n == 3) return true; if(n%2 == 0 || n%3 == 0) return false; long sqrtN = (long)Math.sqrt(n)+1; for(long i = 6L; i <= sqrtN; i += 6) { if(n%(i-1) == 0 || n%(i+1) == 0) return false; } return true; } } static class triplet{ long x; long y; long z; triplet(long x,long y,long z){ this.x=x; this.y=y; this.z=z; } } static class pair{ int x; int y; pair( int x,int y){ this.x=x; this.y=y; } } static Solve s=new Solve(); public static void main(String []args) { int t=scr.nextInt(); // int t=1; // s.preprocess(200000+1); while(t-->0) { s.solve(); } out.println(sb); } static class Solve { int MAX=Integer.MAX_VALUE; int MIN=Integer.MIN_VALUE; ArrayList<Integer>list[]; long mod=998244353; long time[]; boolean visited[]; void solve() { int n=scr.nextInt(); int k=scr.nextInt()+1; long []a=new long[n]; for(int i=0;i<n;i++) { int x=scr.nextInt(); long tem=1; while(x-->0) { tem*=10; } a[i]=tem; } long ans=0; int index=0; while(index<n) { long req=k; if(index<n-1)req=Math.min(a[index+1]/a[index]-1, k); long temp=(long)(a[index])*req; ans+=temp; k-=req; index++; } // ans+=Math.pow(10l, a[n-1])*k; out.println(ans); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
313944409ecbf3ef1096c86a4be1ae26
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; import java.io.*; import java.util.*; import java.math.*; import static java.lang.Math.sqrt; import static java.lang.Math.floor; import java.io.*; import java.util.*; import java.io.*; import java.util.*; import java.util.*; public class topcoder { public static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode() {} TreeNode(int val) { this.val = val; } TreeNode(int val, TreeNode left, TreeNode right) { this.val = val; this.left = left; this.right = right; } static int x = -1; static int y = -1; public static int first_search(TreeNode root, TreeNode main_root) { if(root == null) return 0; int a = first_search(root.left,main_root); int b = first_search(root.right,main_root); if(a > main_root.val) x = a; if(b < main_root.val) y = b; return root.val; } public static void fix(TreeNode root, TreeNode main_root) { if(root == null) return; fix(root.left,main_root); if(root.val > main_root.val) { root.val = y; } fix(root.right,main_root); if(root.val < main_root.val); root.val = x; } public static int max(int []nums, int s, int e) { int max = Integer.MIN_VALUE; for(int i = s; i <= e; i++) { max = Math.max(max, nums[i]); } return max; } public static TreeNode new_node(int []nums, int s, int e) { int max = max(nums,s,e); TreeNode node = new TreeNode(max); return node; } public static TreeNode root; public static void res(int []nums, int s, int e) { if(s > e)return; if(root == null) { root = new_node(nums,s,e); } root.left = new_node(nums,s,e); root.right = new_node(nums,s,e); return; } static int depth(TreeNode root) { if(root == null)return 0; int a = 1+ depth(root.left); int b = 1+ depth(root.right); return Math.max(a,b); } static HashSet<Integer>set = new HashSet<>(); static void deepestLeaves(TreeNode root, int cur_depth, int depth) { if(root == null)return; if(cur_depth == depth)set.add(root.val); deepestLeaves(root.left,cur_depth+1,depth); deepestLeaves(root.right,cur_depth+1,depth); } public static void print(TreeNode root) { if(root == null)return; System.out.print(root.val+" "); System.out.println("er"); print(root.left); print(root.right); } public static HashSet<Integer>original(TreeNode root){ int d = depth(root); deepestLeaves(root,0,d); return set; } static HashSet<Integer>set1 = new HashSet<>(); static void leaves(TreeNode root) { if(root == null)return; if(root.left == null && root.right == null)set1.add(root.val); leaves(root.left); leaves(root.right); } public static boolean check(HashSet<Integer>s, HashSet<Integer>s1) { if(s.size() != s1.size())return false; for(int a : s) { if(!s1.contains(a))return false; } return true; } static TreeNode subTree; public static void smallest_subTree(TreeNode root) { if(root == null)return; smallest_subTree(root.left); smallest_subTree(root.right); set1 = new HashSet<>(); leaves(root); boolean smallest = check(set,set1); if(smallest) { subTree = root; return; } } public static TreeNode answer(TreeNode root) { smallest_subTree(root); return subTree; } } static class pair{ int first; int second; public pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(pair p) { if(first == p.first)return second-p.second; return first-p.first; } } static class Compare{ static void compare(ArrayList<pair>arr, long n) { Collections.sort(arr,new Comparator<pair>() { public int compare(pair p1, pair p2) { return (int) (p1.first-p2.first); } }); } } public static HashMap<Integer,Integer>sortByValue(HashMap<Integer,Integer>hm){ List<Map.Entry<Integer,Integer>>list = new LinkedList<Map.Entry<Integer,Integer>>(hm.entrySet()); Collections.sort(list,new Comparator<Map.Entry<Integer,Integer>>(){ public int compare(Map.Entry<Integer,Integer>o1, Map.Entry<Integer,Integer>o2) { return (o1.getValue()).compareTo(o2.getValue()); }}); HashMap<Integer,Integer>temp = new LinkedHashMap<Integer,Integer>(); for(Map.Entry<Integer,Integer>aa : list) { temp.put(aa.getKey(), aa.getValue()); } return temp; } static class pairr implements Comparable<pairr>{ static Long value; Long index; public pairr(Long value, Long index) { this.value = value; this.index = index; } public int compareTo(pairr o) { return (int)(value-o.value); } } static class Key<K1, K2> { public K1 key1; public K2 key2; public Key(K1 key1, K2 key2) { this.key1 = key1; this.key2 = key2; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Key key = (Key) o; if (key1 != null ? !key1.equals(key.key1) : key.key1 != null) { return false; } if (key2 != null ? !key2.equals(key.key2) : key.key2 != null) { return false; } return true; } @Override public int hashCode() { int result = key1 != null ? key1.hashCode() : 0; result = 31 * result + (key2 != null ? key2.hashCode() : 0); return result; } @Override public String toString() { return "[" + key1 + ", " + key2 + "]"; } } public static int sumOfDigits (long n) { int sum = 0; while(n > 0) { sum += n%10; n /= 10; } return sum; } public static long binary_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return ar[s]; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return binary_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return binary_search(s,mid,num,ar); } return -1; } public static int index_search(int s, int e, long num, long []ar) { if(s > e) { return -1; } int mid = (s+e)/2; if(s == e && ar[s] >= num) { return s; }else if(s == e && ar[s] < num) { return -1; }else if(ar[mid] < num) { return index_search(mid+1,e,num,ar); }else if(ar[mid] >= num) { return index_search(s,mid,num,ar); } return -1; } public static void swap(int []ar, int i, int j) { for(int k= j; k >= i; k--) { int temp = ar[k]; ar[k] = ar[k+1]; ar[k+1] = temp; } } public static boolean digit_exists(long n) { while(n > 0) { if(n%10 == 9) return true; n = n/10; } return false; } public static int log(int n) { int c = 0; while(n > 0) { c++; n /=2; } return c; } public static int findOr(int[]bits){ int or=0; for(int i=0;i<32;i++){ or=or<<1; if(bits[i]>0) or=or+1; } return or; } static void simpleSieve(int limit, Vector<Integer> prime) { // Create a boolean array "mark[0..n-1]" and initialize // all entries of it as true. A value in mark[p] will // finally be false if 'p' is Not a prime, else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; for (int p=2; p*p<limit; p++) { // If p is not changed, then it is a prime if (mark[p] == true) { // Update all multiples of p for (int i=p*p; i<limit; i+=p) mark[i] = false; } } // Print all prime numbers and store them in prime for (int p=2; p<limit; p++) { if (mark[p] == true) { prime.add(p); } } } // Prints all prime numbers smaller than 'n' public static void segmentedSieve(int n, ArrayList<Integer>l) { // Compute all primes smaller than or equal // to square root of n using simple sieve int limit = (int) (floor(sqrt(n))+1); Vector<Integer> prime = new Vector<>(); simpleSieve(limit, prime); // Divide the range [0..n-1] in different segments // We have chosen segment size as sqrt(n). int low = limit; int high = 2*limit; // While all segments of range [0..n-1] are not processed, // process one segment at a time while (low < n) { if (high >= n) high = n; // To mark primes in current range. A value in mark[i] // will finally be false if 'i-low' is Not a prime, // else true. boolean mark[] = new boolean[limit+1]; for (int i = 0; i < mark.length; i++) mark[i] = true; // Use the found primes by simpleSieve() to find // primes in current range for (int i = 0; i < prime.size(); i++) { // Find the minimum number in [low..high] that is // a multiple of prime.get(i) (divisible by prime.get(i)) // For example, if low is 31 and prime.get(i) is 3, // we start with 33. int loLim = (int) (floor(low/prime.get(i)) * prime.get(i)); if (loLim < low) loLim += prime.get(i); /* Mark multiples of prime.get(i) in [low..high]: We are marking j - low for j, i.e. each number in range [low, high] is mapped to [0, high-low] so if range is [50, 100] marking 50 corresponds to marking 0, marking 51 corresponds to 1 and so on. In this way we need to allocate space only for range */ for (int j=loLim; j<high; j+=prime.get(i)) mark[j-low] = false; } // Numbers which are not marked as false are prime for (int i = low; i<high; i++) if (mark[i - low] == true) l.add(i); // Update low and high for next segment low = low + limit; high = high + limit; } } public static int find_indexNum(long k) { long k1 = k; int power = 0; while(k > 0) { power++; k /=2 ; } long check = (long)Math.pow(2, power-1); if(k1 == check) { return power; } // System.out.println(power); long f = (long)Math.pow(2, power-1); long rem = k1-f; return find_indexNum(rem); } public static void sortPair(ArrayList<pair>l, int n) { n = l.size(); Compare obj = new Compare(); obj.compare(l, n); } public static void shuffle(int []array, int num,int t_index, boolean []vis, int m ) { for(int i = 0; i < m; i++) { if(vis[i] == false) { int temp = array[i]; if(i < t_index) { vis[i] = true; } array[i] = num; array[t_index] = temp; // System.out.println(array[t_index]+" "+array[i]); break; } } } public static void rotate(int []arr,int j, int times, int m) { if(j == 0) { int temp1 = arr[0]; arr[0] = arr[times]; arr[times] = temp1; }else { int temp = arr[j]; int z = arr[0]; arr[0] = arr[times]; arr[j] = z; arr[times] = temp; } } public static void recur(int i,int A, int B,int []dp,int []metal, int n, boolean took,int ind) { if(i-A <= 0 && i-B <= 0)return; int count = 0; for(int j = 1; j <= n; j++) { if(dp[j] >= metal[j]) { count++; } } if(count == n)return; if(i-A >= 0 && i-B >= 0 && dp[i] > 0 && dp[i] > metal[i]) { dp[i]--; dp[i-A]++; dp[i-B]++; } if(ind == 6) { // System.out.println(Arrays.toString(dp)); } recur(i-A,A,B,dp,metal,n,took,ind); recur(i-B,A,B,dp,metal,n,took,ind); } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; 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; } public static boolean[] getSieve(int n) { boolean[] isPrime = new boolean[n+1]; for (int i = 2; i <= n; i++) isPrime[i] = true; for (int i = 2; i*i <= n; i++) if (isPrime[i]) for (int j = i; i*j <= n; j++) isPrime[i*j] = false; return isPrime; } static int gcd(int a, int b) { if(a == 0) return b; return gcd(b%a,a); } public static void dfs(LinkedList<Integer>[]list, HashMap<Integer,Integer>map, int parent, int n) { Stack<Integer>st = new Stack<>(); } public static boolean pos(int n) { int i = 1; boolean pos = false; while(i*i <= n) { if(i*i*2 == n || i*i*4 == n) { pos = true; break; } i++; } if(pos)return true; return false; } static long count = 0; public static void pairs (int []ar, int s, int e) { if(e <= s)return; // System.out.println(ar[s]+" "+ar[e]+" "+s+" "+e); if(ar[e]-ar[s] == e-s) { count++; //System.out.println("sdf"); } pairs(ar,s+1,e); pairs(ar,s,e-1); } public static long ways(long n) { return (n*(n-1))/2; } static boolean isPrime(long n) { if(n <= 1)return false; if(n <= 3)return true; if(n%2 == 0 || n%3 == 0)return false; for(int i = 5; i*i <= n; i+= 6) { if(n%i == 0 || n%(i+2) == 0) return false; } return true; } static long nextPrime(long n) { boolean found = false; long prime = n; while(!found) { prime++; if(isPrime(prime)) found = true; } return prime; } public static boolean isValid(int h, int m, int hour, int minute) { int a = flip(hour / 10); if (a == -1) return false; int b = flip(hour % 10); if (b == -1) return false; int c = flip(minute / 10); if (c == -1) return false; int d = flip(minute % 10); if (d == -1) return false; if (10 * d + c >= h) return false; if (10 * b + a >= m) return false; return true; } public static int flip(int x) { if (x == 0) return 0; if (x == 1) return 1; if (x == 2) return 5; if (x == 5) return 2; if (x == 8) return 8; return -1; } static long maximum(long a, long b, long c, long d) { long m = Math.max(a, b); long m1 = Math.max(c, d); return Math.max(m1, m1); } static long minimum(long a, long b, long c,long d) { long m = Math.min(a, b); long m1 = Math.min(c, d); return Math.min(m, m1); } static long ans = 0; public static void solve1(boolean [][]vis,long [][]mat, int r, int c, int r2, int c2, int r1, int c1, int r3, int c3) { if(r > r1 || c > c1 || r > r2 || c > c2 || r1 > r3 || c1 > c3 || r3 < r2 || c3 < c2 || vis[r][c] || vis[r1][c1]|| vis[r2][c2] || vis[r3][c3]) return; vis[r][c] = true; vis[r1][c1] = true; vis[r2][c2] = true; vis[r3][c3] = true; long max = maximum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long min = minimum(mat[r][c],mat[r1][c1],mat[r2][c2],mat[r3][c3]); long a = mat[r][c]; long b = mat[r1][c1]; long c4 = mat[r2][c2]; long d =mat[r3][c3]; long []p = {a,b,c4,d}; Arrays.sort(p); long temp = (p[2]+p[3]-p[0]-p[1]); if(r == r1 && r == r2 && r2 == r3 && r1 == r3) temp /= 2; System.out.println(Arrays.toString(p)); ans += temp; solve1(vis,mat,r+1,c,r2+1,c2,r1-1,c1,r3-1,c3); solve1(vis,mat,r,c+1,r2,c2-1,r1,c1+1,r3,c3-1); solve1 (vis,mat,r+1,c+1,r2+1,c2-1,r1-1,c1+1,r3-1,c3-1); } private static int solve(int[][] mat, int i, int c, int j, int c2, int k, int c1, int l, int c3) { // TODO Auto-generated method stub return 0; } public static int dfs(int parent, LinkedList<Integer>[]list) { for(int i : list[parent]) { if(list[parent].size() == 0) { return 0; }else { return 1 + dfs(i,list); } } return 0; } public static long answer = Integer.MAX_VALUE; public static void min_Time(int [][]dp, int i, HashSet<Integer>set, int min, int r, int c) { if(i > r) { answer = Math.min(answer, min); return; } if(min > answer)return; for(int j = i; j <= c; j++) { if(!set.contains(j)) { set.add(j); min += dp[i][j]; min_Time(dp,i+1,set,min,r,c); min -= dp[i][j]; set.remove(j); } } } public static void dp(int [][]dp, int r, int c, int o, int z, long sum) { if(r > o) { answer = Math.min(answer, sum); } if(r > o || c > z) { return; } if(sum > answer)return; sum += dp[r][c]; dp(dp,r+1,c+1,o,z,sum); sum -= dp[r][c]; dp(dp,r,c+1,o,z,sum); } static HashSet<ArrayList<Integer>>l = new HashSet<>(); public static void fourSum(Deque<Integer>ll, int i, int target, int []ar, int n) { if(ll.size() == 4) { int sum = 0; ArrayList<Integer>list = new ArrayList<>(); for(int a : ll) { sum += a; list.add(a); } if(sum == target) { Collections.sort(list); l.add(list); // System.out.println(ll); } return; } for(int j = i; j < n; j++) { ll.add(ar[j]); fourSum(ll,j+1,target,ar,n); ll.removeLast(); } } static int max_bottles(int cur, int exchange, int n){ if(cur == exchange){ cur = 0; n++; } if(n == 0)return 0; return 1+ max_bottles(cur+1,exchange,n-1); } public static void fill(int [][]mat, List<Integer>ans, int row_start, int row_end, int col_start, int col_end) { for(int i = col_start; i <= col_end; i++) { ans.add(mat[row_start][i]); } for(int i = row_start+1; i <= row_end; i++) { ans.add(mat[i][col_end]); } if(col_start == col_end)return; if(row_start == row_end)return; for(int i = col_end-1; i >= col_start; i--) { ans.add(mat[row_end][i]); } for(int i = row_end-1; i >= row_start+1; i--) { ans.add(mat[i][col_start]); } } public static void create(int [][]mat, int j, int i, int k) { if(i < 1 || j >= mat.length)return; mat[j][i] = k; create(mat,j+1,i-1,k+1); } public static long sum(int [][]mat, int x1, int y1, int x2, int y2) { long sum = 0; while(x1 <= x2) { sum += mat[x1][y1]; // System.out.println(mat[x1][y1]); x1++; } y1++; while(y1 <= y2) { sum += mat[x2][y1]; y1++; } return sum; } public static boolean allneg(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] >= 0)return false; } return true; } public static boolean allpos(int []ar, int n) { for(int i = 0; i < n; i++) { if(ar[i] <= 0)return false; } return true; } public static int max_pos(int []ar, int n) { int min = Integer.MAX_VALUE; for(int i = 1; i < n; i++) { if(ar[i] > 0) { break; } int a = Math.abs(ar[i]-ar[i-1]); min = Math.min(min, a); } int c = 0; boolean zero = false; TreeSet<Integer>set = new TreeSet<>(); int neg = 0; for(int i = 0; i < n; i++) { if(ar[i] <= 0) {neg++; if(ar[i] == 0) zero = true; continue;} if(ar[i] <= min) { c = 1; } } neg += c; return neg; } static final int MAX = 10000000; // prefix[i] is going to store count // of primes till i (including i). static int prefix[] = new int[MAX + 1]; static void buildPrefix() { // Create a boolean array "prime[0..n]". A // value in prime[i] will finally be false // if i is Not a prime, else true. boolean prime[] = new boolean[MAX + 1]; Arrays.fill(prime, true); for (int p = 2; p * p <= MAX; p++) { // If prime[p] is not changed, then // it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * 2; i <= MAX; i += p) prime[i] = false; } } // Build prefix array prefix[0] = prefix[1] = 0; for (int p = 2; p <= MAX; p++) { prefix[p] = prefix[p - 1]; if (prime[p]) prefix[p]++; } } static int query(int L, int R) { return prefix[R] - prefix[L - 1]; } static void alter(int n) { int ans = 0; boolean []vis = new boolean[n+1]; for(int i = 2; i <= n; i++){ boolean p = false; if(vis[i] == false) { for(int j = i; j <= n; j+=i) { if(vis[j] == true) { p = true; }else { vis[j] = true; } } if(!p)ans++; } } System.out.println(ans); } public static void solveDK(int []dp, int i, int D, int K) { int d = D/K; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } public static void solveKD(int []dp, int i, int D, int K) { int d = K/D; int ans = -1; int ind = d+1; while(ind < i) { int temp = i/ind; temp--; if(dp[temp*ind] == temp) { ans = dp[temp*ind]+1; dp[i] = ans; break; } ind = ind*2; } if(ans == -1) dp[i] = 1; } static int countGreater(int arr[], int n, int k) { int l = 0; int r = n - 1; // Stores the index of the left most element // from the array which is greater than k int leftGreater = n; // Finds number of elements greater than k while (l <= r) { int m = l + (r - l) / 2; // If mid element is greater than // k update leftGreater and r if (arr[m] > k) { leftGreater = m; r = m - 1; } // If mid element is less than // or equal to k update l else l = m + 1; } // Return the count of elements greater than k return (n - leftGreater); } static ArrayList<Integer>printDivisors(int n) { // Note that this loop runs till square root ArrayList<Integer>list = new ArrayList<>(); for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { // If divisors are equal, print only one if (n/i == i) list.add(i); else // Otherwise print both list.add(i); list.add(n/i); } } return list; } static List<List<Integer>>result = new ArrayList<>(); public static void comb(List<Integer>list, int n, int i, int []ar) { if(i >= n)return; result.add(new ArrayList<>(list)); for(int j = i; j < n; j++) { list.add(ar[j]); comb(list,n,i+1,ar); list.remove(list.size()-1); } return; } static boolean isPossible(String s, String str, int i, int j) { // System.out.println(i+" "+j); int x = i; int y = j; while(i >= 0 && j < str.length()) { if(s.charAt(i) != str.charAt(j)) { break; } i--; j++; } if(j == str.length()) { System.out.println(x+" "+y); return true; } return false; } static void leftRotate(int l, int r,int arr[], int d) { for (int i = 0; i < d; i++) leftRotatebyOne(l,r,arr); } static void leftRotatebyOne(int l, int r,int arr[]) { int i, temp; temp = arr[l]; for (i = l; i < r; i++) arr[i] = arr[i + 1]; arr[r] = temp; } static class Pair { int x; int y; // Constructor public Pair(int x, int y) { this.x = x; this.y = y; } } static class Compare1{ static void compare(Pair arr[], int n) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.x - p2.x; } }); } } static long modInverse(long a, long m) { long m0 = m; long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { // q is quotient long q = a / m; long t = m; // m is remainder now, process // same as Euclid's algo m = a % m; a = t; t = y; // Update x and y y = x - q * y; x = t; } // Make x positive if (x < 0) x += m0; return x; } static long power(long x, long y, long p) { long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p if (x == 0) return 0; // In case x is divisible by p; while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static long solvetheproblem(long n, long k) { long mod = 1000000007; long ansss = 0; while(n > 0) { // Nearest power of 2<=N long p = (long)(Math.log(n) / Math.log(2));; // Now insert k^p in the answer long temp = (long)power(k,p,mod); ansss += temp; ansss %= mod; // update n n %= (long)Math.pow(2, p); } // Print the ans in sorted order return ansss%mod ; } static boolean pos (int [][]mat, int r, int c, int n, boolean [][]vis) { if(r <= 0 || c <= 0 || r > 2 || c > n )return false; if(r == 2 && c == n)return true; if(vis[r][c])return false; vis[r][c] = true; if(mat[r][c] == 1)return false; boolean a = pos(mat,r+1,c,n,vis); boolean b = pos(mat,r,c+1,n,vis); boolean d = pos(mat,r+1,c+1,n,vis); boolean e = pos(mat,r-1,c+1,n,vis); return a || b || d || e; } static long sameremdiv(long x, long y) { if(x <= y) { y = x-1; } long sq = (long)Math.sqrt(x); if(y <= sq) { y--; long ans = (y*(y+1))/2; return ans; }else { long ans = 0; long dif = y-sq; sq -= 2; if(sq > 1) ans += (sq*(sq+1))/2; long d = x/y; sq+=2; for(int i = (int)sq; i <= y; i++) { if(i > 1 ) { long temp = x/i; if(x%i < temp)temp--; ans += temp; } } return ans; } } static int binary(long []ar, long element, int s, int e) { int mid = (s+e)/2; // System.out.println(mid); if(s > e)return mid; if(ar[mid] == element) { return mid; }else if(ar[mid] > element) { return binary(ar,element,s,mid-1); }else { return binary(ar,element,mid+1,e); } } static boolean isGibbrish(HashSet<String>set, String str, int j) { StringBuilder sb = new StringBuilder(); if(j >= str.length()) { return true; } for(int i = j; i < str.length(); i++) { sb.append(str.charAt(i)); String temp = sb.toString(); if(set.contains(temp)) { boolean test = isGibbrish(set,str,i+1); if(test)return true; } } return false; } public static void main(String args[])throws IOException { // System.setIn(new FileInputStream("Case.txt")); BufferedReader ob = new BufferedReader(new InputStreamReader(System.in)); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out)); int t = Integer.parseInt(ob.readLine()); while(t --> 0) { StringTokenizer st = new StringTokenizer(ob.readLine()); int n = Integer.parseInt(st.nextToken()); long k = Long.parseLong(st.nextToken()); long sum = 0; int []ar = new int[n]; st = new StringTokenizer(ob.readLine()); for(int i = 0; i < n; i++) { ar[i] = Integer.parseInt(st.nextToken()); } int []aa = new int[10]; for(int i = 0; i < 10; i++) { int temp = (int)Math.pow(10, i); aa[i] = temp; } long ans = 0; for(int i = 0; i < n; i++) { long ki = (i == n-1? k+1: Math.min(k+1, aa[ar[i+1]-ar[i]]-1)); ans += aa[ar[i]]*ki; if(ki == k+1)break; k -= ki; } System.out.println(ans); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
d936366cf52b6eb20172237e56961c89
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class P3B { public static void main(String[] args) { FastIO io = new FastIO(); int t = io.nextInt(); while (t --> 0) { int n = io.nextInt(); int k = io.nextInt(); long[] array = new long[n]; for (int i=0; i<n; i++) { array[i] = io.nextInt(); } int count = 0, index = 0; long answer = 0; while (count < k+1) { if (index+1 < n) { long temp = (long)Math.pow(10, array[index+1] - array[index]) - 1; temp = Math.min(k+1-count, temp); count += temp; answer += temp * Math.pow(10, array[index]); } else { int temp = k+1 - count; count += temp; answer += temp * (long)Math.pow(10, array[index]); } index++; } io.println(answer); } io.close(); } static long calculate(long[] array, long k) { int pointer = array.length-1, answer = 0; while (k>0) { if (array[pointer] <= k) { answer += k/array[pointer]; k = k%array[pointer]; } pointer--; } return answer; } static class FastIO extends PrintWriter { BufferedReader br; StringTokenizer st; public FastIO() { super(System.out); br = new BufferedReader(new InputStreamReader(System.in)); } public String next() { try { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } catch (Exception e) { return null; } } public int nextInt() { return Integer.parseInt(next()); } public double nextDouble() { return Double.parseDouble(next()); } public long nextLong() { return Long.parseLong(next()); } public void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } super.close(); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
b99f320c5f24c57a4eaaacec8bac93c3
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; import java.math.*; public class Coder { static StringBuffer str=new StringBuffer(); static int n; static long k; static long a[]; /* Idea i got: Maximize the smaller number of bank notes so that we can minimize s and also at the same time, we can maximize the f(s) Idea i lost which will lead to the solution(confused me a lot): The key idea: We can take as many i coins until it does not exceed (i+1)th coin denomination */ static void solve(){ long ans=0; for(int i=0;i<n;i++){ if(k < 0) break; long num=k+1; if(i!=n-1){ num=Math.min(num, a[i+1]/a[i] - 1); } k-=num; ans+=num*a[i]; } str.append(ans).append("\n"); } public static void main(String[] args) throws java.lang.Exception { BufferedReader bf; PrintWriter pw; boolean lenv=false; if(lenv){ bf = new BufferedReader( new FileReader("input.txt")); pw=new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); }else{ bf = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new OutputStreamWriter(System.out)); } int q = Integer.parseInt(bf.readLine().trim()); while (q-- > 0) { String s[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(s[0]); k=Long.parseLong(s[1]); a=new long[n]; s=bf.readLine().trim().split("\\s+"); for(int i=0;i<n;i++) a[i]=(long)Math.pow(10, Long.parseLong(s[i])); solve(); } pw.print(str); pw.flush(); // System.out.print(str); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
44c55ff5c26c5548e9bb340b476e79a4
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.math.BigInteger; import java.util.*; import static java.lang.Math.PI; import static java.lang.System.in; import static java.lang.System.out; import static java.lang.System.err; public class A { public static void main(String[] args) throws Exception { Foster sc = new Foster(); PrintWriter p = new PrintWriter(out); /* * Is that extra condition needed * Check overflow in pow function or in general * Check indices of read array functions * Think of an easier solution because the problems you solve are always easy * Check the iterator of loop */ int t = sc.nextInt(); while(t--!=0) { int n = sc.nextInt(); long k = sc.nextLong(); long a[] = new long[n]; for(int i = 0; i < n; i++) { long x = sc.nextLong(); a[i] = (long)Math.pow(10, x); } long maxFree[] = new long[n]; for(int i = 0; i < n-1; i++) { maxFree[i] = a[i+1]-a[i]; } long prefix[] = Arrays.copyOf(maxFree, n); for(int i = 1; i < n; i++) { prefix[i] += prefix[i-1]; } long prev = 0; k++; long ans = 0; ans = k-prev; prev += maxFree[0]/a[0]; for(int i = 1; i < n; i++) { if(ans >= a[i]) { ans = prefix[i-1] + ((k-prev)*a[i]); prev += maxFree[i]/a[i]; } } p.println(ans); } p.close(); } static long[] sort(long a[]) { ArrayList<Long> arr = new ArrayList<>(); for(long i : a) { arr.add(i); } Collections.sort(arr); Collections.reverse(arr); for(int i = 0; i < arr.size(); i++) { a[i] = arr.get(i); } return a; } static int[] sort(int a[]) { ArrayList<Integer> arr = new ArrayList<>(); for(int i : a){ arr.add(i); } Collections.sort(arr); // Collections.reverse(arr); for(int i = 0; i < arr.size(); i++){ a[i] = arr.get(i); } return a; } static class Foster { BufferedReader br = new BufferedReader(new InputStreamReader(in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!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()); } int[] intArray(int n) { int arr[] = new int[n]; for(int i = 0; i < n; i++) { arr[i] = nextInt(); } return arr; } long[] longArray(int n) { long arr[] = new long[n]; for(int i = 0; i < n; i++) { arr[i] = nextLong(); } return arr; } static long pow(long... a) { long mod = Long.MAX_VALUE; if(a.length == 3) mod = a[2]; long res = 1; while(a[1] > 0) { if((a[1] & 1) == 1) res = (res * a[0]) % mod; a[1] /= 2; a[0] = (a[0] * a[0]) % mod; } return res; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
55921e39842342973e378814257ef263
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; import java.math.BigDecimal; import java.math.*; public class Main{ public static void main(String[] args) { TaskA solver = new TaskA(); // boolean[]prime=seive(3*100001); int t = in.nextInt(); for (int i = 1; i <= t ; i++) { solver.solve(i, in, out); } // solver.solve(1, in, out); out.flush(); out.close(); } static class TaskA { public void solve(int testNumber, InputReader in, PrintWriter out) { int n= in.nextInt(); int k= in.nextInt()+1; long []arr=new long[n]; for(int i=0;i<n;i++) { arr[i]=(long)Math.pow(10, in.nextInt()); } int count=0;long ans=0;int i=0; for( i=0;i<n-1&&count<k;i++) { count+=((arr[i+1]/(arr[i])-1)); ans+=((arr[i+1]/arr[i])-1)*arr[i]; } if(count<k) { ans+=(k-count)*arr[i]; } else { ans-=(count-k)*arr[i-1]; } System.out.println(ans); } } static int net(int []arr,int index,int num) { int count=0; for(int i=index;i>=0;i--) { count += num/Math.pow(10, arr[i]); num%=Math.pow(10, arr[index]); } return count; } static long pow(long b, long e) { long ans = 1; while (e > 0) { if (e % 2 == 1) ans = ans * b % mod; e >>= 1; b = b * b % mod; } return ans; } static int[]dp; //global variable static int minStep(int n) { if(n<0) { return Integer.MAX_VALUE; //this is never possible should be handled bcz n-1,n-5 can be negative } if(n==0) {return 0;} if(dp[n]!=0) { return dp[n]; //if solution we have already calculated } else { int x=1+Math.min(minStep(n-1),Math.min(minStep(n-3),minStep(n-5))); dp[n]=x; //update the array after calculation return x; } } static void sortF(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { if(p1.first==p2.first) { return p1.second-p2.second; } return p1.first - p2.first; } }); } static int[] shift (int[]inp,int i,int j){ int[]arr=new int[j-i+1]; int n=j-i+1; for(int k=i;k<=j;k++) { arr[(k-i+1)%n]=inp[k]; } for(int k=0;k<n;k++ ) { inp[k+i]=arr[k]; } return inp; } static int sumDigits(long n) { int total=0; while(n!=0) { total+=n%10; n=n/10; } return total; } static long[] fac; static long mod = (long) 1000000007; static void initFac(long n) { fac = new long[(int)n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) { fac[i] = (fac[i - 1] * i) % mod; } } static long nck(int n, int k) { if (n < k) return 0; long den = inv((int) (fac[k] * fac[n - k] % mod)); return fac[n] * den % mod; } static int[]MakeArr(int n){ int[]arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=in.nextInt(); } return arr; } static int[]arr(){ int n= in.nextInt(); int[]arr=new int[n]; for(int i=0;i<n;i++) { arr[i]=in.nextInt(); } return arr; } static long inv(long x) { return pow(x, mod - 2); } static void sort(int[] a) { ArrayList<Integer> q = new ArrayList<>(); for (int i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } static void sort(long[] a) { ArrayList<Long> q = new ArrayList<>(); for (long i : a) q.add(i); Collections.sort(q); for (int i = 0; i < a.length; i++) a[i] = q.get(i); } public static int bfsSize(int source,LinkedList<Integer>[]a,boolean[]visited) { Queue<Integer>q=new LinkedList<>(); q.add(source); visited[source]=true; int distance=0; while(!q.isEmpty()) { int curr=q.poll(); distance++; for(int neighbour:a[curr]) { if(!visited[neighbour]) { visited[neighbour]=true; q.add(neighbour); } } } return distance; } public static Set<Integer>factors(int n){ Set<Integer>ans=new HashSet<>(); ans.add(1); for(int i=2;i*i<=n;i++) { if(n%i==0) { ans.add(i); ans.add(n/i); } } return ans; } public static int bfsSp(int source,int destination,ArrayList<Integer>[]a) { boolean[]visited=new boolean[a.length]; int[]parent=new int[a.length]; Queue<Integer>q=new LinkedList<>(); int distance=0; q.add(source); visited[source]=true; parent[source]=-1; while(!q.isEmpty()) { int curr=q.poll(); if(curr==destination) { break; } for(int neighbour:a[curr]) { if(neighbour!=-1&&!visited[neighbour]) { visited[neighbour]=true; q.add(neighbour); parent[neighbour]=curr; } } } int cur=destination; while(parent[cur]!=-1) { distance++; cur=parent[cur]; } return distance; } static int bs(int size,int[]arr) { int x = -1; for (int b = size/2; b >= 1; b /= 2) { while (!ok(arr)); } int k = x+1; return k; } static boolean ok(int[]x) { return false; } public static int solve1(ArrayList<Integer> A) { long[]arr =new long[A.size()+1]; int n=A.size(); for(int i=1;i<=A.size();i++) { arr[i]=((i%2)*((n-i+1)%2))%2; arr[i]%=2; } int ans=0; for(int i=0;i<A.size();i++) { if(arr[i+1]==1) { ans^=A.get(i); } } return ans; } public static String printBinary(long a) { String str=""; for(int i=31;i>=0;i--) { if((a&(1<<i))!=0) { str+=1; } if((a&(1<<i))==0 && !str.isEmpty()) { str+=0; } } return str; } public static String reverse(long a) { long rev=0; String str=""; int x=(int)(Math.log(a)/Math.log(2))+1; for(int i=0;i<32;i++) { rev<<=1; if((a&(1<<i))!=0) { rev|=1; str+=1; } else { str+=0; } } return str; } //////////////////////////////////////////////////////// static void sortS(Pair arr[]) { // Comparator to sort the pair according to second element Arrays.sort(arr, new Comparator<Pair>() { @Override public int compare(Pair p1, Pair p2) { return p1.second - p2.second; } }); } static class Pair implements Comparable<Pair> { int first ;int second ; public Pair(int x, int y) { this.first = x ;this.second = y ; } @Override public boolean equals(Object obj) { if(obj == this)return true ; if(obj == null)return false ; if(this.getClass() != obj.getClass()) return false ; Pair other = (Pair)(obj) ; if(this.first != other.first)return false ; if(this.second != other.second)return false ; return true ; } @Override public int hashCode() { return this.first^this.second ; } @Override public String toString() { String ans = "" ;ans += this.first ; ans += " "; ans += this.second ; return ans ; } @Override public int compareTo(Main.Pair o) { { if(this.first==o.first) { return this.second-o.second; } return this.first - o.first; } } } ////////////////////////////////////////////////////////////////////////// static int nD(long num) { String s=String.valueOf(num); return s.length(); } static int CommonDigits(int x,int y) { String s=String.valueOf(x); String s2=String.valueOf(y); return 0; } static int lcs(String str1, String str2, int m, int n) { int L[][] = new int[m + 1][n + 1]; int i, j; // Following steps build L[m+1][n+1] in // bottom up fashion. Note that L[i][j] // contains length of LCS of str1[0..i-1] // and str2[0..j-1] for (i = 0; i <= m; i++) { for (j = 0; j <= n; j++) { if (i == 0 || j == 0) L[i][j] = 0; else if (str1.charAt(i - 1) == str2.charAt(j - 1)) L[i][j] = L[i - 1][j - 1] + 1; else L[i][j] = Math.max(L[i - 1][j], L[i][j - 1]); } } // L[m][n] contains length of LCS // for X[0..n-1] and Y[0..m-1] return L[m][n]; } ///////////////////////////////// boolean IsPowerOfTwo(int x) { return (x != 0) && ((x & (x - 1)) == 0); } //////////////////////////////// static long power(long a,long b,long m ) { long ans=1; while(b>0) { if(b%2==1) { ans=((ans%m)*(a%m))%m; b--; } else { a=(a*a)%m;b/=2; } } return ans%m; } /////////////////////////////// public static boolean repeatedSubString(String string) { return ((string + string).indexOf(string, 1) != string.length()); } static int search(char[]c,int start,int end,char x) { for(int i=start;i<end;i++) { if(c[i]==x) {return i;} } return -2; } //////////////////////////////// static int gcd(int a, int b) { while (b != 0) { int t = b; b = a % b; a = t; } return a; } static long fac(long a) { if(a== 0L || a==1L)return 1L ; return a*fac(a-1L) ; } static ArrayList al() { ArrayList<Integer>a =new ArrayList<>(); return a; } static HashSet h() { return new HashSet<Integer>(); } static void debug(int[][]a) { int n= a.length; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { out.print(a[i][j]+" "); } out.print("\n"); } } static void debug(int[]a) { out.println(Arrays.toString(a)); } static void debug(ArrayList<Integer>a) { out.println(a.toString()); } static boolean[]seive(int n){ boolean[]b=new boolean[n+1]; for (int i = 2; i <= n; i++) b[i] = true; for(int i=2;i*i<=n;i++) { if(b[i]) { for(int j=i*i;j<=n;j+=i) { b[j]=false; } } } return b; } static int longestIncreasingSubseq(int[]arr) { int[]sizes=new int[arr.length]; Arrays.fill(sizes, 1); int max=1; for(int i=1;i<arr.length;i++) { for(int j=0;j<i;j++) { if(arr[j]<arr[i]) { sizes[i]=Math.max(sizes[i],sizes[j]+1); max=Math.max(max, sizes[i]); } } } return max; } public static ArrayList primeFactors(long n) { ArrayList<Long>h= new ArrayList<>(); // Print the number of 2s that divide n if(n%2 ==0) {h.add(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+= 2) { if(n%i==0) {h.add(i);} } if (n > 2) h.add(n); return h; } static boolean Divisors(long n){ if(n%2==1) { return true; } for (long i=2; i<=Math.sqrt(n); i++){ if (n%i==0 && i%2==1){ return true; } } return false; } static InputStream inputStream = System.in; static OutputStream outputStream = System.out; static InputReader in = new InputReader(inputStream); static PrintWriter out = new PrintWriter(outputStream); public static void superSet(int[]a,ArrayList<String>al,int i,String s) { if(i==a.length) { al.add(s); return; } superSet(a,al,i+1,s); superSet(a,al,i+1,s+a[i]+" "); } public static long[] makeArr() { long size=in.nextInt(); long []arr=new long[(int)size]; for(int i=0;i<size;i++) { arr[i]=in.nextInt(); } return arr; } public static long[] arr(int n) { long []arr=new long[n+1]; for(int i=1;i<n+1;i++) { arr[i]=in.nextLong(); } return arr; } public static void sort(long arr[], int l, int r) { if (l < r) { // Find the middle point int m = (l+r)/2; // Sort first and second halves sort(arr, l, m); sort(arr , m+1, r); // Merge the sorted halves merge(arr, l, m, r); } } static void print(int c) { out.print(c); } static void println(int x) { out.println(x); } static void print(String s) { out.print(s); } static void println(String s) { out.println(s); } public static void merge(long arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ long L[] = new long[n1]; long R[] = new long[n2]; //Copy data to temp arrays for (int i=0; i<n1; ++i) L[i] = arr[l + i]; for (int j=0; j<n2; ++j) R[j] = arr[m + 1+ j]; /* Merge the temp arrays */ // Initial indexes of first and second subarrays int i = 0, j = 0; // Initial index of merged subarry array int k = l; while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; } /* Copy remaining elements of L[] if any */ while (i < n1) { arr[k] = L[i]; i++; k++; } /* Copy remaining elements of R[] if any */ while (j < n2) { arr[k] = R[j]; j++; k++; } } public static void reverse(int[] array) { int n = array.length; for (int i = 0; i < n / 2; i++) { int temp = array[i]; array[i] = array[n - i - 1]; array[n - i - 1] = temp; } } public static long nCr(int n,int k) { long ans=1L; k=k>n-k?n-k:k; for( int j=1;j<=k;j++,n--) { if(n%j==0) { ans*=n/j; }else if(ans%j==0) { ans=ans/j*n; }else { ans=(ans*n)/j; } } return ans; } static int searchMax(int index,long[]inp) { while(index+1<inp.length &&inp[index+1]>inp[index]) { index+=1; } return index; } static int searchMin(int index,long[]inp) { while(index+1<inp.length &&inp[index+1]<inp[index]) { index+=1; } return index; } public class ListNode { int val; ListNode next; ListNode() {} ListNode(int val) { this.val = val; } ListNode(int val, ListNode next) { this.val = val; this.next = next; } } static class Pairr implements Comparable<Pairr>{ private int index; private int cumsum; private ArrayList<Integer>indices; public Pairr(int index,int cumsum) { this.index=index; this.cumsum=cumsum; indices=new ArrayList<Integer>(); } public int compareTo(Pairr other) { return Integer.compare(cumsum, other.cumsum); } } 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()); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
20d519d138f944deeb4a5f146a3db874
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; public class code{ 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 readLine() 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(); } } public static int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } //@SuppressWarnings("unchecked") public static void main(String[] arg) throws IOException{ //Reader in=new Reader(); PrintWriter out = new PrintWriter(System.out); Scanner in = new Scanner(System.in); int t=in.nextInt(); while(t-- > 0){ int n=in.nextInt(); long k=in.nextLong(); long[] val=new long[n]; for(int i=0;i<n;i++){ int j=in.nextInt(); val[i]=(long)Math.pow(10,j); } long res=0; for(int i=0;i<n-1;i++){ if(k<0) break; long value=Math.min((val[i+1]-val[i])/val[i],k+1); res+=val[i]*value; //out.println(value+" "+k); k-=value; } if(k<0) out.println(res); else{ res+=val[n-1]*(k+1); out.println(res); } } out.flush(); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
468d0e0fc42bfdcaca2107a3008ec709
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.lang.*; import java.io.*; public class C_Banknotes { static int M = 1_000_000_007; static final PrintWriter out =new PrintWriter(System.out); static final FastReader fs = new FastReader(); static boolean prime[]; public static void main (String[] args) throws java.lang.Exception { int t= fs.nextInt(); for(int i=0;i<t;i++) { int n=fs.nextInt(); int k=fs.nextInt(); int a[]=fs.arrayIn(n); int fgot=0; long gg=0; long ans=0; int kk=0; int l=0; for(int j=0;j<=9;j++){ if(kk<n&&a[kk]==j){ l=a[kk]; kk++; } if(kk==n){ ans=ans+(long)(k+1)*((long)Math.pow(10,l)); break; } long wee=(long)9*(long)(Math.pow(10,(j-l))); if(k>=wee){ ans=ans+(long)9*(long)Math.pow(10,j); k=k-(int)wee; }else{ ans=ans+(long)(k+1)*((long)Math.pow(10,l)); break; } } out.println(ans); } out.flush(); } public static long power(long x, long y) { long temp; if (y == 0) return 1; temp = power(x, y / 2); if (y % 2 == 0) return modMult(temp,temp); else { if (y > 0) return modMult(x,modMult(temp,temp)); else return (modMult(temp,temp)) / x; } } static void sieveOfEratosthenes(int n) { prime = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; prime[0]=false; if(1<=n) prime[1]=false; for (int p = 2; p * p <= n; p++) { if (prime[p] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } 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; } int [] arrayIn(int n) throws IOException { int arr[] = new int[n]; for(int i=0; i<n; i++) { arr[i] = nextInt(); } return arr; } } public static class Pairs implements Comparable<Pairs> { int value,index; Pairs(int value, int index) { this.value = value; this.index = index; } public int compareTo(Pairs p) { return Integer.compare(this.value, p.value); } } static final Random random = new Random(); static void ruffleSort(int arr[]) { int n = arr.length; for(int i=0; i<n; i++) { int j = random.nextInt(n),temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static long nCk(int n, int k) { return (modMult(fact(n),fastexp(modMult(fact(n-k),fact(k)),M-2))); } static long fact (long n) { long fact =1; for(int i=1; i<=n; i++) { fact = modMult(fact,i); } return fact%M; } static long modMult(long a,long b) { return a*b%M; } static long fastexp(long x, int y){ if(y==1) return x; long ans = fastexp(x,y/2); if(y%2 == 0) return modMult(ans,ans); else return modMult(ans,modMult(ans,x)); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
e4fd17b81414f4c7b04a3ed1fc3ca961
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class Practice { public static long mod = (long) Math.pow(10, 9) + 7; public static long mod2 = 998244353; public static int tt = 0; public static ArrayList<Integer> prime; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); int t = Integer.parseInt(br.readLine()); int p = 1; // System.out.println(list.size()); while (t-- > 0) { String[] s1 = br.readLine().split(" "); int n = Integer.valueOf(s1[0]); long k = Long.valueOf(s1[1]); long[] arr = new long[n]; String str = (br.readLine()); String[] s2 = str.split(" "); for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(s2[i]); } long curr = 0; String no = ""; for (int i = 1; i < n; i++) { long c = (long) Math.pow(10, arr[i] - arr[i - 1]) - 1; if (curr + c <= k + 1) { curr += c; for (int j = 0; j < arr[i] - arr[i - 1]; j++) { no = '9' + no; } } else { long pp = k + 1 - curr; curr = k + 1; if (pp == 0) { break; } no = pp + no; break; } // System.out.println(curr); } if (curr < k + 1) { long need = k + 1 - curr; no = need + no; } pw.println(no); } pw.close(); } } // private static void getFac(long n, PrintWriter pw) { // // TODO Auto-generated method stub // int a = 0; // while (n % 2 == 0) { // a++; // n = n / 2; // } // if (n == 1) { // a--; // } // for (int i = 3; i <= Math.sqrt(n); i += 2) { // while (n % i == 0) { // n = n / i; // a++; // } // } // if (n > 1) { // a++; // } // if (a % 2 == 0) { // pw.println("Bob"); // } else { // pw.println("Alice"); // } // //System.out.println(a); // return; // } // private static long power(long a, long p) { // // TODO Auto-generated method stub // long res = 1; // while (p > 0) { // if (p % 2 == 1) { // res = (res * a) % mod; // } // p = p / 2; // a = (a * a) % mod; // } // return res; // } // // private static void fac() { // fac[0] = 1; // // TODO Auto-generated method stub // for (int i = 1; i < fac.length; i++) { // if (i == 1) { // fac[i] = 1; // } else { // fac[i] = i * fac[i - 1]; // } // if (fac[i] > mod) { // fac[i] = fac[i] % mod; // } // } // } // // private static int getLower(Long long1, Long[] st) { // // TODO Auto-generated method stub // int left = 0, right = st.length - 1; // int ans = -1; // while (left <= right) { // int mid = (left + right) / 2; // if (st[mid] <= long1) { // ans = mid; // left = mid + 1; // } else { // right = mid - 1; // } // } // return ans; // } // private static long getGCD(long l, long m) { // // long t1 = Math.min(l, m); // long t2 = Math.max(l, m); // while (true) { // long temp = t2 % t1; // if (temp == 0) { // return t1; // } // t2 = t1; // t1 = temp; // } // }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
2d9b2fbe3c156fb3a23b59fb1c4d0a0e
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class Main { public int isNegativeWeightCycle(int n, int[][] edges) { //code here int dis[]=new int[n]; Arrays.fill(dis,Integer.MAX_VALUE); for (int i=1;i<n;i++){ for (int j=0;j<edges.length;j++){ if (dis[edges[j][0]]+edges[j][2]<dis[edges[j][1]]){ dis[edges[j][1]]=dis[edges[j][0]]+edges[j][2]; } } } for (int i=0;i<edges.length;i++){ if (dis[edges[i][0]]+edges[i][2]<dis[edges[i][1]]){ return 1; } } return 0; } static int[] bellman_ford(int V, ArrayList<ArrayList<Integer>> adj, int S) { // Write your code here int dis[]= new int[V]; Arrays.fill(dis,Integer.MAX_VALUE); dis[S]=0; for (int i=1;i<V;i++){ for (int j=0;j<adj.size();j++){ if (adj.get(j).get(0)+adj.get(j).get(2)<adj.get(j).get(1)){ adj.get(j).add(adj.get(j).get(0)+adj.get(j).get(2)); } } } return dis; } public static void main(String[] args) { FastScanner sc = new FastScanner(); // Scanner sc = new Scanner(System.in); int t=sc.nextInt(); outer:while (t-->=1){ int x=sc.nextInt(); long y=sc.nextLong(); y++; long sol=0; int a[]=sc.readArray(x); for (int i=0;i<x-1;i++){ if (y>0){ long z=(long) Math.pow(10,a[i+1]-a[i])-1; long val=Math.min(z,y); sol=sol+(long) (Math.pow(10,a[i])*val); y=y-val; } } sol=sol+(long) (y*Math.pow(10,a[x-1])); System.out.println(sol); } } // out.flush(); //------------------------------------if------------------------------------------------------------------------------------------------------------------------------------------------- //sieve static void primeSieve(int a[]){ //mark all odd number as prime for (int i=3;i<a.length;i+=2){ a[i]=1; } for (long i=3;i<a.length;i+=2){ //if the number is marked then it is prime if (a[(int) i]==1){ //mark all muliple of the number as not prime for (long j=i*i;j<a.length;j+=i){ a[(int)j]=0; } } } a[2]=1; a[1]=a[0]=0; } //prime static boolean isPrime(long n){ if (n==2)return true; if (n==1)return false; for (long i=2;i*i<=n;i++){ if (n%i==0)return false; } return true; } static void sort(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); Collections.sort(l); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static String sortString(String s) { char temp[] = s.toCharArray(); Arrays.sort(temp); return new String(temp); } static class Pair implements Comparable<Pair> { int a; int b; public Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair o) { return a == o.a ? b - o.b : a - o.a; } // to sort first part // public int compareTo(Pair other) { // if (this.a == other.a) return other.b >=this.b ? -1 : 1; // else if (this.a < other.a) return 1; // else return -1; // } // public int compareTo(Pair other) { // if (this.b == other.b){ // if (this.a> other.a)return 1; // else return -1; // } // if (this.b > other.b) return 1; // else return -1; // } //sort on the basis of first part only // public int compareTo(Pair other) { // if (this.a == other.a) return 0; // if (this.a > other.a) return 1; // else return -1; // } } static int[] frequency(String s){ int fre[]= new int[26]; for (int i=0;i<s.length();i++){ fre[s.charAt(i)-'a']++; } return fre; } static long LOWERBOUND(long a[],long k){ int s=0,e=a.length-1; long ans=-1; while (s<=e){ int mid=(s+e)/2; if (a[mid]<=k){ ans=mid; s=mid+1; } else { e=mid-1; } } return ans; } static long mod =(long)(1e9+7); static long mod(long x) { return ((x % mod + mod) % mod); } static long div(long a,long b){ return ((a/b)%mod+mod)%mod; } static long add(long x, long y) { return mod(mod(x) + mod(y)); } static long mul(long x, long y) { return mod(mod(x) * mod(y)); } //Fast Power(logN) static int fastPower(long a,long n){ int ans=1; while (n>0){ long lastBit=n&1; if (lastBit==1){ ans=(int)mul(ans,a); } a=(int)mul(a,a); n>>=1; } return ans; } static int[] find(int n, int start, int diff) { int a[] = new int[n]; a[0] = start; for (int i = 1; i < n; i++) a[i] = a[i - 1] + diff; return a; } static void swap(int a, int b) { int c = a; a = b; b = c; } static void printArray(int a[]) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } } static boolean sorted(int a[]) { int n = a.length; boolean flag = true; for (int i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) flag = false; } if (flag) return true; else return false; } public static int findlog(long n) { if (n == 0) return 0; if (n == 1) return 0; if (n == 2) return 1; double num = Math.log(n); double den = Math.log(2); if (den == 0) return 0; return (int) (num / den); } public static long gcd(long a, long b) { if (b % a == 0) return a; return gcd(b % a, a); } public static int gcdInt(int a, int b) { if (b % a == 0) return a; return gcdInt(b % a, a); } static void sortReverse(int[] a) { ArrayList<Integer> l = new ArrayList<>(); for (int i : a) l.add(i); // Collections.sort.(l); Collections.sort(l, Collections.reverseOrder()); for (int i = 0; i < a.length; i++) a[i] = l.get(i); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } double nextDouble(){ return Double.parseDouble(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long[] readArrayLong(long n) { long[] a = new long[(int) n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } double[] readArrayDouble(long n) { double[] a = new double[(int) n]; for (int i = 0; i < n; i++) a[i] = nextDouble(); return a; } long nextLong() { return Long.parseLong(next()); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
336281c3820c45b8e5fec8b57d9fea3e
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*;import java.util.*;import java.math.*;import static java.lang.Math.*;import static java. util.Map.*;import static java.util.Arrays.*;import static java.util.Collections.*; import static java.lang.System.*; public class Main { public void tq()throws Exception { st=new StringTokenizer(bq.readLine()); int tq=i(); sb=new StringBuilder(2000000); o: while(tq-->0) { int n=i(); int k=i(); long ar[]=arl(n); long c=0l; if(n==1) { sl(k+1); continue o; } for(int x=0;x<n;x++) { long a=1l; while(ar[x]-->0)a*=10; ar[x]=a; } for(int x=1;x<n;x++) { int v=(int)(ar[x]/ar[x-1]); v--; if(k<v) { c+=ar[x-1]*(k+1); sl(c); continue o; } else { c+=ar[x-1]*v; k-=v; } } c+=ar[n-1]*(k+1); sl(c); } p(sb); } void f(){out.flush();} int di[][]={{-1,0},{1,0},{0,-1},{0,1}}; int de[][]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}}; long mod=1000000007l;int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;long maxl=Long.MAX_VALUE, minl=Long. MIN_VALUE;BufferedReader bq=new BufferedReader(new InputStreamReader(in));StringTokenizer st; StringBuilder sb;public static void main(String[] a)throws Exception{new Main().tq();}int[] so(int ar[]) {Integer r[]=new Integer[ar.length];for(int x=0;x<ar.length;x++)r[x]=ar[x];sort(r);for(int x=0;x<ar.length; x++)ar[x]=r[x];return ar;}long[] so(long ar[]){Long r[]=new Long[ar.length];for(int x=0;x<ar.length;x++) r[x]=ar[x];sort(r);for(int x=0;x<ar.length;x++)ar[x]=r[x];return ar;} char[] so(char ar[]) {Character r[]=new Character[ar.length];for(int x=0;x<ar.length;x++)r[x]=ar[x];sort(r);for(int x=0;x<ar.length;x++) ar[x]=r[x];return ar;}void s(String s){sb.append(s);}void s(int s){sb.append(s);}void s(long s){sb. append(s);}void s(char s){sb.append(s);}void s(double s){sb.append(s);}void ss(){sb.append(' ');}void sl (String s){sb.append(s);sb.append("\n");}void sl(int s){sb.append(s);sb.append("\n");}void sl(long s){sb .append(s);sb.append("\n");}void sl(char s) {sb.append(s);sb.append("\n");}void sl(double s){sb.append(s) ;sb.append("\n");}void sl(){sb.append("\n");}int l(int v){return 31-Integer.numberOfLeadingZeros(v);} long l(long v){return 63-Long.numberOfLeadingZeros(v);}int sq(int a){return (int)sqrt(a);}long sq(long a) {return (long)sqrt(a);}long gcd(long a,long b){while(b>0l){long c=a%b;a=b;b=c;}return a;}int gcd(int a,int b) {while(b>0){int c=a%b;a=b;b=c;}return a;}boolean pa(String s,int i,int j){while(i<j)if(s.charAt(i++)!= s.charAt(j--))return false;return true;}boolean[] si(int n) {boolean bo[]=new boolean[n+1];bo[0]=true;bo[1] =true;for(int x=4;x<=n;x+=2)bo[x]=true;for(int x=3;x*x<=n;x+=2){if(!bo[x]){int vv=(x<<1);for(int y=x*x;y<=n; y+=vv)bo[y]=true;}}return bo;}long mul(long a,long b,long m) {long r=1l;a%=m;while(b>0){if((b&1)==1) r=(r*a)%m;b>>=1;a=(a*a)%m;}return r;}int i()throws IOException{if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return Integer.parseInt(st.nextToken());}long l()throws IOException {if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return Long.parseLong(st.nextToken());}String s()throws IOException {if (!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return st.nextToken();} double d()throws IOException{if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());return Double. parseDouble(st.nextToken());}void p(Object p){out.print(p);}void p(String p){out.print(p);}void p(int p) {out.print(p);}void p(double p){out.print(p);}void p(long p){out.print(p);}void p(char p){out.print(p);}void p(boolean p){out.print(p);}void pl(Object p){out.println(p);}void pl(String p){out.println(p);}void pl(int p) {out.println(p);}void pl(char p){out.println(p);}void pl(double p){out.println(p);}void pl(long p){out. println(p);}void pl(boolean p) {out.println(p);}void pl(){out.println();}void s(int a[]){for(int e:a) {sb.append(e);sb.append(' ');}sb.append("\n");} void s(long a[]) {for(long e:a){sb.append(e);sb.append(' ') ;}sb.append("\n");}void s(int ar[][]){for(int a[]:ar){for(int e:a){sb.append(e);sb.append(' ');}sb.append ("\n");}} void s(char a[]) {for(char e:a){sb.append(e);sb.append(' ');}sb.append("\n");}void s(char ar[][]) {for(char a[]:ar){for(char e:a){sb.append(e);sb.append(' ');}sb.append("\n");}}int[] ari(int n)throws IOException {int ar[]=new int[n];if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());for(int x=0; x<n;x++)ar[x]=Integer.parseInt(st.nextToken());return ar;}int[][] ari(int n,int m)throws IOException {int ar[][]=new int[n][m];for(int x=0;x<n;x++){if (!st.hasMoreTokens())st=new StringTokenizer (bq.readLine());for(int y=0;y<m;y++)ar[x][y]=Integer.parseInt(st.nextToken());}return ar;}long[] arl (int n)throws IOException {long ar[]=new long[n];if(!st.hasMoreTokens()) st=new StringTokenizer(bq.readLine()) ;for(int x=0;x<n;x++)ar[x]=Long.parseLong(st.nextToken());return ar;}long[][] arl(int n,int m)throws IOException {long ar[][]=new long[n][m];for(int x=0;x<n;x++) {if(!st.hasMoreTokens()) st=new StringTokenizer(bq.readLine());for(int y=0;y<m;y++)ar[x][y]=Long.parseLong(st.nextToken());}return ar;} String[] ars(int n)throws IOException {String ar[] =new String[n];if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());for(int x=0;x<n;x++)ar[x]=st.nextToken();return ar;}double[] ard (int n)throws IOException {double ar[] =new double[n];if(!st.hasMoreTokens())st=new StringTokenizer (bq.readLine());for(int x=0;x<n;x++)ar[x]=Double.parseDouble(st.nextToken());return ar;}double[][] ard (int n,int m)throws IOException{double ar[][]=new double[n][m];for(int x=0;x<n;x++) {if(!st.hasMoreTokens()) st=new StringTokenizer(bq.readLine());for(int y=0;y<m;y++) ar[x][y]=Double.parseDouble(st.nextToken());} return ar;}char[] arc(int n)throws IOException{char ar[]=new char[n];if(!st.hasMoreTokens())st=new StringTokenizer(bq.readLine());for(int x=0;x<n;x++)ar[x]=st.nextToken().charAt(0);return ar;}char[][] arc(int n,int m)throws IOException {char ar[][]=new char[n][m];for(int x=0;x<n;x++){String s=bq.readLine(); for(int y=0;y<m;y++)ar[x][y]=s.charAt(y);}return ar;}void p(int ar[]) {StringBuilder sb=new StringBuilder (2*ar.length);for(int a:ar){sb.append(a);sb.append(' ');}out.println(sb);}void p(int ar[][]) {StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length);for(int a[]:ar){for(int aa:a){sb.append(aa); sb.append(' ');}sb.append("\n");}p(sb);}void p(long ar[]){StringBuilder sb=new StringBuilder (2*ar.length);for(long a:ar){ sb.append(a);sb.append(' ');}out.println(sb);} void p(long ar[][]) {StringBuilder sb=new StringBuilder(2*ar.length*ar[0].length);for(long a[]:ar){for(long aa:a){sb.append(aa); sb.append(' ');}sb.append("\n");}p(sb);}void p(String ar[]){int c=0;for(String s:ar)c+=s.length()+1; StringBuilder sb=new StringBuilder(c);for(String a:ar){sb.append(a);sb.append(' ');}out.println(sb);} void p(double ar[]) {StringBuilder sb=new StringBuilder(2*ar.length);for(double a:ar){sb.append(a); sb.append(' ');}out.println(sb);}void p (double ar[][]){StringBuilder sb=new StringBuilder(2* ar.length*ar[0].length);for(double a[]:ar){for(double aa:a){sb.append(aa);sb.append(' ');}sb.append("\n") ;}p(sb);}void p(char ar[]) {StringBuilder sb=new StringBuilder(2*ar.length);for(char aa:ar){sb.append(aa); sb.append(' ');}out.println(sb);}void p(char ar[][]){StringBuilder sb=new StringBuilder(2*ar.length*ar[0] .length);for(char a[]:ar){for(char aa:a){sb.append(aa);sb.append(' ');}sb.append("\n");}p(sb);}void pl (int... ar){for(int e:ar)p(e+" ");pl();}void pl(long... ar){for(long e:ar)p(e+" ");pl();} }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
24dedabd8f73b086b019de1aa43477f7
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
/** * check out my youtube channel sh0rkyboy * https://tinyurl.com/zdxe2y4z * I do screencasts, solutions, and other fun stuff in the future */ import java.util.*; import java.io.*; import static java.lang.Math.min; import static java.lang.Math.abs; import static java.lang.Math.max; public class EdA { static long[] mods = {1000000007, 998244353, 1000000009}; static long mod = mods[0]; public static MyScanner sc; public static PrintWriter out; public static void main(String[] largewang) throws Exception{ // TODO Auto-generated method stub sc = new MyScanner(); out = new PrintWriter(System.out); int t = sc.nextInt(); while (t-->0) { int n = sc.nextInt(); int k = sc.nextInt(); int[] arr = readArrayInt(n); ArrayList<Integer> digits = new ArrayList<>(); boolean found = false; for(int j = 0;j<n-1;j++){ long prod = 1; for(int s = 0;s<arr[j+1] - arr[j];s++) prod *= 10L; if (k >= prod - 1) { for(int s = 0;s<arr[j+1] - arr[j];s++) digits.add(9); k-= (prod - 1); } else { found = true; k += 1; while( k != 0){ digits.add(k%10); k/=10L; } break; } } if (!found) { k += 1; while( k != 0){ digits.add(k%10); k/=10L; } } Collections.reverse(digits); boolean flag = true; for(int j : digits) { if (j != 0) flag = false; if (!flag) out.print(j); } out.println(); } out.close(); } public static void sort(int[] array){ ArrayList<Integer> copy = new ArrayList<>(); for (int i : array) copy.add(i); Collections.sort(copy); for(int i = 0;i<array.length;i++) array[i] = copy.get(i); } static String[] readArrayString(int n){ String[] array = new String[n]; for(int j =0 ;j<n;j++) array[j] = sc.next(); return array; } static int[] readArrayInt(int n){ int[] array = new int[n]; for(int j = 0;j<n;j++) array[j] = sc.nextInt(); return array; } static int[] readArrayInt1(int n){ int[] array = new int[n+1]; for(int j = 1;j<=n;j++){ array[j] = sc.nextInt(); } return array; } static long[] readArrayLong(int n){ long[] array = new long[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextLong(); return array; } static double[] readArrayDouble(int n){ double[] array = new double[n]; for(int j =0 ;j<n;j++) array[j] = sc.nextDouble(); return array; } static int minIndex(int[] array){ int minValue = Integer.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(long[] array){ long minValue = Long.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static int minIndex(double[] array){ double minValue = Double.MAX_VALUE; int minIndex = -1; for(int j = 0;j<array.length;j++){ if (array[j] < minValue){ minValue = array[j]; minIndex = j; } } return minIndex; } static long power(long x, long y){ if (y == 0) return 1; if (y%2 == 1) return (x*power(x, y-1))%mod; return power((x*x)%mod, y/2)%mod; } static void verdict(boolean a){ out.println(a ? "YES" : "NO"); } 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; } } } //StringJoiner sj = new StringJoiner(" "); //sj.add(strings) //sj.toString() gives string of those stuff w spaces or whatever that sequence is
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
38582bd1746818889beba906b812b499
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
/* Author:-crazy_coder- */ import java.io.*; import java.util.*; public class cp{ static long mod=1000000007; static int ans=0; static int[] par=new int[100005]; static int[] rank=new int[100005]; static BufferedReader br=new BufferedReader (new InputStreamReader(System.in)); static PrintWriter pw=new PrintWriter(System.out); public static void main(String[] args)throws Exception{ int T=Integer.parseInt(br.readLine()); // int t=1; // comp(); // boolean isPrime[]=sieveOfEratosthenes(1000006); while(T-->0){ solve(); } pw.flush(); } public static void solve()throws Exception{ String[] str=br.readLine().split(" "); int n=Integer.parseInt(str[0]); int k=Integer.parseInt(str[1]); k++; int[] arr=new int[n]; str=br.readLine().split(" "); for(int i=0;i<n;i++)arr[i]=Integer.parseInt(str[i]); long num=0l; for(int i=0;i<n-1;i++) { int max=(int)Math.pow(10,arr[i+1]-arr[i])-1; int taken=Math.min(max,k); num+=(long)Math.pow(10,arr[i])*taken; k-=taken; } if(k>0) { num+=(long)Math.pow(10,arr[n-1])*k; } pw.println(num); } public static boolean[] sieveOfEratosthenes(int n){ boolean isPrime[]=new boolean[n+1]; Arrays.fill(isPrime,true); isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<n;i++){ for(int j=i*i;j<=n;j+=i){ isPrime[j]=false; } } return isPrime; } public static long NCR(int n,int r){ if(r==0||r==n)return 1; return (fac[n]*invfact[r]%mod)*invfact[n-r]%mod; } static long[] fac=new long[100]; static long[] invfact=new long[100]; public static void comp(){ fac[0]=1; invfact[0]=1; for(int i=1;i<100;i++){ fac[i]=(fac[i-1]*i)%mod; invfact[i]=modInverse(fac[i]); } } public static long modInverse(long n){ return power(n,mod-2); } public static long power(long x,long y){ long res=1; x=x%mod; while(y>0){ if((y&1)==1)res=(res*x)%mod; y=y>>1; x=(x*x)%mod; } return res; } //*************************************DSU************************************ */ public static void initialize(int n){ for(int i=1;i<=n;i++){ par[i]=i; rank[i]=1; } } public static void union(int x,int y){ int lx=find(x); int ly=find(y); if(lx!=ly){ if(rank[lx]>rank[ly]){ par[ly]=lx; }else if(rank[lx]<rank[ly]){ par[lx]=ly; }else{ par[lx]=ly; rank[ly]++; } } } public static int find(int x){ if(par[x]==x){ return x; } int temp=find(par[x]); par[x]=temp; return temp; } //************************************DSU END********************************** */ public static boolean isPresent(ArrayList<Long> zero,long val){ for(long x:zero){ if(x==val)return true; } return false; } public static class Pair implements Comparable<Pair>{ int val; int k; Pair(int val,int k){ this.val=val; this.k=k; } public int compareTo(Pair o){ return this.val-o.val; } } public static int countDigit(int x){ return (int)Math.log10(x)+1; } //****************************function to find all factor************************************************* public static ArrayList<Long> findAllFactors(long num){ ArrayList<Long> factors = new ArrayList<Long>(); for(long i = 1; i <= num/i; ++i) { if(num % i == 0) { //if i is a factor, num/i is also a factor factors.add(i); factors.add(num/i); } } //sort the factors Collections.sort(factors); return factors; } //*************************** function to find GCD of two number******************************************* public static long gcd(long a,long b){ if(b==0)return a; return gcd(b,a%b); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
e10322ac35fef1073b023b7be92311bd
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; public class class1 { public static void main(String[] args) { Scanner input=new Scanner(System.in); int t=input.nextInt(); while(t-->0) { int n=input.nextInt(); long a[]=new long[n]; long k=input.nextLong()+1; long ans=0,temp1=0,temp2=0,temp3=0; for(int i=0;i<n;i++) { a[i]=input.nextLong(); } int i=0; while(k>0) { if(i==n-1) { ans+=(long)Math.pow(10,a[i])*k; break; } else { temp1=(long)Math.pow(10,a[i]); temp2=(long)Math.pow(10,a[i+1]); temp3=(long)Math.min(k,temp2/temp1-1)*temp1; ans+=temp3; k-=(long)Math.min(k,temp2/temp1-1); i++; } } System.out.println(ans); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
16befc5e09d2411ebb2a205c8d7bded3
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; public class class462 { public static void main(String arg[]) { Scanner sc=new Scanner(System.in); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long k=sc.nextLong(); long a[]=new long[n]; int i; k+=1; for(i=0;i<n;i++) a[i]=sc.nextInt(); i=0; long num=0; while(k>0) { if(i==n-1) { num+=k*(long)Math.pow(10,a[i]); break; } else { long num1=(a[i]==0) ? 1: (long)Math.pow(10,a[i]); long num2=(long)Math.pow(10,a[i+1]); num+=(long)Math.min(k, num2/num1-1)*num1; k-=Math.min(k, num2/num1-1); } i++; } System.out.println(num); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
9f641d8279370e01a2762a3c9dc1b7c0
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.Scanner; import java.util.stream.IntStream; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); for (int tc = 0; tc < t; ++tc) { int n = sc.nextInt(); int k = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < a.length; ++i) { a[i] = sc.nextInt(); } System.out.println(solve(a, k)); } sc.close(); } static long solve(int[] a, int k) { long result = 0; int rest = k + 1; for (int i = 0; i < a.length; ++i) { int noteNum = Math.min((i == a.length - 1) ? Integer.MAX_VALUE : pow10(a[i + 1] - a[i]) - 1, rest); result += (long) pow10(a[i]) * noteNum; rest -= noteNum; } return result; } static int pow10(int exponent) { return IntStream.range(0, exponent).reduce(1, (x, y) -> x * 10); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
bac0523d1ecd9a6090905257e03b6384
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class Main { static int i, j, k, n, m, t, y, x, sum = 0; static long mod = 100000000000L; static FastScanner fs = new FastScanner(); static PrintWriter out = new PrintWriter(System.out); static String str; static long ans; public static void main(String[] args) { t =fs.nextInt(); while(t-- >0){ n = fs.nextInt(); k = fs.nextInt()+1; int[] arr = fs.readArray(n); ruffleSort(arr); ans = 0; int count = 0; j=0; for(i=1;i<n;i++){ int diff = arr[i]-arr[i-1]; long maxC = exp(10,diff)-1; long contribution = Math.min(k,maxC); ans+=(contribution*exp(10,arr[i-1])); k-=contribution; } ans+=(k* exp(10,arr[n-1])); out.println(ans); } out.close(); } /** * Returns the gretaest index of closest element less than x in arr * returns -1 if all elemets are greater * */ public static int lowerBound(int x, List<Integer> arr, int l, int r){ if(arr.get(l)>=x) return -1; if(arr.get(r)<x) return r; int mid = (l+r)/2; if(arr.get(mid)<x && arr.get(mid+1)>x) return mid; if(arr.get(mid)>=x) return lowerBound(x,arr,l,mid-1); return lowerBound(x,arr,mid+1,r); } /** * Returns the lowest index of closest element greater than or equal to x in arr * returns -1 if all elements are lesser * */ public static int upperBound(int x, List<Integer> arr, int l, int r){ if(arr.get(r) <=x) return -1; if(arr.get(l)>x) return l; int mid = (l+r)/2; if(arr.get(mid)>x && arr.get(mid-1)<=x) return mid; if(arr.get(mid)<=x) return upperBound(x,arr,mid+1,r); return upperBound(x,arr,l,mid-1); } /** * Returns the index of element if present else -1. * */ public static int binSearch(int x, List<Integer> arr){ int y = Collections.binarySearch(arr, x); if(y<0) return -1; return y; } /** * Gets prime factorisation of a number in list of pairs. * x is the factor and y is the occurrence. * */ public static List<Pair> primeFactorization(int num){ List<Pair> ans = new ArrayList<>(); for(int i=2;i*i<=num;i++){ if(num % i ==0){ int count = 0; while(num%i==0){ count++; num=num/i; } ans.add(new Pair(i,count)); } } if(num!=1) ans.add(new Pair(num, 1)); return ans; } /*static long nck(int n , int k){ long a = fact[n]; long b = modInv(fact[k]); b*= modInv(fact[n-k]); b%=mod; return (a*b)%mod; } static void populateFact(){ fact[0]=1; fact[1] = 1; for(i=2;i<300005;i++){ fact[i]=i*fact[i-1]; fact[i]%=mod; } } */ static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long exp(long base, long pow) { if (pow == 0) return 1; long half = exp(base, pow / 2); if (pow % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long mul(long a, long b) { return ((a % mod) * (b % mod)) % mod; } static long add(long a, long b) { return ((a % mod) + (b % mod)) % mod; } static long modInv(long x) { return exp(x, mod - 2); } static void ruffleSort(int[] a) { //ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n), temp = a[i]; a[i] = a[oi]; a[oi] = temp; } //then sort Arrays.sort(a); } static void ruffleSort(long[] a) { //ruffle int n = a.length; Random r = new Random(); for (int i = 0; i < a.length; i++) { int oi = r.nextInt(n); long temp = a[i]; a[i] = a[oi]; a[oi] = temp; } Arrays.sort(a); } static class FastScanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } long nextLong() { return Long.parseLong(next()); } } static class Pair implements Comparable<Pair> { public int x, y; Pair(int x, int y) { this.x = x; this.y = y; } @Override public int compareTo(Pair o) { if (x == o.x) return Integer.compare(y, o.y); return Integer.compare(x, o.x); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
328b66a8daaa011a2b4e8c16497f932e
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; //import javafx.util.*; public class Main { static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static int INF = Integer.MAX_VALUE; static int NINF = Integer.MIN_VALUE; public static StringBuilder str = new StringBuilder(); public static boolean flag = true; public static ArrayList<Integer> arr = new ArrayList<>(); public static void main (String[] args) throws java.lang.Exception { //check if you have to take product or the constraints are big int t = i(); while(t-- > 0){ int n = i(); long k = in.nextLong(); long[] arr = inputLong(n); for(int i = 0;i < n;i++){ arr[i] = (long)Math.pow(10l,arr[i]); } long ans = 0l; k++; for(int i = 0;i <= n - 2;i++){ long first = arr[i + 1]; long second = arr[i]; long total = (first - second)/second; if(k >= total){ ans += total * second; k -= total; }else{ ans += second * k; k = 0l; } } if(k != 0){ ans += k * arr[n-1]; } out.println(ans); } out.close(); } public static void sort(int[] arr){ ArrayList<Integer> ls = new ArrayList<>(); for(int x : arr){ ls.add(x); } Collections.sort(ls); for(int i = 0;i < arr.length;i++){ arr[i] = ls.get(i); } } public static void reverse(int[] arr){ int n = arr.length; for(int i = 0;i < n/2;i++){ int temp = arr[i]; arr[i] = arr[n-1-i]; arr[n-1-i] = temp; } } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static int[] input(int N){ int A[]=new int[N]; for(int i=0; i<N; i++) { A[i]=in.nextInt(); } return A; } static long[] inputLong(int N) { long A[]=new long[N]; for(int i=0; i<A.length; i++)A[i]=in.nextLong(); return A; } } class pair implements Comparable<pair> { int x; int y; public pair(int x, int y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Integer(x).hashCode() * 31 + new Integer(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Integer.compare(this.y, other.y); } return Integer.compare(this.x, other.x); } } 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; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
f65867ad3359c87d7a3e98c9995c30a7
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import static java.lang.Math.*; import java.io.*; import java.math.*; import java.util.*; public class cf2 { static FastReader x = new FastReader(); static OutputStream outputStream = System.out; static PrintWriter out = new PrintWriter(outputStream); /*---------------------------------------CODE STARTS HERE-------------------------*/ public static void main(String[] args) { int t = x.nextInt(); StringBuilder str = new StringBuilder(); while (t > 0) { int n = x.nextInt(); long k= x.nextInt(); k++; long a[] = new long[n]; for(int i=0;i<n;i++) { a[i] = pow(10,x.nextLong()); } long max = 0; long ans =0; for(int i=0;k>0&&i<n-1;i++) { max = a[i+1]/a[i]; max--; // System.out.println(max+" "+k+" "+a[i]); if(max<=k) { k-=max; ans+=(max*a[i]); }else { ans+=(k*a[i]); k=0; // System.out.println(k*a[i]); } } if(k>0) { ans+=(k*a[n-1]); } System.out.println(ans); str.append("\n"); t--; } out.println(str); out.flush(); } /*--------------------------------------------FAST I/O--------------------------------*/ 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; } char nextchar() { char ch = ' '; try { ch = (char) br.read(); } catch (IOException e) { e.printStackTrace(); } return ch; } } /*--------------------------------------------BOILER PLATE---------------------------*/ static int[] readarr(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = x.nextInt(); } return arr; } static int[] sortint(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static long[] sortlong(long a[]) { ArrayList<Long> al = new ArrayList<>(); for (long i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < al.size(); i++) { a[i] = al.get(i); } return a; } static long pow(long x, long y) { long result = 1; while (y > 0) { if (y % 2 == 0) { x = x * x; y = y / 2; } else { result = result * x; y = y - 1; } } return result; } static int[] revsort(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al, Comparator.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static int[] gcd(int a, int b, int ar[]) { if (b == 0) { ar[0] = a; ar[1] = 1; ar[2] = 0; return ar; } ar = gcd(b, a % b, ar); int t = ar[1]; ar[1] = ar[2]; ar[2] = t - (a / b) * ar[2]; return ar; } static boolean[] esieve(int n) { boolean p[] = new boolean[n + 1]; Arrays.fill(p, true); for (int i = 2; i * i <= n; i++) { if (p[i] == true) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } static ArrayList<Integer> primes(int n) { boolean p[] = new boolean[n + 1]; ArrayList<Integer> al = new ArrayList<>(); Arrays.fill(p, true); int i = 0; for (i = 2; i * i <= n; i++) { if (p[i] == true) { al.add(i); for (int j = i * i; j <= n; j += i) { p[j] = false; } } } for (i = i; i <= n; i++) { if (p[i] == true) { al.add(i); } } return al; } static int etf(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res /= i; res *= (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res /= n; res *= (n - 1); } return res; } static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
b1993afb8aa02e8fa804fc289885bd36
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
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_116 { public static long MOD = 998244353; static int[] 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(); int T = in.nextInt(); for (int z = 0; z < T; z++) { int n = in.nextInt(); long k = in.nextInt() + 1; int last = in.nextInt(); Stack<Long> s = new Stack<>(); for (int i = 1; i < n; i++) { int cur = in.nextInt(); if (k == 0) { continue; } int diff = cur - last; long max = pow(10, diff) - 1; if (max < k) { k -= max; s.add(max); } else { s.add(k); k = 0; } last = cur; } if (k > 0) { s.add(k); } while (!s.isEmpty()) { out.print(s.pop()); } out.println(); } out.close(); } static int find(int index, int[] u) { if (u[index] != index) { return u[index] = find(u[index], u); } return index; } static int abs(int a) { return a < 0 ? -a : a; } 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 { int x; int y; public Point(int start, int end) { this.x = start; this.y = end; } public String toString() { return x + " " + y; } } 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, int b) { if (b == 0) { return 1; } if (b == 1) { return a; } long val = pow(a, b / 2); if (b % 2 == 0) { return (val * val); } else { return (val * ((val * a))); } } 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(); } } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
c3581f173131b1954afcdb55867ab495
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class _1606c { FastScanner scn; PrintWriter w; PrintStream fs; int MOD = 1000000007; int MAX = 200005; long mul(long x, long y) {long res = x * y; return (res >= MOD ? res % MOD : res);} long power(long x, long y) {if (y < 0) return 1; long res = 1; x %= MOD; while (y!=0) {if ((y & 1)==1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;} void ruffleSort(int[] a) {int n=a.length;Random r=new Random();for (int i=0; i<a.length; i++) {int oi=r.nextInt(n), temp=a[i];a[i]=a[oi];a[oi]=temp;}Arrays.sort(a);} void reverseSort(int[] arr){List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++){list.add(arr[i]);}Collections.sort(list, Collections.reverseOrder());for (int i = 0; i < arr.length; i++){arr[i] = list.get(i);}} boolean LOCAL; void debug(Object... o){if(LOCAL)System.err.println(Arrays.deepToString(o));} //SPEED IS NOT THE CRITERIA, CODE SHOULD BE A NO BRAINER, CMP KILLS, MOCIM void solve(){ int t=scn.nextInt(); while(t-->0) { int n=scn.nextInt(),k=scn.nextInt(); long[] ar=scn.nextLongArray(n); for(int i=0;i<n;i++){ ar[i] = (long)Math.pow(10,(int)ar[i]); } if(k<=8){ w.println(k+1); continue; } long ans = 1; HashMap<Long,Long> hm = new HashMap<>(); hm.put((long)0,(long)0); for(int i=0;i<n;i++){ if(i==n-1){ long makemine = ar[i]-1; long costmine = hm.get(makemine); long leftoverk = k-costmine; ans = (leftoverk*ar[i])+makemine; ans+=(ar[i]); break; }else{ long ninejustless = ar[i+1]-1; long reqd = ninejustless/ar[i]; long rem = hm.get(ninejustless%ar[i]); long total = reqd+rem; if(total<=k){ hm.put(ninejustless,total); continue; }else{ long makemine = ar[i]-1; long costmine = hm.get(makemine); long leftoverk = k-costmine; ans = (leftoverk*ar[i])+makemine; ans+=ar[i]; break; } } } w.println(ans); } } void run() { try { long ct = System.currentTimeMillis(); scn = new FastScanner(new File("input.txt")); w = new PrintWriter(new File("output.txt")); fs=new PrintStream("error.txt"); System.setErr(fs); LOCAL=true; solve(); w.close(); System.err.println(System.currentTimeMillis() - ct); } catch (FileNotFoundException e) { e.printStackTrace(); } } void runIO() { scn = new FastScanner(System.in); w = new PrintWriter(System.out); LOCAL=false; solve(); w.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } long[] nextLongArray(int n) { long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } int lowerBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid-1 >= 0 && arr[mid-1] == arr[mid]){ei = mid-1;}else{return mid;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; } int upperBound(int[] arr, int x){int n = arr.length, si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(arr[mid] == x){if(mid+1 < n && arr[mid+1] == arr[mid]){si = mid+1;}else{return mid + 1;}}else if(arr[mid] > x){ei = mid - 1; }else{si = mid+1;}}return si; } int upperBound(ArrayList<Integer> list, int x){int n = list.size(), si = 0, ei = n - 1;while(si <= ei){int mid = si + (ei - si)/2;if(list.get(mid) == x){if(mid+1 < n && list.get(mid+1) == list.get(mid)){si = mid+1;}else{return mid + 1;}}else if(list.get(mid) > x){ei = mid - 1; }else{si = mid+1;}}return si; } void swap(int[] arr, int i, int j){int temp = arr[i];arr[i] = arr[j];arr[j] = temp;} long nextPowerOf2(long v){if (v == 0) return 1;v--;v |= v >> 1;v |= v >> 2;v |= v >> 4;v |= v >> 8;v |= v >> 16;v |= v >> 32;v++;return v;} int gcd(int a, int b) {if(a == 0){return b;}return gcd(b%a, a);} // TC- O(logmax(a,b)) boolean nextPermutation(int[] arr) {if(arr == null || arr.length <= 1){return false;}int last = arr.length-2;while(last >= 0){if(arr[last] < arr[last+1]){break;}last--;}if (last < 0){return false;}if(last >= 0){int nextGreater = arr.length-1;for(int i=arr.length-1; i>last; i--){if(arr[i] > arr[last]){nextGreater = i;break;}}swap(arr, last, nextGreater);}int i = last + 1, j = arr.length - 1;while(i < j){swap(arr, i++, j--);}return true;} public static void main(String[] args) { new _1606c().runIO(); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
8e64bde73cdfd6a9e73ba332cbe5ca2d
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedList; import java.util.Map; import java.util.PriorityQueue; import java.util.Queue; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeMap; import java.util.TreeSet; public class Practice1 { static long[] sort(long[] arr) { int n=arr.length; ArrayList<Long> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static long nCr(int n, int r) { // int x=1000000007; long dp[][]=new long[2][r+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=i&&j<=r;j++){ if(i==0||j==0||i==j){ dp[i%2][j]=1; }else { // dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1])%x; dp[i%2][j]=(dp[(i-1)%2][j]+dp[(i-1)%2][j-1]); } } } return dp[n%2][r]; } public static class UnionFind { private final int[] p; public UnionFind(int n) { p = new int[n]; for (int i = 0; i < n; i++) { p[i] = i; } } public int find(int x) { return x == p[x] ? x : (p[x] = find(p[x])); } public void union(int x, int y) { x = find(x); y = find(y); if (x != y) { p[x] = y; } } } public static boolean ispalin(String str) { int n=str.length(); for(int i=0;i<n/2;i++) { if(str.charAt(i)!=str.charAt(n-i-1)) { return false; } } return true; } static class Pair{ int val; int pos; Pair(int val,int pos){ this.val=val; this.pos=pos; } } static long power(long N,long R) { long x=1000000007; if(R==0) return 1; if(R==1) return N; long temp= power(N,R/2)%x; // (a*b)%p = (a%p*b%p)*p temp=(temp*temp)%x; if(R%2==0){ return temp%x; }else{ return (N*temp)%x; } } public static String binary(int n) { StringBuffer ans=new StringBuffer(); int a=4; while(a-->0) { int temp=(n&1); if(temp!=0) { ans.append('1'); }else { ans.append('0'); } n =n>>1; } ans=ans.reverse(); return ans.toString(); } public static int find(String[][] arr,boolean[][] vis,int dir,int i,int j) { if(i<0||i>=arr.length||j<0||j>=arr[0].length) return 0; if(vis[i][j]==true) return 0; if(dir==1&&arr[i][j].charAt(0)=='1') return 0; if(dir==2&&arr[i][j].charAt(1)=='1') return 0; if(dir==3&&arr[i][j].charAt(2)=='1') return 0; if(dir==4&&arr[i][j].charAt(3)=='1') return 0; vis[i][j]=true; int a=find(arr,vis,1,i+1,j); int b=find(arr,vis,2,i-1,j); int c=find(arr,vis,3,i,j+1); int d=find(arr,vis,4,i,j-1); return 1+a+b+c+d; } static ArrayList<Integer> allDivisors(int n) { ArrayList<Integer> al=new ArrayList<>(); int i=2; while(i*i<=n) { if(n%i==0) al.add(i); if(n%i==0&&i*i!=n) al.add(n/i); i++; } return al; } static int[] sort(int[] arr) { int n=arr.length; ArrayList<Integer> al=new ArrayList<>(); for(int i=0;i<n;i++) { al.add(arr[i]); } Collections.sort(al); for(int i=0;i<n;i++) { arr[i]=al.get(i); } return arr; } public static void main (String[] args) { PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); // out.print(); //out.println(); FastReader sc=new FastReader(); int t=sc.nextInt(); while(t-->0) { int n=sc.nextInt(); long k=sc.nextLong(); k++; long[] arr=new long[n]; for(int i=0;i<n;i++) { long a=sc.nextLong(); arr[i]=(long)Math.pow(10, a); } sort(arr); long sum=0; for(int i=0;i<n-1;i++) { long a=arr[i+1]/arr[i]-1; long x=Math.min(k, a); sum +=arr[i]*x; k -=x; } sum +=arr[n-1]*k; out.println(sum); } out.close(); } 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; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
60ac3fd2752680a09344f2c9de3c63d6
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class C { public static void main(String[] args) { FastReader in = new FastReader(); PrintWriter out = new PrintWriter(System.out); int T = in.nextInt(); for(int ttt = 1; ttt <= T; ttt++) { int n = in.nextInt(); long k = in.nextLong(); long[] a = new long[n]; for(int i = 0; i < n; i++) { int exp = in.nextInt(); a[i] = (long)Math.pow(10, exp); } int cur = 0; long ans = 0, left = k+1; while(left>0 && cur<n-1) { long max = (a[cur+1]/a[cur]) - 1; long min = Math.min(left, max); ans += min*a[cur]; left -= min; cur++; } ans += left*a[cur]; System.out.println(ans); } out.close(); } 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; } int[] readInt(int size) { int[] arr = new int[size]; for(int i = 0; i < size; i++) arr[i] = Integer.parseInt(next()); return arr; } long[] readLong(int size) { long[] arr = new long[size]; for(int i = 0; i < size; i++) arr[i] = Long.parseLong(next()); return arr; } int[][] read2dArray(int rows, int cols) { int[][] arr = new int[rows][cols]; for(int i = 0; i < rows; i++) { for(int j = 0; j < cols; j++) arr[i][j] = Integer.parseInt(next()); } return arr; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
7574a9fe0cff6c4edac6d540eb4e6377
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; public class CodeForces { class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { try { br = new BufferedReader( new FileReader("input.txt")); PrintStream out = new PrintStream(new FileOutputStream("output.txt")); System.setOut(out); } catch (Exception e) { 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; } } void shuffle(long a[], int n) { for (int i = 0; i < n; i++) { int t = (int) Math.random() * a.length; long x = a[t]; a[t] = a[i]; a[i] = x; } } long lcm(int a, int b) { long val = a * b; return val / gcd(a, b); } void swap(long a[], long b[], int i) { long temp = a[i]; a[i] = b[i]; b[i] = temp; } long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } long mod = (long) 1e9 + 7, c; FastReader in; PrintWriter o; public static void main(String[] args)throws IOException { new CodeForces().run(); } void run() throws IOException{ in = new FastReader(); OutputStream op = System.out; o = new PrintWriter(op); int t; // t = 1; t = in.nextInt(); for (int i = 1; i <= t; i++) { helper(); o.println(); } o.flush(); o.close(); } public void helper() { int n = in.nextInt(), k = in.nextInt(); long a[] = new long[n]; for (int j = 0; j < n; j++) a[j] = in.nextLong(); for (int i=0;i<n;i++) { long cur=1; while(a[i]-->0)cur*=10; a[i]=cur; } long res = 0;k++; for (int i = 0; i < n; i++) { int cnt = k; if (i + 1 < n) cnt = Math.min(cnt, (int)(a[i + 1] / a[i] - 1)); res += a[i] * 1L * cnt; k -= cnt; } o.print(res); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
27a41a04c75a3e357b09eafb34235f13
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; public class MyCpClass{ public static void main(String []args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuilder sb = new StringBuilder(); int T = Integer.parseInt(br.readLine().trim()); while(T-- > 0){ String []ip = br.readLine().trim().split(" "); int N = Integer.parseInt(ip[0]); int K = Integer.parseInt(ip[1]); String []ip2 = br.readLine().trim().split(" "); int []a = new int[N]; for(int i=0; i<N; i++) a[i] = Integer.parseInt(ip2[i]); long s = 0; K++; for(int i=0; i<N-1; i++){ int d = (int)Math.pow(10, a[i+1]-a[i])-1; int sp = Math.min(K, d); K -= sp; s += sp*(long)Math.pow(10, a[i]); if(K == 0) break; } if(K > 0) s += K*(long)Math.pow(10, a[N-1]); sb.append(s + "\n"); } System.out.println(sb); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
c55f8045f796962cd3087c49575cafda
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.lang.reflect.Array; import java.util.*; import java.lang.*; public class Solution{ static class Graph{ public static class Vertex{ HashMap<Integer,Integer> nb= new HashMap<>(); // for neighbours of each vertex } public static HashMap<Integer,Vertex> vt; // for vertices(all) public Graph(){ vt= new HashMap<>(); } public static int numVer(){ return vt.size(); } public static boolean contVer(int ver){ return vt.containsKey(ver); } public static void addVer(int ver){ Vertex v= new Vertex(); vt.put(ver,v); } public static void addEdge(int ver1, int ver2, int weight){ if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){ return; } Vertex v1= vt.get(ver1); Vertex v2= vt.get(ver2); v1.nb.put(ver2,weight); // if previously there is an edge, then this replaces that edge v2.nb.put(ver1,weight); } public static void delEdge(int ver1, int ver2){ if(!vt.containsKey(ver1) || !vt.containsKey(ver2)){ return; } vt.get(ver1).nb.remove(ver2); vt.get(ver2).nb.remove(ver1); } public static void delVer(int ver){ if(!vt.containsKey(ver)){ return; } Vertex v1= vt.get(ver); ArrayList<Integer> arr= new ArrayList<>(v1.nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int s= arr.get(i); vt.get(s).nb.remove(ver); } vt.remove(ver); } static boolean done[]; static int parent[]; static ArrayList<Integer>vals= new ArrayList<>(); public static boolean isCycle(int i){ Stack<Integer>stk= new Stack<>(); stk.push(i); while(!stk.isEmpty()){ int x= stk.pop(); vals.add(x); // System.out.print("current="+x+" stackinit="+stk); if(!done[x]){ done[x]=true; } else if(done[x] ){ return true; } ArrayList<Integer>ar= new ArrayList<>(vt.get(x).nb.keySet()); for (int j = 0; j <ar.size() ; j++) { if(parent[x]!=ar.get(j)){ parent[ar.get(j)]=x; stk.push(ar.get(j)); } } // System.out.println(" stackfin="+stk); } return false; } static int[]level; static int[] curr; static int[] fin; public static void dfs(int src){ done[src]=true; level[src]=0; Queue<Integer>q= new LinkedList<>(); q.add(src); while(!q.isEmpty()){ int x= q.poll(); ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet()); for (int i = 0; i <arr.size() ; i++) { int v= arr.get(i); if(!done[v]){ level[v]=level[x]+1; done[v]=true; q.offer(v); } } } } static int oc[]; static int ec[]; public static void dfs1(int src){ Queue<Integer>q= new LinkedList<>(); q.add(src); done[src]= true; // int count=0; while(!q.isEmpty()){ int x= q.poll(); // System.out.println("x="+x); int even= ec[x]; int odd= oc[x]; if(level[x]%2==0){ int val= (curr[x]+even)%2; if(val!=fin[x]){ // System.out.println("bc"); even++; vals.add(x); } } else{ int val= (curr[x]+odd)%2; if(val!=fin[x]){ // System.out.println("bc"); odd++; vals.add(x); } } ArrayList<Integer>arr= new ArrayList<>(vt.get(x).nb.keySet()); // System.out.println(arr); for (int i = 0; i <arr.size() ; i++) { int v= arr.get(i); if(!done[v]){ done[v]=true; oc[v]=odd; ec[v]=even; q.add(v); } } } } static long popu[]; static long happy[]; static long count[]; // total people crossing that pos static long sum[]; // total sum of happy people including that. public static void bfs(int x){ done[x]=true; long total= popu[x]; // long smile= happy[x]; ArrayList<Integer>nbrs= new ArrayList<>(vt.get(x).nb.keySet()); for (int i = 0; i <nbrs.size() ; i++) { int r= nbrs.get(i); if(!done[r]){ bfs(r); total+=count[r]; // smile+=sum[r]; } } count[x]=total; // sum[x]=smile; } public static void bfs1(int x){ done[x]=true; // long total= popu[x]; long smile= 0; ArrayList<Integer>nbrs= new ArrayList<>(vt.get(x).nb.keySet()); for (int i = 0; i <nbrs.size() ; i++) { int r= nbrs.get(i); if(!done[r]){ bfs1(r); // total+=count[r]; smile+=happy[r]; } } // count[x]=total; sum[x]=smile; } } static class rr { static BufferedReader reader; static StringTokenizer tokenizer; static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } 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 double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } } static class disjointSet{ HashMap<Integer, Node> mp= new HashMap<>(); public static class Node{ int data; Node parent; int rank; } public void create(int val){ Node nn= new Node(); nn.data=val; nn.parent=nn; nn.rank=0; mp.put(val,nn); } public int findparent(int val){ return findparentn(mp.get(val)).data; } public Node findparentn(Node n){ if(n==n.parent){ return n; } Node rr= findparentn(n.parent); n.parent=rr; return rr; } public void union(int val1, int val2){ // can also be used to check cycles Node n1= findparentn(mp.get(val1)); Node n2= findparentn(mp.get(val2)); if(n1.data==n2.data) { return; } if(n1.rank<n2.rank){ n1.parent=n2; } else if(n2.rank<n1.rank){ n2.parent=n1; } else { n2.parent=n1; n1.rank++; } } } static class Pair implements Comparable<Pair>{ int x; int y; // smallest form only public Pair(int x, int y){ this.x=x; this.y=y; } @Override public int compareTo(Pair x){ if(this.x>x.x){ return 1; } else if(this.x==x.x){ if(this.y>x.y){ return 1; } else if(this.y==x.y){ return 0; } else{ return -1; } } return -1; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; return x == pair.x && y == pair.y; } @Override public int hashCode() { return Objects.hash(x, y); } public String toString(){ return x+" "+y; } } public static void main(String[] args) throws IOException { rr.init(System.in); // Scanner sc= new Scanner(System.in); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out)); int t= rr.nextInt(); outer: for (int tt = 0; tt <t ; tt++) { int n= rr.nextInt(); long k= rr.nextLong(); long arr[]= new long[n]; for (int i = 0; i <n ; i++) { arr[i]= power(10,rr.nextLong(),(long)1e18); } long ans=0; for (int i = 0; i <arr.length-1 ; i++) { long val= arr[i+1]/arr[i]; val--; if(k>=val){ k-=val; ans+=val*arr[i]; } else{ out.append((ans+(k+1)*arr[i])+"\n"); continue outer; } } k++; ans+=k*arr[n-1]; out.append(ans+"\n"); continue outer; } out.flush(); out.close(); } static long power( long x, long y, long p) { long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
6032722ecc9fab5a47ab570ae4062987
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; // cd C:\Users\Lenovo\Desktop\New //ArrayList<Integer> a=new ArrayList<>(); //List<Integer> lis=new ArrayList<>(); //StringBuilder ans = new StringBuilder(); //HashMap<Integer,Integer> map=new HashMap<>(); public class fast { static FastReader in=new FastReader(); static final Random random=new Random(); static long mod=1000000007L; public static void main(String args[]) throws IOException { FastReader sc=new FastReader(); //Scanner s=new Scanner(System.in); int tt=sc.nextInt(); //int tt=1; while(tt-->0){ //System.out.println(t); int n=sc.nextInt(); int k=sc.nextInt()+1; int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } long result = 0l; for (int i = 0; i < n; i++) { long range = k; if (i < n - 1) { range = Math.min(range, pow(10, a[i + 1] - a[i]) - 1); } if (range == 0) break; result += range * pow(10, a[i]); k -= range; } System.out.println(result); } } static long pow(long a, long b) { if (b == 0) return 1; long res = pow(a, b / 2); res = res * res; if (b % 2 == 1) { res = res * a; } return res; } static void dfs(int [][]arr,int row,int col,boolean [][]seen,int n){ if(row<0 || col<0 || row==n || col==n){ return; } if(arr[row][col]==1){ return; } if(seen[row][col]==true){ return; } seen[row][col]=true; dfs(arr,row+1,col,seen,n); dfs(arr,row,col+1,seen,n); dfs(arr,row-1,col,seen,n); dfs(arr,row,col-1,seen,n); } static int msb(int x){ int ans=0; while(x!=0){ x=x/2; ans++; } return ans; } static void ruffleSort(int[] a) { int n=a.length; for (int i=0; i<n; i++) { int oi=random.nextInt(n), temp=a[oi]; a[oi]=a[i]; a[i]=temp; } Arrays.sort(a); } static int gcd(int a,int b) { if(b==0) { return a; } return gcd(b,a%b); } /* static boolean checkprime(int n1) { if(n1%2==0||n1%3==0) return false; else { for(int i=5;i*i<=n1;i+=6) { if(n1%i==0||n1%(i+2)==0) return false; } return true; } } */ /* Iterator<Map.Entry<Integer, Integer>> iterator = map.entrySet().iterator(); while(iterator.hasNext()){ Map.Entry<Integer, Integer> entry = iterator.next(); int value = entry.getValue(); if(value==1){ iterator.remove(); } else{ entry.setValue(value-1); } } */ static class Pair implements Comparable { int a,b; public String toString() { return a+" " + b; } public Pair(int x , int y) { a=x;b=y; } @Override public int compareTo(Object o) { Pair p = (Pair)o; if(p.a!=a){ return a-p.a;//in } else{ return p.b-b;// } } } /* public static boolean checkAP(List<Integer> lis){ for(int i=1;i<lis.size()-1;i++){ if(2*lis.get(i)!=lis.get(i-1)+lis.get(i+1)){ return false; } } return true; } public static int minBS(int[]arr,int val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]>=val){ r=mid; } else{ l=mid; } } return r; } public static int maxBS(int[]arr,int val){ int l=-1; int r=arr.length; while(r>l+1){ int mid=(l+r)/2; if(arr[mid]<=val){ l=mid; } else{ r=mid; } } return l; } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; }*/ 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; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
d79376c35c5064e81717cc971ed1be6a
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
/** * * YUVRAJ PARASHAR * * */ import static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.util.*; import java.io.*; import java.math.*; public class Main{ // constants___________________________________________________________________________________________________________________________ static FastReader sc = new FastReader(); static final long inf = 0x1fffffffffffffffL; static final int iinf = 0x3fffffff; static final double eps = 1e-9; static long mod = 1000000007; // solve and main method___________________________________________________________________________________________________________________________ static void solve(){ int n,k; n = sc.nextInt(); k = sc.nextInt(); k++; List<Integer> a = new ArrayList<>(); for(int i = 0; i < n; i++){ int t = sc.nextInt(); a.add((int)Math.pow(10, t)); } int cnt = 0; long res = 0; for(int i = 0; i < n; i++){ cnt = k; if(i+1 < n){ cnt = min(cnt, a.get(i+1)/a.get(i)-1); } res += (long) cnt*a.get(i); k -= cnt; } System.out.println(res); } public static void main(String[] args) { int t = 1; t = sc.nextInt(); while(t-- > 0){ solve(); } } // Other Important Methods _________________________________________________________________________________________________________________________________ // Fast Reader static class FastReader{ BufferedReader br; StringTokenizer st; public FastReader(){ br=new BufferedReader(new InputStreamReader(System.in)); } 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()); } String nextLine(){ String str=""; try { str=br.readLine().trim(); } catch (Exception e) { e.printStackTrace(); } return str; } } // print array [for debugging purpose only] public static void print(int arr[]){ for(int x : arr){ System.out.println(x+" "); }System.out.println(); } // GCD - Greatest Common Divisor public static long gcd(long a, long b){ if(b == 0L){ return a; } return gcd(b, a%b); } // sort public static void sort(int[] arr){ // Arrays.sort is Quick Sort which can take upto O(N^2) if array is in reverse order ArrayList<Integer> ls = new ArrayList<>(); for(int x : arr){ ls.add(x); } Collections.sort(ls); // Merge sort - Always O(N*log N) for(int i = 0; i < arr.length; i++){ arr[i] = ls.get(i); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
2cf393a910c4885fc996d520941842ae
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.PrintStream; import java.util.Scanner; public class C { public static void main(String[] args) { Scanner sc = new Scanner(System.in); PrintStream out = System.out; int t = sc.nextInt(); for (int i = 0; i < t; i++) { int n = sc.nextInt(); int k = sc.nextInt() + 1; int[] a = new int[n]; for (int j = 0; j < n; j++) { a[j] = (int) Math.pow(10, sc.nextInt()); } out.println(solve(a, k)); } } private static long solve(int[] a, int k) { long res = 0; int left = k; for (int i = 0; i < a.length; i++) { int digit = left; if (i < a.length - 1) { digit = Math.min(digit, a[i + 1] / a[i] - 1); } left -= digit; res += (long) a[i] * digit; } return res; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
17af7bd3321a2d46edc116bbe1f82aa6
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.util.stream.Collectors; import java.io.*; import java.math.*; public class ER116_C { public static FastScanner sc; public static int MOD= 1000000007; public static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) {} return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static int gcd(int a, int b) { if(b==0) return a; return gcd(b,a%b); } public static int lcm(int a, int b) { return((a*b)/gcd(a,b)); } public static void decreasingOrder(ArrayList<Integer> al) { Comparator<Integer> c = Collections.reverseOrder(); Collections.sort(al,c); } public static boolean[] sieveOfEratosthenes(int n) { boolean isPrime[] = new boolean[n+1]; Arrays.fill(isPrime, true); isPrime[0]=false; isPrime[1]=false; for(int i=2;i*i<n;i++) { for(int j=2*i;j<n;j+=i) { isPrime[j]=false; } } return isPrime; } public static boolean isPalindrome(int n){ int num=0; int temp=n; while(n>0) { num=(num*10)+(n%10); n=n/10; } if(temp==num) return true; else return false; } public static int nCr(int N, int R) { double result=1; for(int i=1;i<=R;i++){ result=((result*(N-R+i))/i); } return (int) result; } public static void printList(List<Integer> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printList(Deque<Integer> al) { System.out.println(al.stream().map(it -> it.toString()).collect(Collectors.joining(" "))); } public static void printArr(int[] arr) { System.out.println(Arrays.toString(arr).trim().replace("[", "").replace("]","").replace(","," ")); } public static long fastPower_v2(long a, long b, int n) { long result=1; while(b>0) { if((b&1)==1) result=(result*a %n)%n; a=(a%n * a%n)%n; b>>=1; } return result; } public static void solve(int t) { int n=sc.nextInt(); long k=sc.nextLong(); k++; long[] arr = new long[n]; for(int i=0;i<n;i++) arr[i]=(long)Math.pow(10,sc.nextLong()); long temp=k; long ans=0; for(int i=0;i<n-1;i++) { long count=0; count=Math.min(temp, (arr[i+1]/arr[i])-1); temp-=count; ans+=(arr[i]*count); } if(temp>0) ans+=(temp*arr[n-1]); System.out.println(ans); } public static void main(String[] args) { sc = new FastScanner(); int t=sc.nextInt(); for(int i=1;i<=t;i++){ solve(i); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
123068dcb51d839caf111aa289130019
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.Scanner; public class CF_1606 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-->0){ int n = sc.nextInt(); long k = sc.nextInt(); Integer[] d= new Integer[n]; for(int i = 0;i<n;i+=1){ d[i] = sc.nextInt(); } long ans = 0; for(int i = 0;i<n;i+=1){ int temp = 0; if(i<n-1){ temp = (int) ((Math.pow(10,d[i+1]))/Math.pow(10,d[i])) - 1; } if(temp != 0 && k>= temp){ k-=temp; ans+=(temp*Math.pow(10,d[i])); }else{ ans= (ans+((k+1)*(long)Math.pow(10,d[i]))); break; } } System.out.println(ans); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
1e7a610e18f4db4bf43ba949f55f1ffd
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
/* package codechef; // don't place package name! */ import java.io.*; public final class PROBOBAL { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t=Integer.parseInt(br.readLine()),b=0,n=0,j=0,c=0; long l=0,c1=0; for(int i=1;i<=t;i++) { long d=0; String arg1[]=br.readLine().split(" "); n=Integer.parseInt(arg1[0]); b=Integer.parseInt(arg1[1]); b=b+1; long a[]=new long[n]; String arg[]=br.readLine().split(" "); for(j=0;j<n;j++) { a[j]=Integer.parseInt(arg[j]); } l=1; c=0; for(j=0;j<=9&&c<n;j++) { if(a[c]==j) { a[c]=l; c++; } l*=10; } for(j=0;j<n-1&&b>0;j++) { c1=a[j+1]-a[j]; c1/=a[j]; if(c1>b) { d+=b*a[j]; b=0; } else { d+=c1*a[j]; b-=c1; } } if(b>0) d+=a[n-1]*b; System.out.println(d); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
f72290e8de1bb728f8fe0e0fd6d0da4d
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; import java.io.*; public class Omar { static PrintWriter pw = new PrintWriter(System.out); static Scanner sc=new Scanner(System.in); public static void main(String[] args) throws IOException { int t=sc.nextInt(); while(t-->0) { long n=sc.nextLong(),k=sc.nextLong(); TreeSet<Long> ts=new TreeSet<>(); long []pow=new long[(int) n]; for(int i=0;i<n;i++) { long x=(long)Math.pow(10, sc.nextInt()); ts.add(x); pow[i]=x; } long []ans=new long[19]; long min=1; for(long i=1,c=0;c<19;c++,i*=10) { if(ts.floor(i)==null) { ans[(int)c]=0; } else { long build=0,need=0; for(long j=i,c1=c;c1>0 && j>=1;j/=10,c1--) { if(ts.lower(j)==null || ts.ceiling(j)==null) { build=1; break; } else { if(j==10) { need+=8; build+=8; continue; } // pw.println(j+" "+ts.lower(j)); need+=(long)(1.0*(j-j/10)/ts.lower(j)); build+=(j-j/10); } } // pw.println(need+" "+c); if(need>k || build==1) { ans[(int) c]=0; } else { build+=(k-need)*ts.floor(i); ans[(int) c]=build; } } } for(int i=1;i<10;i++) { if(ans[i]==0) { min=ans[i-1]; break; } else min=ans[i]; } // pw.println(Arrays.toString(ans)); pw.println(min+1); } pw.flush(); } static class pair implements Comparable<pair> { long x; long y; public pair(long x, long y) { this.x = x; this.y = y; } public String toString() { return x + " " + y; } public boolean equals(Object o) { if (o instanceof pair) { pair p = (pair) o; return p.x == x && p.y == y; } return false; } public int hashCode() { return new Long(x).hashCode() * 31 + new Long(y).hashCode(); } public int compareTo(pair other) { if (this.x == other.x) { return Long.compare(this.y, other.y); } return Long.compare(this.x, other.x); } } static class tuble implements Comparable<tuble> { int x; int y; int z; public tuble(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } public String toString() { return x + " " + y + " " + z; } public int compareTo(tuble other) { if (this.x == other.x) { if (this.y == other.y) { return this.z - other.z; } return this.y - other.y; } else { return this.x - other.x; } } } static class PairPoint { pair p1; pair p2; public PairPoint(pair p1, pair p2) { this.p1 = p1; this.p2 = p2; } public String toString() { return p1.toString() + p2.toString(); } public boolean equals(PairPoint pp) { if (p1.equals(pp.p1) && p2.equals(pp.p2)) return true; return false; } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(String file) throws IOException { br = new BufferedReader(new FileReader(file)); } public Scanner(FileReader r) { br = new BufferedReader(r); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public String readAllLines(BufferedReader reader) throws IOException { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line); content.append(System.lineSeparator()); } return content.toString(); } 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 { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; boolean dec = false, neg = false; int start = 0; if (x.charAt(0) == '-') { neg = true; start++; } for (int i = start; i < x.length(); i++) if (x.charAt(i) == '.') { res = Long.parseLong(sb.toString()); sb = new StringBuilder("0"); dec = true; } else { sb.append(x.charAt(i)); if (dec) f *= 10; } res += Long.parseLong(sb.toString()) / f; return res * (neg ? -1 : 1); } 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[] nextLongArray(int n) throws IOException { Long[] a = new Long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } 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 Integer[] nextIntegerArray(int n) throws IOException { Integer[] a = new Integer[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public boolean ready() throws IOException { return br.ready(); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
730655eddcdc2d6410dd90d25de7f0ae
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt() ; while (N -- != 0){ int n = sc.nextInt() ; int k = sc.nextInt() ; k++ ; long[] ans = new long[n + 2] ; for(int i = 1 ;i <= n ; i ++){ ans[i] = 1 ; long s1 = sc.nextLong() ; while (s1-- != 0 ){ ans[i] *= 10 ; } } long sum = 0 ;//前面的纸币所凑出来的钱 long cnt = 0 ;//前面总用的纸张数 ans[n + 1] = (long)1e18 ; for(int i = 1 ; i <= n ; i ++){ if( sum + (k - cnt)* ans[i] >= ans[i + 1] ){ cnt += (ans[ i +1 ] / ans[i] - 1 ) ; sum += (ans[ i +1 ] / ans[i] - 1 ) * ans[i] ; }else{ sum += (k - cnt) * ans[i] ; break; } } System.out.println(sum); } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
85f7cee9cce5bd7302687c0624daddb8
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Banknotes { private static final Scanner scanner = new Scanner(System.in); private static void solve(int t) { int n = scanner.nextInt(); int k = scanner.nextInt() + 1; List<Integer> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { int tmp = scanner.nextInt(); arr.add((int) Math.pow(10, tmp)); } int cnt; long res = 0; for (int i = 0; i < n; i++) { cnt = k; if (i + 1 < n) { cnt = Math.min(cnt , arr.get(i + 1) / arr.get(i) - 1); } res += (long) cnt * arr.get(i); k -= cnt; } System.out.println(res); } public static void main(String[] args) { int t = scanner.nextInt(); while (t > 0) { solve(t); t--; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
ce68fb455733a9ccda57c7fc05e65d76
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
//---#ON_MY_WAY--- import static java.lang.Math.*; import java.io.*; import java.math.*; import java.util.*; public class apples { static class pair { int x, y; public pair(int a, int b) { x = a; y = b; } } static FastReader x = new FastReader(); static OutputStream outputStream = System.out; static PrintWriter out = new PrintWriter(outputStream); /*---------------------------------------CODE STARTS HERE-------------------------*/ public static void main(String[] args) throws NumberFormatException, IOException { long startTime = System.nanoTime(); int mod = 1000000007; int t = x.nextInt(); StringBuilder str = new StringBuilder(); while (t > 0) { int n = x.nextInt(); int k = x.nextInt(); long a[] = new long[n]; for (int i = 0; i < n; i++) { a[i] = (int) pow(10, x.nextLong()); } if(n==1) { str.append((k+1)); } else { k++; long ans = 0; for (int i = 0; i < n-1; i++) { ans += a[i]*min(k, (a[i+1]/a[i])-1); k -= min(k, (a[i+1]/a[i])-1); } ans += a[n-1]*k; str.append(ans); } str.append("\n"); t--; } out.println(str); out.flush(); long endTime = System.nanoTime(); //System.out.println((endTime-startTime)/1000000000.0); } /*--------------------------------------------FAST I/O--------------------------------*/ 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; } char nextchar() { char ch = ' '; try { ch = (char) br.read(); } catch (IOException e) { e.printStackTrace(); } return ch; } } /*--------------------------------------------BOILER PLATE---------------------------*/ static int[] readarr(int n) { int arr[] = new int[n]; for (int i = 0; i < n; i++) { arr[i] = x.nextInt(); } return arr; } static int[] sortint(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static long[] sortlong(long a[]) { ArrayList<Long> al = new ArrayList<>(); for (long i : a) { al.add(i); } Collections.sort(al); for (int i = 0; i < al.size(); i++) { a[i] = al.get(i); } return a; } static long pow(long x, long y) { long result = 1; while (y > 0) { if (y % 2 == 0) { x = x * x; y = y / 2; } else { result = result * x; y = y - 1; } } return result; } static long pow(long x, long y, long mod) { long result = 1; x %= mod; while (y > 0) { if (y % 2 == 0) { x = (x%mod * x%mod) % mod; y /= 2; } else { result = (result%mod * x%mod) % mod; y--; } } return result; } static int[] revsort(int a[]) { ArrayList<Integer> al = new ArrayList<>(); for (int i : a) { al.add(i); } Collections.sort(al, Comparator.reverseOrder()); for (int i = 0; i < a.length; i++) { a[i] = al.get(i); } return a; } static int[] gcd(int a, int b, int ar[]) { if (b == 0) { ar[0] = a; ar[1] = 1; ar[2] = 0; return ar; } ar = gcd(b, a % b, ar); int t = ar[1]; ar[1] = ar[2]; ar[2] = t - (a / b) * ar[2]; return ar; } static boolean[] esieve(int n) { boolean p[] = new boolean[n + 1]; Arrays.fill(p, true); for (int i = 2; i * i <= n; i++) { if (p[i] == true) { for (int j = i * i; j <= n; j += i) { p[j] = false; } } } return p; } static ArrayList<Integer> primes(int n) { boolean p[] = new boolean[n + 1]; ArrayList<Integer> al = new ArrayList<>(); Arrays.fill(p, true); int i = 0; for (i = 2; i * i <= n; i++) { if (p[i] == true) { al.add(i); for (int j = i * i; j <= n; j += i) { p[j] = false; } } } for (i = i; i <= n; i++) { if (p[i] == true) { al.add(i); } } return al; } static int etf(int n) { int res = n; for (int i = 2; i * i <= n; i++) { if (n % i == 0) { res /= i; res *= (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { res /= n; res *= (n - 1); } return res; } static int gcd(int a, int b) { if (a == 0) { return b; } return gcd(b % a, a); } static long gcd(long a, long b) { if (a == 0) { return b; } return gcd(b % a, a); } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
a5846a7530536c8c5ae3dbf24374bb4f
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; public class Main { public static void main(String[] args) { FastReader input=new FastReader(); PrintWriter out=new PrintWriter(System.out); int T=input.nextInt(); while(T-->0) { int n=input.nextInt(); int k=input.nextInt(); ArrayList<Integer> list=new ArrayList<>(); int arr[]=new int[n]; for(int i=0;i<n;i++) { int a=input.nextInt(); arr[i]=a; } for(int i=0;i<n-1;i++) { int d=arr[i+1]-arr[i]; list.add((int)Math.pow(10,d)-1); } k++; long val=0; for(int i=0;i<n;i++) { if(i==n-1) { val+=(long)Math.pow(10,arr[i])*(long)k; k=0; } else { if(k<=list.get(i)) { val+=(long)Math.pow(10,arr[i])*(long)k; k=0; } else { val+=(long)Math.pow(10,arr[i])*(long)list.get(i); k-=list.get(i); } } } out.println(val); } out.close(); } 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; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
3cc3daac5e0e936fdd44abb136bcee26
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.*; import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.LongStream; public class TaskB { static long mod = 1000000007; static FastScanner scanner; static final StringBuilder result = new StringBuilder(); public static void main(String[] args) { scanner = new FastScanner(); int T = scanner.nextInt(); for (int t = 0; t < T; t++) { solve(t + 1); result.append("\n"); } System.out.println(result); } static void solve(int t) { int n = scanner.nextInt(); long k = scanner.nextLong() + 1; int[] a = scanner.nextIntArray(n); long[] b = new long[n]; for (int i = 0; i < n; i++) { b[i] = MathUtils.modpow(10, a[i], mod * 100); } BigInteger res = BigInteger.ZERO; for (int i = 1; i < n; i++) { long count = b[i] / b[i - 1] - 1; if (k <= count) { res = res.add(BigInteger.valueOf(k * b[i - 1])); result.append(res); return; } res = res.add(BigInteger.valueOf(count * b[i - 1])); k -= count; } result.append(res.add(BigInteger.valueOf(k * b[n - 1]))); } static class Data implements Comparable<Data> { int a, b, m, idx, x; public Data(int a, int b, int m, int idx) { this.a = a; this.b = b; this.m = m; this.idx = idx; } @Override public int compareTo(Data o) { if (a == o.a) { return Integer.compare(b, o.b); } return Integer.compare(a, o.a); } } static class WithIdx implements Comparable<WithIdx> { int val, idx; public WithIdx(int val, int idx) { this.val = val; this.idx = idx; } @Override public int compareTo(WithIdx o) { if (val == o.val) { return Integer.compare(idx, o.idx); } return Integer.compare(val, o.val); } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } String nextLine() { try { return br.readLine(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(); } } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } int[] nextIntArray(int n) { int[] res = new int[n]; for (int i = 0; i < n; i++) res[i] = nextInt(); return res; } long[] nextLongArray(int n) { long[] res = new long[n]; for (int i = 0; i < n; i++) res[i] = nextLong(); return res; } String[] nextStringArray(int n) { String[] res = new String[n]; for (int i = 0; i < n; i++) res[i] = nextToken(); return res; } } static class PrefixSums { long[] sums; public PrefixSums(long[] sums) { this.sums = sums; } public long sum(int fromInclusive, int toExclusive) { if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value"); return sums[toExclusive] - sums[fromInclusive]; } public static PrefixSums of(int[] ar) { long[] sums = new long[ar.length + 1]; for (int i = 1; i <= ar.length; i++) { sums[i] = sums[i - 1] + ar[i - 1]; } return new PrefixSums(sums); } public static PrefixSums of(long[] ar) { long[] sums = new long[ar.length + 1]; for (int i = 1; i <= ar.length; i++) { sums[i] = sums[i - 1] + ar[i - 1]; } return new PrefixSums(sums); } } static class ADUtils { static void sort(int[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap int a = ar[index]; ar[index] = ar[i]; ar[i] = a; } Arrays.sort(ar); } static void reverse(int[] arr) { int last = arr.length / 2; for (int i = 0; i < last; i++) { int tmp = arr[i]; arr[i] = arr[arr.length - 1 - i]; arr[arr.length - 1 - i] = tmp; } } static void sort(long[] ar) { Random rnd = ThreadLocalRandom.current(); for (int i = ar.length - 1; i > 0; i--) { int index = rnd.nextInt(i + 1); // Simple swap long a = ar[index]; ar[index] = ar[i]; ar[i] = a; } Arrays.sort(ar); } } static class MathUtils { static long[] FIRST_PRIMES = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051}; static long[] primes(int to) { long[] all = new long[to + 1]; long[] primes = new long[to + 1]; all[1] = 1; int primesLength = 0; for (int i = 2; i <= to; i++) { if (all[i] == 0) { primes[primesLength++] = i; all[i] = i; } for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j]; j++) { all[(int) (i * primes[j])] = primes[j]; } } return Arrays.copyOf(primes, primesLength); } static long modpow(long b, long e, long m) { long result = 1; while (e > 0) { if ((e & 1) == 1) { /* multiply in this bit's contribution while using modulus to keep * result small */ result = (result * b) % m; } b = (b * b) % m; e >>= 1; } return result; } static long submod(long x, long y, long m) { return (x - y + m) % m; } static long modInverse(long a, long m) { long g = gcdF(a, m); if (g != 1) { throw new IllegalArgumentException("Inverse doesn't exist"); } else { // If a and m are relatively prime, then modulo // inverse is a^(m-2) mode m return modpow(a, m - 2, m); } } static public long gcdF(long a, long b) { while (b != 0) { long na = b; long nb = a % b; a = na; b = nb; } return a; } } } /* 5 3 2 3 8 8 2 8 5 10 1 */
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
520b08c79d04710ed70e4f5c5143d0e7
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.util.*; public class Contest_yandexA{ public static void main(String[] args) { Scanner input = new Scanner(System.in); int t = input.nextInt(); for(int tt = 0;tt<t;tt++){ int n = input.nextInt(); int k = input.nextInt()+1; int[] a = new int[n]; for(int i = 0;i<n;i++){ a[i] = input.nextInt(); } long ans = 0; for(int i = 0;i<n-1 && k > 0;i++){ int x = (int)Math.pow(10,a[i+1]-a[i])-1; x = Math.min(x,k); ans+= (long)Math.pow(10,a[i])*x; k-= x; } ans+= (long)Math.pow(10,a[n-1])*k; System.out.println(ans); } } public static int gcd(int a,int b){ if(b == 0){ return a; } return gcd(b,a%b); } public static int lcm(int a,int b){ return (a / gcd(a, b)) * b; } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output
PASSED
b2b97869881df6d168577b014bd69edb
train_108.jsonl
1635518100
In Berland, $$$n$$$ different types of banknotes are used. Banknotes of the $$$i$$$-th type have denomination $$$10^{a_i}$$$ burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly $$$1$$$.Let's denote $$$f(s)$$$ as the minimum number of banknotes required to represent exactly $$$s$$$ burles. For example, if the denominations of banknotes used in Berland are $$$1$$$, $$$10$$$ and $$$100$$$, then $$$f(59) = 14$$$: $$$9$$$ banknotes with denomination of $$$1$$$ burle and $$$5$$$ banknotes with denomination of $$$10$$$ burles can be used to represent exactly $$$9 \cdot 1 + 5 \cdot 10 = 59$$$ burles, and there's no way to do it with fewer banknotes.For a given integer $$$k$$$, find the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes (that is, $$$f(s) &gt; k$$$).
256 megabytes
import java.io.*; import java.util.*; import java.lang.*; public class Main{ public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); static long MOD = (long) (1e9 + 7); // static long MOD = 998244353; static long MOD2 = MOD * MOD; static FastReader sc = new FastReader(); static int pInf = Integer.MAX_VALUE; static int nInf = Integer.MIN_VALUE; static long ded = (long)(1e17)+9; public static void main(String[] args) throws Exception { int test = 1; test = sc.nextInt(); for (int i = 1; i <= test; i++){ // out.print("Case #"+i+": "); solve(); } out.flush(); out.close(); } static void solve(){ int n = sc.nextInt(); int k = sc.nextInt()+1; long[] a = new long[n]; for(int i = 0; i < n; i++){ int x = sc.nextInt(); a[i] = 1; while (x-->0){ a[i] = a[i]*10; } } ruffleSort(a); long sum = 0; int p = 0; while (k>0&&p<n){ long rem = k; if(p<n-1){ rem = Math.min(rem,(a[p+1]-1)/a[p]); } k -= rem; sum += (rem*a[p]); p++; } out.println(sum); } static long lcm(long a, long b){ return (a*b)/gcd(a,b); } public static long mul(long a, long b) { return ((a % MOD) * (b % MOD)) % MOD; } public static long sub(long a, long b) { return ((a%MOD)-(b%MOD)+MOD)%MOD; } public static long add(long a, long b) { if((a+b)>MOD){ return a+b-MOD; }else{ return a+b; } // return ((a % MOD) + (b % MOD)) % MOD; } static class Pair implements Comparable<Pair> { int x; int y; int idx; public Pair(int x, int y,int idx) { this.x = x; this.y = y; this.idx = idx; } @Override public int compareTo(Pair o){ if(this.y==o.y){ return Integer.compare(this.x,o.x); } return Integer.compare(this.y,o.y); } @Override public String toString() { return "Pair{" + "x=" + x + ", y=" + y + '}'; // return x+" "+y; } // public boolean equals(Pair o){ // return this.x==o.x&&this.y==o.y; // } } public static long c2(long n) { if ((n & 1) == 0) { return mul(n / 2, n - 1); } else { return mul(n, (n - 1) / 2); } } //Shuffle Sort static final Random random = new Random(); 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); } //Brian Kernighans Algorithm static long countSetBits(long n) { if (n == 0) return 0; return 1 + countSetBits(n & (n - 1)); } //Euclidean Algorithm static long gcd(long A, long B) { if (B == 0) return A; return gcd(B, A % B); } //Modular Exponentiation static long fastExpo(long x, long n) { if (n == 0) return 1; if ((n & 1) == 0) return fastExpo((x * x) % MOD, n / 2) % MOD; return ((x % MOD) * fastExpo((x * x) % MOD, (n - 1) / 2)) % MOD; } //AKS Algorithm static boolean isPrime(long n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i <= Math.sqrt(n); i += 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } public static long modinv(long x) { return modpow(x, MOD - 2); } public static long modpow(long a, long b) { if (b == 0) { return 1; } long x = modpow(a, b / 2); x = (x * x) % MOD; if (b % 2 == 1) { return (x * a) % MOD; } return x; } 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; } } }
Java
["4\n3 13\n0 1 2\n2 777\n0 4\n3 255\n0 1 3\n10 1000000000\n0 1 2 3 4 5 6 7 8 9"]
2 seconds
["59\n778\n148999\n999999920999999999"]
null
Java 8
standard input
[ "greedy", "number theory" ]
e25e44b8f300d6be856df50bff8ff244
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \le n \le 10; 1 \le k \le 10^9$$$). The next line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 = a_1 &lt; a_2 &lt; \dots &lt; a_n \le 9$$$).
1,400
For each test case, print one integer — the minimum positive number of burles $$$s$$$ that cannot be represented with $$$k$$$ or fewer banknotes.
standard output