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
288468fdb343224efa361e617b6a923f
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.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); // int a = 1; int t; //t = in.nextInt(); t = 1; while (t > 0) { // out.print("Case #"+(a++)+": "); solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n, p, a, b; n = in.nextInt(); p = in.nextInt(); int[] array = new int[n]; long ans = 0; long[] fab = new long[200005]; long[] pre = new long[200005]; for (int i = 0; i < n; i++) { array[i] = in.nextInt(); } fab[0] = 1; fab[1] = 1; pre[0] = 1; pre[1] = 2; int mod = (int)(1e9 + 7); for (int i = 2; i < fab.length; i++) { fab[i] = (fab[i - 1] %mod+ fab[i - 2] % mod)%mod; pre[i] = (pre[i - 1]%mod + fab[i]%mod)%mod; } Arrays.sort(array); Set<Integer> set = new HashSet<>(); int min = array[0]; for (int i = 0; i < n; i++) { b = 0; boolean flag = ans(array[i], set, min); set.add(array[i]); if(flag){ continue; } while (array[i]!=1){ b++; array[i]/=2; } a = p - b - 1; if(a < 0){ break; } ans = (ans % mod + pre[a]%mod)%mod; } out.println(ans); } public boolean ans(int num, Set<Integer> set, int first){ if(set.contains(num)){ return true; } if(num < first){ return false; } boolean flag = false; if((num - 1) % 2==0) { flag = ans((num-1)/2, set, first); } if(num %4==0) { flag |= ans(num / 4, set, first); } return flag; } } static int gcd(int a, int b ) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ int a, b; public answer(int a, int b, int c) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return Integer.compare(this.b, o.b); } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(answer1 o) { return (o.a - this.a); } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (Long i:a) l.add(i); l.sort(Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(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 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()); } public 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 8
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
edc46df08af978a4b3323aede4460122
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.*; import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskA solver = new TaskA(); // int a = 1; int t; //t = in.nextInt(); t = 1; while (t > 0) { // out.print("Case #"+(a++)+": "); solver.call(in,out); t--; } out.close(); } static class TaskA { public void call(InputReader in, PrintWriter out) { int n, p, a, b; n = in.nextInt(); p = in.nextInt(); int[] array = new int[n]; long ans = 0; long[] fab = new long[200005]; long[] pre = new long[200005]; for (int i = 0; i < n; i++) { array[i] = in.nextInt(); } fab[0] = 1; fab[1] = 1; pre[0] = 1; pre[1] = 2; int mod = (int)(1e9 + 7); for (int i = 2; i < fab.length; i++) { fab[i] = (fab[i - 1] %mod+ fab[i - 2] % mod)%mod; pre[i] = (pre[i - 1]%mod + fab[i]%mod)%mod; } Arrays.sort(array); Set<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { set.add(array[i]); a = 0; b = 0; boolean flag = true; for(int x=array[i]&3, y=array[i]; x!=2 ; x=y&3){ y >>= (2-(x&1)); if( y < array[0])break; if(set.contains(y)) { flag=false; break; } } if(!flag){ continue; } while (array[i]!=1){ b++; array[i]/=2; } a = p - b - 1; if(a < 0){ break; } ans = (ans % mod + pre[a]%mod)%mod; } out.println(ans); } } static int gcd(int a, int b ) { if (a == 0) return b; return gcd(b % a, a); } static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } static class answer implements Comparable<answer>{ int a, b; public answer(int a, int b, int c) { this.a = a; this.b = b; } @Override public int compareTo(answer o) { return Integer.compare(this.b, o.b); } } static class answer1 implements Comparable<answer1>{ int a, b, c; public answer1(int a, int b, int c) { this.a = a; this.b = b; this.c = c; } @Override public int compareTo(answer1 o) { return (o.a - this.a); } } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void sort(long[] a) { ArrayList<Long> l = new ArrayList<>(); for (Long i:a) l.add(i); l.sort(Collections.reverseOrder()); for (int i=0; i<a.length; i++) a[i]=l.get(i); } static final Random random=new Random(); static void shuffleSort(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 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()); } public 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 8
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
ca2872f4717305b528a17f94db2ef578
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.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.*; import java.util.concurrent.ThreadLocalRandom; //import javax.print.attribute.HashAttributeSet; //import com.sun.tools.javac.code.Attribute.Array; public class c731 { public static void main(String[] args) throws IOException { // try { BufferedWriter out = new BufferedWriter( new OutputStreamWriter(System.out)); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); PrintWriter pt = new PrintWriter(System.out); FastReader sc = new FastReader(); // int t = sc.nextInt(); //for(int o = 0; o<t;o++) { // //} // out.flush(); // int mod = (int)1e9 + 7; // long[] fib = new long[(int) 1e6]; // fib[0] = 1; // fib[1] = 1; // for (int i = 2; i < fib.length; i++) { // fib[i] = (fib[i - 1] + fib[i - 2]) % mod; // } // for (int i = 1; i < fib.length; i++) { // fib[i] = (fib[i - 1] + fib[i]) % mod; // } // System.out.println(fib[2] + " " + fib[3] + " " + fib[5]); int n = sc.nextInt(); int p = sc.nextInt(); long[] arr = new long[n]; HashSet<Long> set = new HashSet<Long>(); for(int i = 0 ; i<n;i++) { arr[i] = sc.nextLong(); set.add(arr[i]); } Arrays.sort(arr); long m = (long)1e9 + 7; long ans = 0; long[] dp = new long[2000005]; dp[0] = 1; dp[1] = 2; dp[2] = 4; for(int i = 3 ; i<=p;i++) { dp[i] = (dp[i-1] + dp[i-2])%m; dp[i] = (dp[i]+1)%m; } for(int i = 0 ; i<n;i++) { long x = arr[i]; long temp = x; int f = 0; while(temp>1) { if(temp%4==0) { temp/=4; }else if(temp%2== 1) { temp/= 2; }else { break; } if(set.contains(temp)) { f = 1; break; } } if(f == 1) { continue; } //System.out.println(temp); //System.out.println(temp); //System.out.println(temp); int v = 0; long c = x; while(c>0) { v++; c/=2; } if(v>p) { continue; } int d = p - v; //System.out.println(d); ans = (ans + dp[d])%m; } //System.out.println(); System.out.println(ans); } // // } // // }catch(Exception e) { // return; // } // } //------------------------------------------------------------------------------------------------------------------------------------------------ public static boolean check(int[] arr , int n) { for(int i = 1 ; i<n;i++) { if(arr[i]<arr[i-1]) { return false; } } return true; } public static void dfs(int s , HashMap<Integer, ArrayList<Integer>> map, boolean[] vis) { vis[s] = true; for(int x : map.get(s)) { if(!vis[x]) { dfs(x, map, vis); } } } public static int lis(int[] arr) { int n = arr.length; ArrayList<Integer> al = new ArrayList<Integer>(); al.add(arr[0]); for(int i = 1 ; i<n;i++) { int x = al.get(al.size()-1); if(arr[i]>=x) { al.add(arr[i]); }else { int v = upper_bound(al, 0, al.size(), arr[i]); al.set(v, arr[i]); } } return al.size(); } static int cntDivisors(int n){ int cnt = 0; for (int i=1; i<=Math.sqrt(n); i++) { if (n%i==0) { if (n/i == i) cnt++; else cnt+=2; } } return cnt; } public 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) != 0) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } public static long ncr(long[] fac, int n , int r , long m) { return fac[n]*(modInverse(fac[r], m))%m *(modInverse(fac[n-r], m))%m; } public static int lower_bound(ArrayList<Integer> ar,int lo , int hi , int k) { int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get(mid) <k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k) { int s=lo; int e=hi; while (s !=e) { int mid = s+e>>1; if (ar.get(mid) <=k) { s=mid+1; } else { e=mid; } } if(s==ar.size()) { return -1; } return s; } // ----------------------------------------------------------------------------------------------------------------------------------------------- public static int gcd(int a, int b){ if (a == 0) return b; return gcd(b % a, a); } //-------------------------------------------------------------------------------------------------------------------------------------------------------- public 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; } //----------------------------------------------------------------------------------------------------------------------------------- //segment tree //for finding minimum in range public static void build(long [] seg,long []arr,int idx, int lo , int hi) { if(lo == hi) { seg[idx] = arr[lo]; return; } int mid = (lo + hi)/2; build(seg,arr,2*idx+1, lo, mid); build(seg,arr,idx*2+2, mid +1, hi); // seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]); // seg[idx] = seg[idx*2+1]+ seg[idx*2+2]; seg[idx] = Math.min(seg[idx*2+1], seg[idx*2+2]); } //for finding minimum in range public static long query(long[]seg,int idx , int lo , int hi , int l , int r) { if(lo>=l && hi<=r) { return seg[idx]; } if(hi<l || lo>r) { return 0; } int mid = (lo + hi)/2; long left = query(seg,idx*2 +1, lo, mid, l, r); long right = query(seg,idx*2 + 2, mid + 1, hi, l, r); // return Math.min(left, right); //return gcd(left, right); return Math.min(left, right); } // // for sum // //public static void update(int[]seg,int idx, int lo , int hi , int node , int val) { // if(lo == hi) { // seg[idx] += val; // }else { //int mid = (lo + hi )/2; //if(node<=mid && node>=lo) { // update(seg, idx * 2 +1, lo, mid, node, val); //}else { // update(seg, idx*2 + 2, mid + 1, hi, node, val); //} //seg[idx] = seg[idx*2 + 1] + seg[idx*2 + 2]; // //} //} //--------------------------------------------------------------------------------------------------------------------------------------- // static void shuffleArray(int[] ar) // { // // If running on Java 6 or older, use `new Random()` on RHS here // 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; // } // } //----------------------------------------------------------------------------------------------------------------------------------------------------------- } 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; } } //------------------------------------------------------------------------------------------------------------------------------------------------------------ class SegmentTree{ int n; public SegmentTree(int[] arr,int n) { this.arr = arr; this.n = n; } int[] arr = new int[n]; // int n = arr.length; int[] seg = new int[4*n]; void build(int idx, int lo , int hi) { if(lo == hi) { seg[idx] = arr[lo]; return; } int mid = (lo + hi)/2; build(2*idx+1, lo, mid); build(idx*2+2, mid +1, hi); seg[idx] = Math.min(seg[2*idx+1],seg[2*idx+2]); } int query(int idx , int lo , int hi , int l , int r) { if(lo<=l && hi>=r) { return seg[idx]; } if(hi<l || lo>r) { return Integer.MAX_VALUE; } int mid = (lo + hi)/2; int left = query(idx*2 +1, lo, mid, l, r); int right = query(idx*2 + 2, mid + 1, hi, l, r); return Math.min(left, right); } } //----------------------------------------------------------------------------------------------------------------------------------------------------------- class coup{ int a; int b; public coup(int a , int b) { this.a = a; this.b = b; } } class trip{ int a , b, c; public trip(int a , int b, int c) { this.a = a; this.b = b; this.c = c; } }
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 8
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
e2e76a352c00610a3afe8cee87d89d11
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.BufferedWriter; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.PriorityQueue; import java.util.Scanner; import java.util.Stack; /** * * @author xpeng */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Deque; import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; import java.util.StringTokenizer; import java.util.TreeSet; public class codeforces { 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 BigInteger ans; static class obj { String s; long l,r; obj(String s,long l,long r){ this.s=s;this.l=l;this.r=r; } } static class Q implements Comparable<Q>{ long at; char ans; public Q(long at) { this.at=at; } public int compareTo(Q o) { return Long.compare(at, o.at); } } //static Scanner in = new Scanner(System.in); //static BufferedWriter write = new BufferedWriter(new OutputStreamWriter(System.out)); //static PrintWriter out = new PrintWriter(write); static double r1,r2; static int[] x={1,0,0,0}; static int[] y={0,1,1,-1}; static void cycle(int node,ArrayList<ArrayList<Integer>> graph,boolean[] vis,boolean[] s, ArrayList<Integer> cont){ vis[node]=true; s[node]=true; cont.add(node); for (int i :graph.get(node)) { if (s[i]) { return ; }else{ vis[i]=true; s[i]=true; cycle(i,graph,vis,s,cont); } } //return len; } static boolean check(int[] arr,int x,int y){ // System.out.println(y-1); // System.out.println(arr.length); if (arr[y-1]>=x) { return false; } return true; } static boolean check2(int x,int y,int x2,int y2,int k){ if (Math.abs(x2-x)%k!=0) { return false; } if (Math.abs(y2-y)%k!=0) { return false; } return true; } static int check(int r,int c,int n,int m,int[][] matrix){ int min=4; //1 if (r+1<n&&c+1<m) { int sum=matrix[r][c]+matrix[r+1][c]+matrix[r+1][c+1]; if (sum<min&&sum!=0) { min=sum; } } //2 if (r-1>=0&&c+1<m) { int sum=matrix[r][c]+matrix[r-1][c+1]+matrix[r][c+1]; if (sum<min&&sum!=0) { min=sum; } } //3 if (r+1<n&&c+1<m) { int sum=matrix[r][c]+matrix[r+1][c]+matrix[r][c+1]; if (sum<min&&sum!=0) { min=sum; } } //4 if (r+1<n&&c+1<m) { int sum=matrix[r][c]+matrix[r][c+1]+matrix[r+1][c+1]; if (sum<min&&sum!=0) { min=sum; } } //5 if (r+1<n&&c+1<m) { int sum=matrix[r][c]+matrix[r+1][c]+matrix[r][c+1]; if (sum<min&&sum!=0) { min=sum; } } //6 if (r+1<n&&c+1<m) { int sum=matrix[r][c]+matrix[r+1][c+1]+matrix[r][c+1]; if (sum<min&&sum!=0) { min=sum; } } //7 if (r+1<n&&c+1<m) { int sum=matrix[r][c]+matrix[r+1][c]+matrix[r+1][c+1]; if (sum<min&&sum!=0) { min=sum; } } //8 if (r-1>=0&&c+1<m) { int sum=matrix[r][c]+matrix[r][c+1]+matrix[r-1][c+1]; if (sum<min&&sum!=0) { min=sum; } } return min; } static long mod = (long) (Math.pow(10,9)+7); // 1) make dp array // 2) sort array // 3) check if we have seen this if we have just don't add // // // // static boolean check(TreeSet<Integer> tr,int num){ while(true){ if (tr.contains(num)) { return true; } if ((num&1)==1) { num=num>>1; }else if (Integer.toBinaryString(num).length()>=2&&(num&2)!=2) { num=num>>2; }else{ break; } } return false; } public static void main(String[] args) { FastReader in = new FastReader(); //int test = in.nextInt(); int n = in.nextInt(), p = in.nextInt(); int[] arr = new int[n]; long ans = 0; //long num=(long) (Math.pow(2, p)%mod); for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); // if (Math.log(arr[i])/Math.log(2)<p) { // ans++; // } } Arrays.sort(arr); long[] dp = new long[p + 1]; dp[1] = 1; for (int i = 2; i < p + 1; i++) { if (i==2) { dp[i]=2;continue; } dp[i] = dp[i - 1] + dp[i - 2]; dp[i]=dp[i]%mod; } for (int i = 1; i <= p; i++) { dp[i]+=dp[i-1]; } TreeSet<Integer> tr = new TreeSet(); for (int i = 0; i < n; i++) { if (i != 0) { if (!check(tr, arr[i])) { int len = p - Integer.toBinaryString(arr[i]).length(); if (Math.log(arr[i]) / Math.log(2) < p) { ans++; } if (len<0) { continue; } ans += dp[len]; ans %= mod; tr.add(arr[i]); } } else { int len = p - Integer.toBinaryString(arr[i]).length(); if (len<0) { continue; } ans += dp[len]; if (Math.log(arr[i]) / Math.log(2) < p) { ans++; } // long num = (long) (Math.pow(2, p)%mod); // //1*4^k=num // double calc=(double)Math.log(num/(double)arr[i])/Math.log(4); // if (calc%1==0) { // ans++; // } ans %= mod; tr.add(arr[i]); } } System.out.println(ans); } //out.close(); static int[] constructST(int arr[], int n) { // Height of segment tree int x = (int) Math.ceil(Math.log(n) / Math.log(2)); // Maximum size of segment tree int max_size = 2 * (int) Math.pow(2, x) - 1; // Allocate memory int[] st = new int[max_size]; // Fill the allocated memory st constructSTUtil(arr, 0, n - 1, st, 0); // Return the constructed segment tree return st; } static int constructSTUtil(int arr[], int ss, int se, int[] st, int si) { // If there is one element in array, store // it in current node of segment tree and return if (ss == se) { st[si] = arr[ss]; return arr[ss]; } // If there are more than one elements, then // recur for left and right subtrees and // store the max of values in this node int mid = getMid(ss, se); st[si] = Math.max( constructSTUtil(arr, ss, mid, st, si * 2 + 1), constructSTUtil(arr, mid + 1, se, st, si * 2 + 2)); return st[si]; } static int getMax(int[] st, int n, int l, int r) { // Check for erroneous input values if (l < 0 || r > n - 1 || l > r) { System.out.printf("Invalid Input\n"); return -1; } return MaxUtil(st, 0, n - 1, l, r, 0); } static int MaxUtil(int[] st, int ss, int se, int l, int r, int node) { // If segment of this node is completely // part of given range, then return // the max of segment if (l <= ss && r >= se) return st[node]; // If segment of this node does not // belong to given range if (se < l || ss > r) return -1; // If segment of this node is partially // the part of given range int mid = getMid(ss, se); return Math.max( MaxUtil(st, ss, mid, l, r, 2 * node + 1), MaxUtil(st, mid + 1, se, l, r, 2 * node + 2)); } static void updateValue(int arr[], int[] st, int ss, int se, int index, int value, int node) { if (index < ss || index > se) { System.out.println("Invalid Input"); return; } if (ss == se) { // update value in array and in // segment tree arr[index] = value; st[node] = value; } else { int mid = getMid(ss, se); if (index >= ss && index <= mid) updateValue(arr, st, ss, mid, index, value, 2 * node + 1); else updateValue(arr, st, mid + 1, se, index, value, 2 * node + 2); st[node] = Math.max(st[2 * node + 1], st[2 * node + 2]); } return; } static int getMid(int s, int e) { return s + (e - s) / 2; } static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static class coordinate { int x, y, sum; coordinate(int x, int y, int sum) { this.x = x; this.y = y; this.sum = sum; } } public static void QuadRoots(double a, double b, int c) { double d = b * b - 4.0 * a * c; if (d > 0.0) { r1 = (-b + Math.pow(d, 0.5)) / (2.0 * a); r2 = (-b - Math.pow(d, 0.5)) / (2.0 * a); //System.out.println("The roots are " + r1 + " and " + r2); } else if (d == 0.0) { r1 = -b / (2.0 * a); r2 = r1; //System.out.println("The root is " + r1); } else { System.out.println("Roots are not real."); } } public static int search(ArrayList<Integer> array, int num) { int low = 0; int high = array.size() - 1; while (low < high) { // low<high when we search for the index that needs to be filled in int mid = (low + high) / 2; if (array.get(mid) < num && array.get(mid + 1) >= num) { return mid + 1; } else if (array.get(mid) < num) { low = mid + 1; } else if (array.get(mid) >= num) { // these inequality signs are specifically catering for the first appearance of the number //2,3,3,3 // if we search for 3, we would get 1 instead of like 4 high = mid; } } return -1; } }
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 8
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
36783fe8e185c99a628b534247c9b615
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.*; public class InfiniteSet { static MScanner scanner = new MScanner(); static long mod = (int) (1e9 + 7); public static void main(String[] args) { int n = scanner.nextInt(); int p = scanner.nextInt(); int[] nums = new int[n]; for (int i = 0; i < n; ++i) { nums[i] = scanner.nextInt(); } System.out.println(solve(nums, n, p)); } //两种二进制操作分别对应位操作(x << 1 + 1,x << 2) static long solve(int[] nums, int n, int p) { Arrays.sort(nums); //不能由前面的数推出来的集合 Set<Integer> set = new HashSet<>(); for (int num: nums) { int temp = num; while (!set.contains(temp)) { if (temp == 0) { break; } if ((temp & 1) != 0) { temp = temp >> 1; } else if ((temp & 3) == 0) { temp = temp >> 2; } else { break; } } if (!set.contains(temp)) { set.add(num); } } // System.out.println(set); //斐波那契数列 long[] dp = new long[p + 32]; for (int num: set) { int b = 0; for (int i = 31; i >= 0; --i) { if (((1 << i) & num) != 0) { b = i; break; } } dp[b + 1] ++; } dp[2] = dp[2] + dp[1]; for (int i = 3; i <= p; ++i) { dp[i] = (dp[i] + dp[i - 1] + dp[i - 2]) % mod; } // System.out.println(Arrays.toString(dp)); long ret = 0; for (int i = 1; i <= p; ++ i) { ret = (ret + dp[i]) % mod; } return ret; } static class MScanner { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public 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 8
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
176613c9e9158f1e827a48b415802b21
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 { //--------------------------INPUT READER---------------------------------// static class fs { public BufferedReader br; StringTokenizer st = new StringTokenizer(""); public fs() { this(System.in); } public fs(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() { while (!st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } String ns() { return next(); } int[] na(long nn) { int n = (int) nn; int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = ni(); return a; } long[] nal(long nn) { int n = (int) nn; long[] l = new long[n]; for(int i = 0; i < n; i++) l[i] = nl(); return l; } } //-----------------------------------------------------------------------// //---------------------------PRINTER-------------------------------------// static class Printer { static PrintWriter w; public Printer() {this(System.out);} public Printer(OutputStream os) { w = new PrintWriter(os); } public void p(int i) {w.println(i);} public void p(long l) {w.println(l);} public void p(double d) {w.println(d);} public void p(String s) { w.println(s);} public void pr(int i) {w.print(i);} public void pr(long l) {w.print(l);} public void pr(double d) {w.print(d);} public void pr(String s) { w.print(s);} public void pl() {w.println();} public void close() {w.close();} } //-----------------------------------------------------------------------// //--------------------------VARIABLES------------------------------------// static fs sc = new fs(); static OutputStream outputStream = System.out; static Printer w = new Printer(outputStream); static long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE; static int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE; static long mod = 1000000007; //-----------------------------------------------------------------------// //--------------------------ADMIN_MODE-----------------------------------// private static void ADMIN_MODE() throws IOException { if (System.getProperty("ONLINE_JUDGE") == null) { w = new Printer(new FileOutputStream("output.txt")); sc = new fs(new FileInputStream("input.txt")); } } //-----------------------------------------------------------------------// //----------------------------START--------------------------------------// public static void main(String[] args) throws IOException { ADMIN_MODE(); // int t = sc.ni();while(t-->0) solve(); w.close(); } static void solve() throws IOException { int n = sc.ni(), p = sc.ni(); long[] dp = new long[p+10]; dp[0] = 1; dp[1] = 1; for(int i = 2; i < dp.length; i++) { dp[i] = dp[i-1]+dp[i-2]; dp[i] %= mod; } for(int i = 1; i < dp.length; i++) { dp[i] += dp[i-1]; dp[i] %= mod; } long[] arr = sc.nal(n); long ans = 0; HashSet<String> hs = new HashSet<>(); for(long i: arr) { hs.add(Long.toString(i, 2)); } for(long i: arr) { String str = Long.toBinaryString(i); char[] strr = str.toCharArray(); int len = strr.length; boolean f = false; int[] chk = new int[len]; int pzer = 0; for(int j = len-1; j >= 0; j--) { if(strr[j]=='1' && pzer%2==1) { chk[j] = -2; break; } if(pzer%2==1) { chk[j] = -1; } if(strr[j]=='0') pzer++; else pzer = 0; } for(int j = len-2; j >= 0; j--) { if(chk[j] == -2) break; if(chk[j] == 0) { if(hs.contains(str.substring(0, j+1))) { f = true; break; } } } if(!f) { int rem = p-len; if(rem < 0) continue; ans+=dp[rem]; ans%=mod; } } w.p(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 8
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
2ee041f990c718f0c884aa648dcb2406
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.Arrays; import java.util.HashSet; import java.util.StringTokenizer; public class Main { static AReader scan = new AReader(); static int MOD = (int)1e9+7; static void slove() { int n = scan.nextInt(); // 二进制表示最多有p位 int p = scan.nextInt(); int[] arr = new int[n]; for(int i = 0;i<n;i++) arr[i] = scan.nextInt(); Arrays.sort(arr); HashSet<Integer> hash = new HashSet<>(); for(int x:arr){ boolean flag = true; int t = x; while(t > 0){ if(hash.contains(t)){ flag = false; break; } if(t % 4 == 0){ t >>=2; continue; } if((t & 1) == 1){ t >>=1; continue; } break; } if(flag){ hash.add(x); } } long[] dp = new long[p+40]; for(int x:hash){ dp[32 - Integer.numberOfLeadingZeros(x)]++; } long ans = 0; for(int i = 1;i<=p;i++){ dp[i] = (dp[i] + dp[i-1]) % MOD; if(i - 2 > 0) dp[i] = (dp[i] + dp[i-2]) % MOD; ans = (ans + dp[i]) % MOD; } System.out.println(ans); } public static void main(String[] args) { //int T = scan.nextInt(); //while (T-- > 0) { slove(); //} } } class AReader { private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer tokenizer = new StringTokenizer(""); private String innerNextLine() { try { return reader.readLine(); } catch (IOException ex) { return null; } } public boolean hasNext() { while (!tokenizer.hasMoreTokens()) { String nextLine = innerNextLine(); if (nextLine == null) { return false; } tokenizer = new StringTokenizer(nextLine); } return true; } public String nextLine() { tokenizer = new StringTokenizer(""); return innerNextLine(); } public String next() { hasNext(); return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } } class Pair { int x; int y; public Pair(int x, int y) { this.x = x; this.y = y; } }
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 8
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
29246726b1a4f832d7a02e08a287f8f2
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 AACFCC { public static long mod = (long) Math.pow(10, 9) + 7; public static long mod2 = 998244353; public static int oo = 0; public static ArrayList<Integer> prime; public static int zz = -1; public static int ttt = 0; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); ArrayList<Integer> list = new ArrayList<>(); int t = 1; // Integer.parseInt(br.readLine()); while (t-- > 0) { String[] str = br.readLine().split(" "); int n = (int) Long.parseLong(str[0]); int p = (int) Long.parseLong(str[1]); int[] arr = new int[n]; String[] s1 = br.readLine().split(" "); for (int i = 0; i < n; i++) { arr[i] = Integer.valueOf(s1[i]); } long[] dp = new long[p]; dp[0] = 1; for (int i = 0; i < p; i++) { if (i < p - 1) { dp[i + 1] = (dp[i] + dp[i + 1]) % mod; } if (i < p - 2) { dp[i + 2] = (dp[i] + dp[i + 2]) % mod; } } for (int i = 1; i < p; i++) { dp[i] = (dp[i] + dp[i - 1]) % mod; } Arrays.sort(arr); // System.out.println(); // ArrayList<Integer> uni=new ArrayList<>(); HashMap<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) { int pre = arr[i]; // if (arr[i] % 2 == 0) { int c=0; while (arr[i] % 4 == 0 || (arr[i]>1&&arr[i] % 2 != 0)) { if (arr[i] % 2 != 0) { arr[i] /= 2; } else { arr[i] /= 4; } if(map.containsKey(arr[i])) { c=1; break; } } if(map.containsKey(pre)) { c=1; } // if (arr[i] % 2 == 0) { if (c==0) { map.put(pre, pre); } else { // map.put(arr[i], Math.min(map.get(arr[i]), pre)); } // } else { // } } long ans = 0; // System.out.println(map); for (int a : map.keySet()) { long curr = map.get(a); long cc = 1; int pp = -1; while (cc <= curr) { pp++; cc = cc * 2; } // System.out.println(pp); if (p - 1 - pp < 0) { continue; } ans = (ans + dp[p - 1 - pp]) % mod; } pw.println(ans); } pw.close(); } } // private static void putBit(int ind, int val, int[] bit) { // // TODO Auto-generated method stub // for (int i = ind; i < bit.length; i += (i & -i)) { // bit[i] += val; // } // } // // private static int getSum(int ind, int[] bit) { // // TODO Auto-generated method stub // int ans = 0; // for (int i = ind; i > 0; i -= (i & -i)) { // ans += bit[i]; // } // return ans; // } // private static void product(long[] bin, int ind, int currIt, long[] power) { // // TODO Auto-generated method stub // long pre = 1; // if (ind > 1) { // pre = power(power[ind - 1] - 1, mod2 - 1); // } // long cc = power[ind] - 1; // // System.out.println(pre + " " + cc); // for (int i = ind; i < bin.length; i += (i & (-i))) { // bin[i] = (bin[i] * pre) % mod2; // bin[i] = (bin[i] * cc) % mod2; // } // } // // private static void add(long[] bin, int ind, int val) { // // TODO Auto-generated method stub // for (int i = ind; i < bin.length; i += (i & (-i))) { // bin[i] += val; // } // } // // private static long sum(long[] bin, int ind) { // // TODO Auto-generated method stub // long ans = 0; // for (int i = ind; i > 0; i -= (i & (-i))) { // ans += bin[i]; // } // return ans; // } // // 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 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; // } // } // private static int kmp(String str) { // // TODO Auto-generated method stub // // System.out.println(str); // int[] pi = new int[str.length()]; // pi[0] = 0; // for (int i = 1; i < str.length(); i++) { // int j = pi[i - 1]; // while (j > 0 && str.charAt(i) != str.charAt(j)) { // j = pi[j - 1]; // } // if (str.charAt(j) == str.charAt(i)) { // j++; // } // pi[i] = j; // System.out.print(pi[i]); // } // System.out.println(); // return pi[str.length() - 1]; // } // 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
["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 8
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
5229eeea14ae20da3c8b7e4db7e40f42
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 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer st = new StreamTokenizer(br); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws Exception { final int MOD = 1000000000 + 7; final int MAX = 200005; int n = nextInt(), p = nextInt(), ans = 0; int[] A = new int[MAX]; int[] F = new int[MAX]; int[] prefix = new int[MAX]; F[0] = F[1] = 1; prefix[0] = 1; prefix[1] = 2; HashMap<Integer, Boolean> map = new HashMap<>(); for(int i = 2; i < MAX; i++) { F[i] = (F[i-1] + F[i-2]) % MOD; prefix[i] = (prefix[i-1] + F[i]) % MOD; } for(int i = 1; i <= n; i++) { A[i] = nextInt(); map.put(A[i], true); } for(int i = 1; i <= n; i++) { int x = A[i]; while(x != 0) { if(x % 2 == 1) x >>= 1; else if(x % 4 == 0) x >>= 2; else break; if(map.containsKey(x)) { map.remove(A[i]); break; } } } Set<Integer> set = map.keySet(); for(Integer x : set) { int bit = 32 - Integer.numberOfLeadingZeros(x); if(bit > p) continue; ans += prefix[p - bit]; ans %= MOD; } System.out.println(ans); } public static double log2(double N) { return Math.log(N) / Math.log(2); } public static int nextInt() throws Exception { st.nextToken(); return (int) st.nval; } public static String nextStr() throws Exception { st.nextToken(); return st.sval; } }
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 8
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
8c884cfc15b5a523725548a90549ae5c
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.math.BigInteger; import java.util.*; import java.io.*; public class Main { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer st = new StreamTokenizer(br); static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static void main(String[] args) throws Exception { final int MOD = 1000000000 + 7; final int MAX = 200005; int n = nextInt(), p = nextInt(), ans = 0; int[] A = new int[MAX]; int[] F = new int[MAX]; int[] prefix = new int[MAX]; F[1] = F[2] = 1; HashMap<Integer, Boolean> map = new HashMap<>(); for(int i = 3; i < MAX; i++) F[i] = (F[i-1] + F[i-2]) % MOD; for(int i = 1; i < MAX; i++) prefix[i] = (prefix[i-1] + F[i]) % MOD; for(int i = 1; i <= n; i++) { A[i] = nextInt(); map.put(A[i], true); } for(int i = 1; i <= n; i++) { int x = A[i]; while(x != 0) { if(x % 2 == 1) x >>= 1; else if(x % 4 == 0) x >>= 2; else break; if(map.containsKey(x)) { map.remove(A[i]); break; } } } Set<Integer> set = map.keySet(); for(Integer x : set) { int bit = (int) log2(x); ans += prefix[Math.max(0, p - bit)]; ans %= MOD; } System.out.println(ans); } public static double log2(double N) { return Math.log(N) / Math.log(2); } public static int nextInt() throws Exception { st.nextToken(); return (int) st.nval; } public static String nextStr() throws Exception { st.nextToken(); return st.sval; } }
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 8
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
1864af9fb7420ced23bd5b4f29eacb94
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.*; import java.math.*; /** * @author Naitik * */ public class Main { static FastReader sc=new FastReader(); static long dp[]; // static boolean v[][][]; // static int mod=998244353;; static int mod=1000000007; static int max; static int bit[]; //static long fact[]; // static long A[]; static HashMap<Integer,Integer> map; //static StringBuffer sb=new StringBuffer(""); //static HashMap<Integer,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(); int m=i(); int A[]=input(n); dp=new long[m+1]; fill(dp); go(1, m); sort(A); Queue<Long> q=new LinkedList<>(); HashMap<Integer,Integer> map=hash(A); reverse(A); for(int i=0;i<n;i++) { int k=A[i]; while(k>0) { if(k!=A[i] && map.containsKey(k)) { map.remove(A[i]); break; } if(k%4==0) k/=4; else { if(k%2==0) break; k/=2; } } } long ans=0; for(int i=0;i<n;i++) { if(!map.containsKey(A[i])) continue; int l=Integer.toBinaryString(A[i]).length(); if(l<=m) ans+=dp[l]; ans%=mod; } System.out.println(ans); } //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(int n,int m) { if(n>m) return 0; if(dp[n]!=-1) return dp[n]; // System.out.println(n); long op1=go(n+1, m)%mod; long op2=go(n+2, m)%mod; dp[n]= (1+op1+op2)%mod; go(n+1, m); return dp[n]; } static class Pair implements Comparable<Pair> { int x; int y; int z; Pair(int 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]==a) 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(int v) { if(!map.containsKey(v)) { map.put(v, 1); } else { map.put(v, map.get(v)+1); } } static void remove(int 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 nextPowerOf2(int n) { if(n==0) return 1; n--; n |= n >> 1; n |= n >> 2; n |= n >> 4; n |= n >> 8; n |= n >> 16; n++; return n; } static int highestPowerof2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } static long highestPowerof2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } 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) { out.print(i+" "); } 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
["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 8
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
5e91e5fe5bef87eebfa0a0c5eab683d4
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
/* Setting up my ambitions Check 'em one at a time, yeah, I made it Now it's time for ignition I'm the start, heat it up They follow, burning up a chain reaction, oh-oh-oh Go fire it up, oh, oh, no Assemble the crowd now, now Line 'em up on the ground No leaving anyone behind */ import static java.lang.Math.*; import java.util.*; import java.io.*; import java.math.*; public class x1635D { static final long MOD = 1000000007L; public static void main(String hi[]) throws Exception { FastScanner infile = new FastScanner(); int N = infile.nextInt(); int P = infile.nextInt(); int[] arr = infile.nextInts(N); long[] dp = new long[P+3]; for(int k=P-1; k >= 0; k--) { dp[k] = dp[k+1]+dp[k+2]; if(dp[k] >= MOD) dp[k] -= MOD; dp[k]++; if(dp[k] >= MOD) dp[k] -= MOD; } int min = arr[0]; HashSet<Integer> set = new HashSet<Integer>(); for(int x: arr) { min = min(min, x); set.add(x); } boolean[] add = new boolean[N]; Arrays.fill(add, true); for(int i=0; i < N; i++) { if(!hasPar(arr[i])) continue; int val = par(arr[i]); while(val >= min) { if(set.contains(val)) { add[i] = false; break; } if(!hasPar(val)) break; val = par(val); } } long res = 0L; for(int i=0; i < N; i++) if(add[i]) { int bit = 0; for(int b=29; b >= 0; b--) if((arr[i]&(1<<b)) > 0) { bit = b; break; } if(bit < P) { res += dp[bit]; if(res >= MOD) res -= MOD; } } System.out.println(res); } public static boolean hasPar(int x) { if(x == 1) return false; if((x&1) == 1) return true; return (x&3) == 0; } public static int par(int x) { if((x&1) == 1) return x/2; return x/4; } } /* if x != y, then descendents of x will not overlap with descendents of y we only care about the msb, so do dp on that how to determine if one element in the array can reach another element in the array? for each element, we can get the move that was just performed dp[k] = contribution to answer if msb=2^k if(k >= P) dp[k] = 0; else dp[k] = 1+dp[k+1]+dp[k+2] */ class FastScanner { //I don't understand how this works lmao private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
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 8
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
9d21cf1397a5a2e86216dd0bf98dbe52
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
// OM NAMAH SHIVAY // 2 days remaining(2609) import java.math.BigInteger; import java.util.*; import java.io.*; public class Vaibhav { static boolean prime[]; static class Pair implements Comparable<Pair> { int a; int b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair o) { return (int)(this.a - o.a); //else return this.b - o.b; } } static long power(long x, long y, long p) { if (y == 0) return 1; if (x == 0) return 0; long res = 1l; x = x % p; while (y > 0) { if (y % 2 == 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } 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 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[] readlongArray(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()); } } 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] == true) { for (int i = p * p; i <= n; i += p) prime[i] = false; } } } public static int log2(int x) { return (int) (Math.log(x) / Math.log(2)); } static long binomialCoeff(long n, long r) { if (r > n) return 0l; long m = 998244353l; long inv[] = new long[(int) r + 1]; inv[0] = 1; if (r + 1 >= 2) inv[1] = 1; for (int i = 2; i <= r; i++) { inv[i] = m - (m / i) * inv[(int) (m % i)] % m; } long ans = 1l; for (int i = 2; i <= r; i++) { ans = (int) (((ans % m) * (inv[i] % m)) % m); } for (int i = (int) n; i >= (n - r + 1); i--) { ans = (int) (((ans % m) * (i % m)) % m); } return ans; } public static long gcd(long a, long b) { if (b == 0) { return a; } return gcd(b, a % b); } static BigInteger bi(String str) { return new BigInteger(str); } // Author - vaibhav_1710 static FastScanner fs = null; static long mod = 1_000_000_007; static int[][] dirs = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}, }; static long findTileCount(int x, int y, int l, int r) { long lcm = (x * y) / gcd(x, y); // Number multiple of lcm less than L long countl = (l - 1) / lcm; // Number of multiples of // lcm less than R+1 long countr = r / lcm; return countr - countl; } static long dp[]; public static void main(String[] args) { fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t =1; outer: while (t-- > 0){ int n = fs.nextInt(); int p = fs.nextInt(); long a[] = fs.readlongArray(n); long mod = 1_000_000_007; dp = new long[p+1]; dp[0]=1; dp[1]=1; for(int i=2;i<=p;i++){ dp[i] = (dp[i-1]+dp[i-2])%mod; } for(int i=1;i<=p;i++){ dp[i] = (dp[i]+dp[i-1])%mod; } HashSet<Long> h = new HashSet<>(); sort(a); long ans=0l; for(int i=0;i<n;i++){ if(parent(a[i],h)){ continue; }else{ h.add(a[i]); ans = (ans+wer(a[i],p))%mod; } } out.println(ans); } out.close(); } public static boolean parent(long x,HashSet<Long> h){ while(x>0) { if(h.contains(x)) return true; if (x % 2 == 1) { x = x >> 1; } else if (x % 4 > 0) { break; } else { x = x >> 2; } } return false; } public static long wer(long x,int p){ long tmp=x; int len=0; while(x>0){ len++; x /= 2; } if(len>p) return 0l; p -= len; return dp[p]; } }
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 8
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
90c38f9373ae5222586e11f6de6e8fd1
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.HashMap; import java.util.HashSet; import java.util.Objects; import java.util.Random; import java.util.Stack; import java.util.StringTokenizer; import java.util.TreeMap; // CFPS -> CodeForcesProblemSet public final class CFPS { static FastReader fr = new FastReader(); static PrintWriter out = new PrintWriter(System.out); static final int gigamod = 1000000007; static int t = 1; static double epsilon = 0.00000001; static boolean[] isPrime; static int[] smallestFactorOf; @SuppressWarnings("unused") public static void main(String[] args) throws Exception { OUTER: for (int tc = 0; tc < t; tc++) { int n = fr.nextInt(), p = fr.nextInt(); long[] arr = fr.nextLongArray(n); // Observations: // 1. The task is to determine the number of elements in the set that have atmost // 'p' bits. // 2. x = 2y + 1 -->> bitString += "1" // x = 4y -->> bitString += "00" // 3. There will be some elements in arr[] that can be derived out of some other. // These have to be removed. // 4. If some element is derivative, we know that removing all "1"s until possible // and all "00"s until possible will yield the root element along the process. // 5. We will carry out the process for all elements. If the element is a derivative, // we will mark it to be so. boolean[] isDerivative = new boolean[n]; HashSet<Long> hs = new HashSet<>(); for (long l : arr) hs.add(l); REDUCTION: for (int i = 0; i < n; i++) { String bitString = Long.toString(arr[i], 2); int len = bitString.length(); while (len > 1) { if (bitString.charAt(len - 1) == '1') { bitString = bitString.substring(0, len - 1); long val = Long.parseLong(bitString, 2); if (hs.contains(val)) { isDerivative[i] = true; continue REDUCTION; } } else if (len > 1 && bitString.charAt(len - 1) == '0' && bitString.charAt(len - 2) == '0') { bitString = bitString.substring(0, len - 2); long val = Long.parseLong(bitString, 2); if (hs.contains(val)) { isDerivative[i] = true; continue REDUCTION; } } else break; len = bitString.length(); } } // 6. Derivatives of two root values will never coincide. // PROOF: Suppose derivatives coincide. // This means that appending a bunch of "00"s and "1"s // to one string resulted in same after doing it on other. // This implies that one string could have been made out of // the other using append operation. This is a contradiction. // 7. All root values will contribute to the answer. This number will // be equal to the number of derivatives of the root value such that // the number is atmost 'p' bits. // 8. We need to be able to answer the query that goes: // "How many 'k' bit numbers can be formed using "00"s and "1"s arbitrarily?" long[][] dp = new long[Math.max(10, p + 1)][2]; // State: (i, j) // i -- number of bits // j -- ending character // Value: dp[i][j] -- number of strings of 'i' bits ending in 'j' // Base case: // dp[1][1] = 1 // dp[2][0] = 1 // dp[2][1] = 1 dp[2][0] = dp[1][1] = dp[2][1] = 1; for (int i = 3; i < p + 1; i++) { // if the number ends in 0, the ending characters are "00" dp[i][0] = dp[i - 2][0] + dp[i - 2][1]; dp[i][1] = dp[i - 1][0] + dp[i - 1][1]; dp[i][0] %= gigamod; dp[i][1] %= gigamod; } long[] remUpto = new long[p + 1]; remUpto[0] = dp[0][0] + dp[0][1]; for (int i = 1; i < p + 1; i++) { remUpto[i] = remUpto[i - 1] + dp[i][0] + dp[i][1]; remUpto[i] %= gigamod; } long answer = 0; for (int i = 0; i < n; i++) { if (isDerivative[i]) continue; int remBits = p - Long.toString(arr[i], 2).length(); if (remBits < 0) continue; answer += remUpto[remBits] + 1; answer %= gigamod; } out.println(answer); } out.close(); } public static long[][] sparseTable(long[] a) { int n = a.length; int b = 32-Integer.numberOfLeadingZeros(n); long[][] ret = new long[b][]; for(int i = 0, l = 1;i < b;i++, l*=2) { if(i == 0) { ret[i] = a; }else { ret[i] = new long[n-l+1]; for(int j = 0;j < n-l+1;j++) { ret[i][j] = Math.min(ret[i-1][j], ret[i-1][j+l/2]); } } } return ret; } // [a,b) public static long sparseRMQ(long[][] table, int l, int r) { assert l <= r; if(l >= r)return Integer.MAX_VALUE; // 1:0, 2:1, 3:1, 4:2, 5:2, 6:2, 7:2, 8:3 int t = 31-Integer.numberOfLeadingZeros(r-l); return Math.min(table[t][l], table[t][r-(1<<t)]); } static class Edge implements Comparable<Edge> { int from, to; long weight; int hash; Edge(int fro, int t, long weigh) { from = fro; to = t; weight = weigh; hash = Objects.hash(from, to, weight); } public int hashCode() { return hash; } public int compareTo(Edge that) { return Long.compare(weight, that.weight); } } static class Point implements Comparable<Point> { long x; long y; long z; long id; // private int hashCode; Point() { x = z = y = 0; // this.hashCode = Objects.hash(x, y, cost); } Point(Point p) { this.x = p.x; this.y = p.y; this.z = p.z; this.id = p.id; // this.hashCode = Objects.hash(x, y, cost); } Point(long x, long y, long z, long id) { this.x = x; this.y = y; this.z = z; this.id = id; // this.hashCode = Objects.hash(x, y, id); } Point(long a, long b) { this.x = a; this.y = b; this.z = 0; // this.hashCode = Objects.hash(a, b); } Point(long x, long y, long id) { this.x = x; this.y = y; this.id = id; } @Override public int compareTo(Point o) { if (this.x < o.x) return -1; if (this.x > o.x) return 1; if (this.y < o.y) return -1; if (this.y > o.y) return 1; if (this.z < o.z) return -1; if (this.z > o.z) return 1; return 0; } @Override public boolean equals(Object that) { return this.compareTo((Point) that) == 0; } /*@Override public int hashCode() { return this.hashCode; }*/ } static class BinaryLift { // FUNCTIONS: k-th ancestor and LCA in log(n) int[] parentOf; int maxJmpPow; int[][] binAncestorOf; int n; int[] lvlOf; // How this works? // a. For every node, we store the b-ancestor for b in {1, 2, 4, 8, .. log(n)}. // b. When we need k-ancestor, we represent 'k' in binary and for each set bit, we // lift level in the tree. public BinaryLift(UGraph tree) { n = tree.V(); maxJmpPow = logk(n, 2) + 1; parentOf = new int[n]; binAncestorOf = new int[n][maxJmpPow]; lvlOf = new int[n]; for (int i = 0; i < n; i++) Arrays.fill(binAncestorOf[i], -1); parentConstruct(0, -1, tree, 0); binConstruct(); } // TODO: Implement lvlOf[] initialization public BinaryLift(int[] parentOf) { this.parentOf = parentOf; n = parentOf.length; maxJmpPow = logk(n, 2) + 1; binAncestorOf = new int[n][maxJmpPow]; lvlOf = new int[n]; for (int i = 0; i < n; i++) Arrays.fill(binAncestorOf[i], -1); UGraph tree = new UGraph(n); for (int i = 1; i < n; i++) tree.addEdge(i, parentOf[i]); binConstruct(); parentConstruct(0, -1, tree, 0); } private void parentConstruct(int current, int from, UGraph tree, int depth) { parentOf[current] = from; lvlOf[current] = depth; for (int adj : tree.adj(current)) if (adj != from) parentConstruct(adj, current, tree, depth + 1); } private void binConstruct() { for (int node = 0; node < n; node++) for (int lvl = 0; lvl < maxJmpPow; lvl++) binConstruct(node, lvl); } private int binConstruct(int node, int lvl) { if (node < 0) return -1; if (lvl == 0) return binAncestorOf[node][lvl] = parentOf[node]; if (node == 0) return binAncestorOf[node][lvl] = -1; if (binAncestorOf[node][lvl] != -1) return binAncestorOf[node][lvl]; return binAncestorOf[node][lvl] = binConstruct(binConstruct(node, lvl - 1), lvl - 1); } // return ancestor which is 'k' levels above this one public int ancestor(int node, int k) { if (node < 0) return -1; if (node == 0) if (k == 0) return node; else return -1; if (k > (1 << maxJmpPow) - 1) return -1; if (k == 0) return node; int ancestor = node; int highestBit = Integer.highestOneBit(k); while (k > 0 && ancestor != -1) { ancestor = binAncestorOf[ancestor][logk(highestBit, 2)]; k -= highestBit; highestBit = Integer.highestOneBit(k); } return ancestor; } public int lca(int u, int v) { if (u == v) return u; // The invariant will be that 'u' is below 'v' initially. if (lvlOf[u] < lvlOf[v]) { int temp = u; u = v; v = temp; } // Equalizing the levels. u = ancestor(u, lvlOf[u] - lvlOf[v]); if (u == v) return u; // We will now raise level by largest fitting power of two until possible. for (int power = maxJmpPow - 1; power > -1; power--) if (binAncestorOf[u][power] != binAncestorOf[v][power]) { u = binAncestorOf[u][power]; v = binAncestorOf[v][power]; } return ancestor(u, 1); } } static class DFSTree { // NOTE: The thing is made keeping in mind that the whole // input graph is connected. UGraph tree; UGraph backUG; int hasBridge; int n; DFSTree(UGraph ug) { this.n = ug.V(); tree = new UGraph(n); hasBridge = -1; backUG = new UGraph(n); treeCalc(0, -1, new boolean[n], ug); } private void treeCalc(int current, int from, boolean[] marked, UGraph ug) { if (marked[current]) { // This is a backEdge. backUG.addEdge(from, current); return; } if (from != -1) tree.addEdge(from, current); marked[current] = true; for (int adj : ug.adj(current)) if (adj != from) treeCalc(adj, current, marked, ug); } public boolean hasBridge() { if (hasBridge != -1) return (hasBridge == 1); // We have to determine the bridge. bridgeFinder(); return (hasBridge == 1); } int[] levelOf; int[] dp; private void bridgeFinder() { // Finding the level of each node. levelOf = new int[n]; // Applying DP solution. // dp[i] -> Highest level reachable from subtree of 'i' using // some backEdge. dp = new int[n]; Arrays.fill(dp, Integer.MAX_VALUE / 100); dpDFS(0, -1, 0); // Now, we will check each edge and determine whether its a // bridge. for (int i = 0; i < n; i++) for (int adj : tree.adj(i)) { // (i -> adj) is the edge. if (dp[adj] > levelOf[i]) hasBridge = 1; } if (hasBridge != 1) hasBridge = 0; } private int dpDFS(int current, int from, int lvl) { levelOf[current] = lvl; dp[current] = levelOf[current]; for (int back : backUG.adj(current)) dp[current] = Math.min(dp[current], levelOf[back]); for (int adj : tree.adj(current)) if (adj != from) dp[current] = Math.min(dp[current], dpDFS(adj, current, lvl + 1)); return dp[current]; } } static class UnionFind { // Uses weighted quick-union with path compression. private int[] parent; // parent[i] = parent of i private int[] size; // size[i] = number of sites in tree rooted at i // Note: not necessarily correct if i is not a root node private int count; // number of components public UnionFind(int n) { count = n; parent = new int[n]; size = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; size[i] = 1; } } // Number of connected components. public int count() { return count; } // Find the root of p. public int find(int p) { while (p != parent[p]) p = parent[p]; return p; } public boolean connected(int p, int q) { return find(p) == find(q); } public int numConnectedTo(int node) { return size[find(node)]; } // Weighted union. public void union(int p, int q) { int rootP = find(p); int rootQ = find(q); if (rootP == rootQ) return; // make smaller root point to larger one if (size[rootP] < size[rootQ]) { parent[rootP] = rootQ; size[rootQ] += size[rootP]; } else { parent[rootQ] = rootP; size[rootP] += size[rootQ]; } count--; } public static int[] connectedComponents(UnionFind uf) { // We can do this in nlogn. int n = uf.size.length; int[] compoColors = new int[n]; for (int i = 0; i < n; i++) compoColors[i] = uf.find(i); HashMap<Integer, Integer> oldToNew = new HashMap<>(); int newCtr = 0; for (int i = 0; i < n; i++) { int thisOldColor = compoColors[i]; Integer thisNewColor = oldToNew.get(thisOldColor); if (thisNewColor == null) thisNewColor = newCtr++; oldToNew.put(thisOldColor, thisNewColor); compoColors[i] = thisNewColor; } return compoColors; } } static class UGraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public UGraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); adj[to].add(from); } public HashSet<Integer> adj(int from) { return adj[from]; } public int degree(int v) { return adj[v].size(); } public int V() { return adj.length; } public int E() { return E; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static void dfsMark(int current, boolean[] marked, UGraph g) { if (marked[current]) return; marked[current] = true; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, marked, g); } public static void dfsMark(int current, int from, long[] distTo, boolean[] marked, UGraph g, ArrayList<Integer> endPoints) { if (marked[current]) return; marked[current] = true; if (from != -1) distTo[current] = distTo[from] + 1; HashSet<Integer> adj = g.adj(current); int alreadyMarkedCtr = 0; for (int adjc : adj) { if (marked[adjc]) alreadyMarkedCtr++; dfsMark(adjc, current, distTo, marked, g, endPoints); } if (alreadyMarkedCtr == adj.size()) endPoints.add(current); } public static void bfsOrder(int current, UGraph g) { } public static void dfsMark(int current, int[] colorIds, int color, UGraph g) { if (colorIds[current] != -1) return; colorIds[current] = color; Iterable<Integer> adj = g.adj(current); for (int adjc : adj) dfsMark(adjc, colorIds, color, g); } public static int[] connectedComponents(UGraph g) { int n = g.V(); int[] componentId = new int[n]; Arrays.fill(componentId, -1); int colorCtr = 0; for (int i = 0; i < n; i++) { if (componentId[i] != -1) continue; dfsMark(i, componentId, colorCtr, g); colorCtr++; } return componentId; } public static boolean hasCycle(UGraph ug) { int n = ug.V(); boolean[] marked = new boolean[n]; boolean[] hasCycleFirst = new boolean[1]; for (int i = 0; i < n; i++) { if (marked[i]) continue; hcDfsMark(i, ug, marked, hasCycleFirst, -1); } return hasCycleFirst[0]; } // Helper for hasCycle. private static void hcDfsMark(int current, UGraph ug, boolean[] marked, boolean[] hasCycleFirst, int parent) { if (marked[current]) return; if (hasCycleFirst[0]) return; marked[current] = true; HashSet<Integer> adjc = ug.adj(current); for (int adj : adjc) { if (marked[adj] && adj != parent && parent != -1) { hasCycleFirst[0] = true; return; } hcDfsMark(adj, ug, marked, hasCycleFirst, current); } } } static class Digraph { // Adjacency list. private HashSet<Integer>[] adj; private static final String NEWLINE = "\n"; private int E; @SuppressWarnings("unchecked") public Digraph(int V) { adj = (HashSet<Integer>[]) new HashSet[V]; E = 0; for (int i = 0; i < V; i++) adj[i] = new HashSet<Integer>(); } public void addEdge(int from, int to) { if (adj[from].contains(to)) return; E++; adj[from].add(to); } public HashSet<Integer> adj(int from) { return adj[from]; } public int V() { return adj.length; } public int E() { return E; } public Digraph reversed() { Digraph dg = new Digraph(V()); for (int i = 0; i < V(); i++) for (int adjVert : adj(i)) dg.addEdge(adjVert, i); return dg; } public String toString() { StringBuilder s = new StringBuilder(); s.append(V() + " vertices, " + E() + " edges " + NEWLINE); for (int v = 0; v < V(); v++) { s.append(v + ": "); for (int w : adj[v]) { s.append(w + " "); } s.append(NEWLINE); } return s.toString(); } public static int[] KosarajuSharirSCC(Digraph dg) { int[] id = new int[dg.V()]; Digraph reversed = dg.reversed(); // Gotta perform topological sort on this one to get the stack. Stack<Integer> revStack = Digraph.topologicalSort(reversed); // Initializing id and idCtr. id = new int[dg.V()]; int idCtr = -1; // Creating a 'marked' array. boolean[] marked = new boolean[dg.V()]; while (!revStack.isEmpty()) { int vertex = revStack.pop(); if (!marked[vertex]) sccDFS(dg, vertex, marked, ++idCtr, id); } return id; } private static void sccDFS(Digraph dg, int source, boolean[] marked, int idCtr, int[] id) { marked[source] = true; id[source] = idCtr; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) sccDFS(dg, adjVertex, marked, idCtr, id); } public static Stack<Integer> topologicalSort(Digraph dg) { // dg has to be a directed acyclic graph. // We'll have to run dfs on the digraph and push the deepest nodes on stack first. // We'll need a Stack<Integer> and a int[] marked. Stack<Integer> topologicalStack = new Stack<Integer>(); boolean[] marked = new boolean[dg.V()]; // Calling dfs for (int i = 0; i < dg.V(); i++) if (!marked[i]) runDfs(dg, topologicalStack, marked, i); return topologicalStack; } static void runDfs(Digraph dg, Stack<Integer> topologicalStack, boolean[] marked, int source) { marked[source] = true; for (Integer adjVertex : dg.adj(source)) if (!marked[adjVertex]) runDfs(dg, topologicalStack, marked, adjVertex); topologicalStack.add(source); } } static class FastReader { private BufferedReader bfr; private StringTokenizer st; public FastReader() { bfr = new BufferedReader(new InputStreamReader(System.in)); } String next() { if (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(bfr.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()); } char nextChar() { return next().toCharArray()[0]; } String nextString() { return next(); } int[] nextIntArray(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = nextInt(); return arr; } double[] nextDoubleArray(int n) { double[] arr = new double[n]; for (int i = 0; i < arr.length; i++) arr[i] = nextDouble(); return arr; } long[] nextLongArray(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = nextLong(); return arr; } int[][] nextIntGrid(int n, int m) { int[][] grid = new int[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) grid[i][j] = fr.nextInt(); } return grid; } } static class LCA { int[] height, first, segtree; ArrayList<Integer> euler; boolean[] visited; int n; LCA(UGraph ug, int root) { n = ug.V(); height = new int[n]; first = new int[n]; euler = new ArrayList<>(); visited = new boolean[n]; dfs(ug, root, 0); int m = euler.size(); segtree = new int[m * 4]; build(1, 0, m - 1); } void dfs(UGraph ug, int node, int h) { visited[node] = true; height[node] = h; first[node] = euler.size(); euler.add(node); for (int adj : ug.adj(node)) { if (!visited[adj]) { dfs(ug, adj, h + 1); euler.add(node); } } } void build(int node, int b, int e) { if (b == e) { segtree[node] = euler.get(b); } else { int mid = (b + e) / 2; build(node << 1, b, mid); build(node << 1 | 1, mid + 1, e); int l = segtree[node << 1], r = segtree[node << 1 | 1]; segtree[node] = (height[l] < height[r]) ? l : r; } } int query(int node, int b, int e, int L, int R) { if (b > R || e < L) return -1; if (b >= L && e <= R) return segtree[node]; int mid = (b + e) >> 1; int left = query(node << 1, b, mid, L, R); int right = query(node << 1 | 1, mid + 1, e, L, R); if (left == -1) return right; if (right == -1) return left; return height[left] < height[right] ? left : right; } int lca(int u, int v) { int left = first[u], right = first[v]; if (left > right) { int temp = left; left = right; right = temp; } return query(1, 0, euler.size() - 1, left, right); } } static class FenwickTree { long[] array; // 1-indexed array, In this array We save cumulative information to perform efficient range queries and updates public FenwickTree(int size) { array = new long[size + 1]; } public long rsq(int ind) { assert ind > 0; long sum = 0; while (ind > 0) { sum += array[ind]; //Extracting the portion up to the first significant one of the binary representation of 'ind' and decrementing ind by that number ind -= ind & (-ind); } return sum; } public long rsq(int a, int b) { assert b >= a && a > 0 && b > 0; return rsq(b) - rsq(a - 1); } public void update(int ind, long value) { assert ind > 0; while (ind < array.length) { array[ind] += value; //Extracting the portion up to the first significant one of the binary representation of 'ind' and incrementing ind by that number ind += ind & (-ind); } } public int size() { return array.length - 1; } } static class SegmentTree { private Node[] heap; private long[] array; private int size; public SegmentTree(long[] array) { this.array = Arrays.copyOf(array, array.length); //The max size of this array is about 2 * 2 ^ log2(n) + 1 size = (int) (2 * Math.pow(2.0, Math.floor((Math.log((double) array.length) / Math.log(2.0)) + 1))); heap = new Node[size]; build(1, 0, array.length); } public int size() { return array.length; } //Initialize the Nodes of the Segment tree private void build(int v, int from, int size) { heap[v] = new Node(); heap[v].from = from; heap[v].to = from + size - 1; if (size == 1) { heap[v].sum = array[from]; heap[v].min = array[from]; heap[v].max = array[from]; } else { //Build childs build(2 * v, from, size / 2); build(2 * v + 1, from + size / 2, size - size / 2); heap[v].sum = heap[2 * v].sum + heap[2 * v + 1].sum; //min = min of the children heap[v].min = Math.min(heap[2 * v].min, heap[2 * v + 1].min); heap[v].max = Math.max(heap[2 * v].max, heap[2 * v + 1].max); } } public long rsq(int from, int to) { return rsq(1, from, to); } private long rsq(int v, int from, int to) { Node n = heap[v]; //If you did a range update that contained this node, you can infer the Sum without going down the tree if (n.pendingVal != null && contains(n.from, n.to, from, to)) { return (to - from + 1) * n.pendingVal; } if (contains(from, to, n.from, n.to)) { return heap[v].sum; } if (intersects(from, to, n.from, n.to)) { propagate(v); long leftSum = rsq(2 * v, from, to); long rightSum = rsq(2 * v + 1, from, to); return leftSum + rightSum; } return 0; } public long rMinQ(int from, int to) { return rMinQ(1, from, to); } private long rMinQ(int v, int from, int to) { Node n = heap[v]; //If you did a range update that contained this node, you can infer the Min value without going down the tree if (n.pendingVal != null && contains(n.from, n.to, from, to)) { return n.pendingVal; } if (contains(from, to, n.from, n.to)) { return heap[v].min; } if (intersects(from, to, n.from, n.to)) { propagate(v); long leftMin = rMinQ(2 * v, from, to); long rightMin = rMinQ(2 * v + 1, from, to); return Math.min(leftMin, rightMin); } return Integer.MAX_VALUE; } public long rMaxQ(int from, int to) { return rMaxQ(1, from, to); } private long rMaxQ(int v, int from, int to) { Node n = heap[v]; //If you did a range update that contained this node, you can infer the Min value without going down the tree if (n.pendingVal != null && contains(n.from, n.to, from, to)) { return n.pendingVal; } if (contains(from, to, n.from, n.to)) { return heap[v].max; } if (intersects(from, to, n.from, n.to)) { propagate(v); long leftMax = rMaxQ(2 * v, from, to); long rightMax = rMaxQ(2 * v + 1, from, to); return Math.max(leftMax, rightMax); } return Integer.MIN_VALUE; } public void update(int from, int to, long value) { update(1, from, to, value); } private void update(int v, int from, int to, long value) { //The Node of the heap tree represents a range of the array with bounds: [n.from, n.to] Node n = heap[v]; if (contains(from, to, n.from, n.to)) { change(n, value); } if (n.size() == 1) return; if (intersects(from, to, n.from, n.to)) { propagate(v); update(2 * v, from, to, value); update(2 * v + 1, from, to, value); n.sum = heap[2 * v].sum + heap[2 * v + 1].sum; n.min = Math.min(heap[2 * v].min, heap[2 * v + 1].min); n.max = Math.max(heap[2 * v].max, heap[2 * v + 1].max); } } //Propagate temporal values to children private void propagate(int v) { Node n = heap[v]; if (n.pendingVal != null) { change(heap[2 * v], n.pendingVal); change(heap[2 * v + 1], n.pendingVal); n.pendingVal = null; //unset the pending propagation value } } //Save the temporal values that will be propagated lazily private void change(Node n, long value) { n.pendingVal = value; n.sum = n.size() * value; n.min = value; n.max = value; array[n.from] = value; } //Test if the range1 contains range2 private boolean contains(int from1, int to1, int from2, int to2) { return from2 >= from1 && to2 <= to1; } //check inclusive intersection, test if range1[from1, to1] intersects range2[from2, to2] private boolean intersects(int from1, int to1, int from2, int to2) { return from1 <= from2 && to1 >= from2 // (.[..)..] or (.[...]..) || from1 >= from2 && from1 <= to2; // [.(..]..) or [..(..).. } //The Node class represents a partition range of the array. static class Node { long sum; long min; long max; //Here we store the value that will be propagated lazily Long pendingVal = null; int from; int to; int size() { return to - from + 1; } } } @SuppressWarnings("serial") static class CountMap<T> extends TreeMap<T, Integer>{ CountMap() { } CountMap(T[] arr) { this.putCM(arr); } public Integer putCM(T key) { return super.put(key, super.getOrDefault(key, 0) + 1); } public Integer removeCM(T key) { int count = super.getOrDefault(key, -1); if (count == -1) return -1; if (count == 1) return super.remove(key); else return super.put(key, count - 1); } public Integer getCM(T key) { return super.getOrDefault(key, 0); } public void putCM(T[] arr) { for (T l : arr) this.putCM(l); } } static long dioGCD(long a, long b, long[] x0, long[] y0) { if (b == 0) { x0[0] = 1; y0[0] = 0; return a; } long[] x1 = new long[1], y1 = new long[1]; long d = dioGCD(b, a % b, x1, y1); x0[0] = y1[0]; y0[0] = x1[0] - y1[0] * (a / b); return d; } static boolean diophantine(long a, long b, long c, long[] x0, long[] y0, long[] g) { g[0] = dioGCD(Math.abs(a), Math.abs(b), x0, y0); if (c % g[0] > 0) { return false; } x0[0] *= c / g[0]; y0[0] *= c / g[0]; if (a < 0) x0[0] = -x0[0]; if (b < 0) y0[0] = -y0[0]; return true; } static long[][] prod(long[][] mat1, long[][] mat2) { int n = mat1.length; long[][] prod = new long[n][n]; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) // determining prod[i][j] // it will be the dot product of mat1[i][] and mat2[][i] for (int k = 0; k < n; k++) prod[i][j] += mat1[i][k] * mat2[k][j]; return prod; } static long[][] matExpo(long[][] mat, long power) { int n = mat.length; long[][] ans = new long[n][n]; if (power == 0) return null; if (power == 1) return mat; long[][] half = matExpo(mat, power / 2); ans = prod(half, half); if (power % 2 == 1) { ans = prod(ans, mat); } return ans; } static int KMPNumOcc(char[] text, char[] pat) { int n = text.length; int m = pat.length; char[] patPlusText = new char[n + m + 1]; for (int i = 0; i < m; i++) patPlusText[i] = pat[i]; patPlusText[m] = '^'; // Seperator for (int i = 0; i < n; i++) patPlusText[m + i] = text[i]; int[] fullPi = piCalcKMP(patPlusText); int answer = 0; for (int i = 0; i < n + m + 1; i++) if (fullPi[i] == m) answer++; return answer; } static int[] piCalcKMP(char[] s) { int n = s.length; int[] pi = new int[n]; for (int i = 1; i < n; i++) { int j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; } static long hash(long key) { long h = Long.hashCode(key); h ^= (h >>> 20) ^ (h >>> 12) ^ (h >>> 7) ^ (h >>> 4); return h & (gigamod-1); } static int mapTo1D(int row, int col, int n, int m) { // Maps elements in a 2D matrix serially to elements in // a 1D array. return row * m + col; } static int[] mapTo2D(int idx, int n, int m) { // Inverse of what the one above does. int[] rnc = new int[2]; rnc[0] = idx / m; rnc[1] = idx % m; return rnc; } static boolean[] primeGenerator(int upto) { // Sieve of Eratosthenes: isPrime = new boolean[upto + 1]; smallestFactorOf = new int[upto + 1]; Arrays.fill(smallestFactorOf, 1); Arrays.fill(isPrime, true); isPrime[1] = isPrime[0] = false; for (long i = 2; i < upto + 1; i++) if (isPrime[(int) i]) { smallestFactorOf[(int) i] = (int) i; // Mark all the multiples greater than or equal // to the square of i to be false. for (long j = i; j * i < upto + 1; j++) { if (isPrime[(int) j * (int) i]) { isPrime[(int) j * (int) i] = false; smallestFactorOf[(int) j * (int) i] = (int) i; } } } return isPrime; } static HashMap<Integer, Integer> smolNumPrimeFactorization(int num) { if (smallestFactorOf == null) primeGenerator(num + 1); HashMap<Integer, Integer> fnps = new HashMap<>(); while (num != 1) { fnps.put(smallestFactorOf[num], fnps.getOrDefault(smallestFactorOf[num], 0) + 1); num /= smallestFactorOf[num]; } return fnps; } static HashMap<Long, Integer> primeFactorization(long num) { // Returns map of factor and its power in the number. HashMap<Long, Integer> map = new HashMap<>(); while (num % 2 == 0) { num /= 2; Integer pwrCnt = map.get(2L); map.put(2L, pwrCnt != null ? pwrCnt + 1 : 1); } for (long i = 3; i * i <= num; i += 2) { while (num % i == 0) { num /= i; Integer pwrCnt = map.get(i); map.put(i, pwrCnt != null ? pwrCnt + 1 : 1); } } // If the number is prime, we have to add it to the // map. if (num != 1) map.put(num, 1); return map; } static HashSet<Long> divisors(long num) { HashSet<Long> divisors = new HashSet<Long>(); divisors.add(1L); divisors.add(num); for (long i = 2; i * i <= num; i++) { if (num % i == 0) { divisors.add(num/i); divisors.add(i); } } return divisors; } static void coprimeGenerator(int m, int n, ArrayList<Point> coprimes, int limit, int numCoprimes) { if (m > limit) return; if (m <= limit && n <= limit) coprimes.add(new Point(m, n)); if (coprimes.size() > numCoprimes) return; coprimeGenerator(2 * m - n, m, coprimes, limit, numCoprimes); coprimeGenerator(2 * m + n, m, coprimes, limit, numCoprimes); coprimeGenerator(m + 2 * n, n, coprimes, limit, numCoprimes); } static int bsearch(int[] arr, int val, int lo, int hi) { // Returns the index of the first element // larger than or equal to val. int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi) { // Returns the index of the first element // larger than or equal to val. int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] >= val) { idx = mid; hi = mid - 1; } else lo = mid + 1; } return idx; } static int bsearch(long[] arr, long val, int lo, int hi, boolean sMode) { // Returns the index of the last element // smaller than or equal to val. int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static int bsearch(int[] arr, long val, int lo, int hi, boolean sMode) { int idx = -1; while (lo <= hi) { int mid = lo + (hi - lo)/2; if (arr[mid] > val) { hi = mid - 1; } else { idx = mid; lo = mid + 1; } } return idx; } static long nCr(long n, long r, long[] fac) { long p = gigamod; if (r == 0) return 1; return (fac[(int)n] * modInverse(fac[(int)r], p) % p * modInverse(fac[(int)n - (int)r], p) % p) % p; } static long modInverse(long n, long p) { return power(n, p - 2, p); } static long modDiv(long a, long b){return mod(a * power(b, gigamod - 2, gigamod));} static long power(long x, long y, long p) { long res = 1; x = x % p; while (y > 0) { if ((y & 1)==1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } static int logk(long n, long k) { return (int)(Math.log(n) / Math.log(k)); } static long gcd(long a, long b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static int gcd(int a, int b) { if (b == 0) { return a; } else { return gcd(b, a % b); } } static long gcd(long[] arr) { int n = arr.length; long gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static int gcd(int[] arr) { int n = arr.length; int gcd = arr[0]; for (int i = 1; i < n; i++) { gcd = gcd(gcd, arr[i]); } return gcd; } static long lcm(long[] arr) { long lcm = arr[0]; int n = arr.length; for (int i = 1; i < n; i++) { lcm = (lcm * arr[i]) / gcd(lcm, arr[i]); } return lcm; } static long lcm(long a, long b) { return (a * b)/gcd(a, b); } static boolean less(int a, int b) { return a < b ? true : false; } static boolean isSorted(int[] a) { for (int i = 1; i < a.length; i++) { if (less(a[i], a[i - 1])) return false; } return true; } static boolean isSorted(long[] a) { for (int i = 1; i < a.length; i++) { if (a[i] < a[i - 1]) return false; } return true; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(double[] a, int i, int j) { double temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(char[] a, int i, int j) { char temp = a[i]; a[i] = a[j]; a[j] = temp; } static void sort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void sort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); } static void reverseSort(int[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(char[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverse(char[] arr) { int n = arr.length; for (int i = 0; i < n / 2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(long[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void reverseSort(double[] arr) { shuffleArray(arr, 0, arr.length - 1); Arrays.sort(arr); int n = arr.length; for (int i = 0; i < n/2; i++) swap(arr, i, n - 1 - i); } static void shuffleArray(long[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { long tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(int[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(double[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { double tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } static void shuffleArray(char[] arr, int startPos, int endPos) { Random rnd = new Random(); for (int i = startPos; i < endPos; ++i) { char tmp = arr[i]; int randomPos = i + rnd.nextInt(endPos - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } } 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(long i=5;i*i<=n;i=i+6)if(n%i==0||n%(i+2)==0)return false;return true;} static String toString(int[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(boolean[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(long[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+" ");return sb.toString();} static String toString(char[] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++)sb.append(dp[i]+"");return sb.toString();} static String toString(int[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(long[][] dp){StringBuilder sb=new StringBuilder();for(int i=0;i<dp.length;i++){for(int j=0;j<dp[i].length;j++) {sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(double[][] dp){StringBuilder sb=new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+" ");}sb.append('\n');}return sb.toString();} static String toString(char[][] dp){StringBuilder sb = new StringBuilder();for(int i = 0;i<dp.length;i++){for(int j = 0;j<dp[i].length;j++){sb.append(dp[i][j]+"");}sb.append('\n');}return sb.toString();} static long mod(long a, long m){return(a%m+1000000L*m)%m;} static long mod(long num){return(num%gigamod+gigamod)%gigamod;} } // NOTES: // ASCII VALUE OF 'A': 65 // ASCII VALUE OF 'a': 97 // Range of long: 9 * 10^18 // ASCII VALUE OF '0': 48 // Primes upto 'n' can be given by (n / (logn)).
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 8
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
277bcb585a64292f843ce34fe65d78db
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 InfiniteSet { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pr = new PrintWriter(System.out); String[] ints = br.readLine().split(" "); int n = Integer.parseInt(ints[0]); int p = Integer.parseInt(ints[1]); ints = br.readLine().split(" "); HashSet<Integer> a = new HashSet<Integer>(); List<Integer> b = new ArrayList<>(); for(int i = 0;i<n;i++){ int temp = Integer.parseInt(ints[i]); b.add(temp); } List<Integer> c = new ArrayList<>(); b.sort(Comparator.naturalOrder()); a.add(b.get(0)); c.add(b.get(0)); for(int i = 1;i<n;i++){ int temp = b.get(i); boolean unique = true; while(temp!=0){ if (a.contains(temp)){ unique = false; break; } //duplicate element found if (temp%4 == 0){ temp/=4; } else if (temp%2 == 1){ temp = (temp -1 )/2; } else{ temp = 0; //unique element found. } } if (unique) { a.add(b.get(i)); c.add(b.get(i)); } else{ //element is aleady reachable, we do nothing } } int MODVAL = (1000000000 + 7); List<Long> dp = new ArrayList<>(); int pos = 0; int val = 1; for(int i = 0;i<30;i++){ Long count = 0L; val*=2; while (pos<c.size() && c.get(pos) < val){ pos++; count++; } Long temp = count; if (i > 0) temp+=dp.get(i-1); temp%=MODVAL; if (i > 1) temp+=dp.get(i-2); temp%=MODVAL; dp.add(temp); } while(dp.size()<p){ Long temp = dp.get(dp.size()-1); temp%=MODVAL; Long temp2 = dp.get(dp.size()-2); temp2%=MODVAL; temp2+=temp; temp2%=MODVAL; dp.add(temp2); } Long ans = 0l; for(int i = 0;i<p;i++){ ans+=dp.get(i); ans%=MODVAL; } pr.println(ans); pr.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 8
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
457f6d1e5e3fe1df90a9c0829728898a
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.*; // import java.lang.*; // import java.math.*; public class Main { 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 int seg[]; static int lazy[];//lazy propagation is used in case of range updates. // static int dp[][]; static long dp[]; static HashSet<Long> hs; public static void main (String[] args) throws java.lang.Exception { // code goes here // int t=I(); // outer:while(t-->0) // { int n=I(),p=I(); long a[]=IL(n); hs=new HashSet<>(); for(int i=0;i<n;i++){ hs.add(a[i]); } sort(a); dp=new long[p+1]; 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]=add(dp[i-1],dp[i]); else{ dp[i]=add(dp[i],dp[i-1]); dp[i]=add(dp[i],dp[i-2]); } } long ans=0; for(int i=1;i<=p;i++){ ans=add(ans,dp[i]); } out.println(ans); // } out.close(); } public static boolean fun(long a) { if(hs.contains(a))return false; if(a==0)return true; if(a%4==0)return fun(a/4); if(a%2==1)return fun(a/2); return true; } public static class pair { int a; int b; public pair(int aa,int bb) { a=aa; b=bb; } } 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 void DFS(int s,boolean visited[],int dis) { visited[s]=true; for(int i:graph[s]){ if(!visited[i]){ DFS(i,visited,dis); } } } 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); } } //LOWER_BOUND and UPPER_BOUND functions //It returns answer according to zero based indexing. public static int lower_bound(long arr[],long 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; } //It returns answer according to zero based indexing. public static int upper_bound(long arr[],long 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 // File file = new File("C:\\Users\\Dell\\Desktop\\JAVA\\testcase.txt"); // FileWriter fw = new FileWriter("output.txt"); // BufferedReader br= new BufferedReader(new FileReader(file)); //Segment Tree Code public static void buildTree(int si,int ss,int se) { if(ss==se){ seg[si]=0; return; } int mid=(ss+se)/2; buildTree(2*si+1,ss,mid); buildTree(2*si+2,mid+1,se); seg[si]=max(seg[2*si+1],seg[2*si+2]); } public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b) { int i=0,j=0; while(i<a.size() && j<b.size()){ if(a.get(i)<=b.get(j)){ f.add(a.get(i)); i++; }else{ f.add(b.get(j)); j++; } } while(i<a.size()){ f.add(a.get(i)); i++; } while(j<b.size()){ f.add(b.get(j)); j++; } } public static void update(int si,int ss,int se,int pos,int val) { if(ss==se){ seg[si]=(int)add(seg[si],val); return; } int mid=(ss+se)/2; if(pos<=mid){ update(2*si+1,ss,mid,pos,val); }else{ update(2*si+2,mid+1,se,pos,val); } seg[si]=(int)add(seg[2*si+1],seg[2*si+2]); } public static int query(int si,int ss,int se,int qs,int qe) { if(qs>se || qe<ss)return 0; if(ss>=qs && se<=qe)return seg[si]; int mid=(ss+se)/2; return max(query(2*si+1,ss,mid,qs,qe),query(2*si+2,mid+1,se,qs,qe)); } //Segment Tree Code end //Prefix Function of KMP Algorithm public static int[] KMP(char c[],int n) { int pi[]=new int[n]; for(int i=1;i<n;i++){ int j=pi[i-1]; while(j>0 && c[i]!=c[j]){ j=pi[j-1]; } if(c[i]==c[j])j++; pi[i]=j; } return pi; } 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 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 int np2(int x) { x--; x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; x++; return x; } public static int hp2(int x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } public static long hp2(long x) { x |= x >> 1; x |= x >> 2; x |= x >> 4; x |= x >> 8; x |= x >> 16; return x ^ (x >> 1); } 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 int get(int x) { int 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 HashMap<Integer,Integer> primeFact(int a) { // HashSet<Long> arr=new HashSet<>(); HashMap<Integer,Integer> hm=new HashMap<>(); int p=0; while(a%2==0){ // arr.add(2L); p++; a=a/2; } hm.put(2,hm.getOrDefault(2,0)+p); for(int i=3;i*i<=a;i+=2){ p=0; while(a%i==0){ // arr.add(i); p++; a=a/i; } hm.put(i,hm.getOrDefault(i,0)+p); } if(a>2){ // arr.add(a); hm.put(a,hm.getOrDefault(a,0)+1); } // return arr; return hm; } 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=s.length(); 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 printArray(ArrayList<?> arr) { for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); } out.println(); } public static void printMapI(HashMap<?,?> hm){ for(Map.Entry<?,?> e:hm.entrySet()){ out.println(e.getKey()+"->"+e.getValue()); }out.println(); } public static void printMap(HashMap<Long,ArrayList<Integer>> hm){ for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){ out.print(e.getKey()+"->"); ArrayList<Integer> arr=e.getValue(); for(int i=0;i<arr.size();i++){ out.print(arr.get(i)+" "); }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); } //max & min public static int max(int a,int b){return Math.max(a,b);} public static int min(int a,int b){return Math.min(a,b);} public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));} public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));} public static long max(long a,long b){return Math.max(a,b);} public static long min(long a,long b){return Math.min(a,b);} public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));} public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));} public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;} public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;} public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;} //end public static int[] I(int n) { int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=I(); } return a; } public static long[] IL(int n) { long a[]=new long[n]; for(int i=0;i<n;i++){ a[i]=L(); } return a; } public static long[] prefix(int a[]) { int n=a.length; long pre[]=new long[n]; pre[0]=a[0]; for(int i=1;i<n;i++){ pre[i]=pre[i-1]+a[i]; } return pre; } public static long[] prefix(long a[]) { int n=a.length; long pre[]=new long[n]; pre[0]=a[0]; for(int i=1;i<n;i++){ pre[i]=pre[i-1]+a[i]; } return pre; } 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
["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 8
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
d6e573c543962d7ef7364731b6fceb52
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 FastReader sc=null; static long mod=(long)1e9+7; public static void main(String[] args) { sc=new FastReader(); int n=sc.nextInt(),p=sc.nextInt(); int a[]=sc.readArray(n); long dp[]=new long[p+1],pre[]=new long[p+1]; dp[0]=dp[1]=1; pre[0]=1; pre[1]=2; for(int i=2;i<=p;i++) { dp[i]=add(dp[i-1],dp[i-2]); pre[i]=add(dp[i], pre[i-1]); } Arrays.sort(a); long ans=0; Set<Integer> done=new HashSet<>(); for(int i=0;i<n;i++) { int val=a[i]; boolean taken=false; while(val>0) { if(val%2==1)val/=2; else if(val%4==0)val/=4; else break; if(done.contains(val)) { taken=true; break; } } if(!taken) { done.add(a[i]); int bit=go(a[i]); int id=(p-bit-1); if(id<0)continue; ans=add(ans, pre[id]); } } System.out.println(ans); } static int go(int val) { int cur=1,cnt=-1; while(cur<=val) { cur*=2; cnt++; } return cnt; } static long add(long a,long b) { a+=b; if(a>=mod)a-=mod; return a; } static int[] ruffleSort(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 void print(int a[]) { for(int e:a) { System.out.print(e+" "); } System.out.println(); } static class FastReader{ StringTokenizer st=new StringTokenizer(""); BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); 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()); } int[] readArray(int n) { int a[]=new int[n]; for(int i=0;i<n;i++)a[i]=sc.nextInt(); 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 8
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
65a29ddab988d9e0dd0bf835b760fa5c
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 { // For fast input output static 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; } } // end of fast i/o code public static void main(String[] args) { FastReader reader = new FastReader(); StringBuilder sb = new StringBuilder(""); int n = reader.nextInt(); int p = reader.nextInt(); ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < n; i++) { arr.add(reader.nextInt()); } Collections.sort(arr); TreeSet<Integer> ts = new TreeSet<>(); for(int i=0; i<n; i++){ int curr = arr.get(i); boolean is_valid = true; while(curr > 0){ if(ts.contains(curr)){ is_valid = false; } if(curr%4==0){ curr = curr/4; }else if(curr%2==1){ curr = (curr-1)/2; }else{ break; } } if(is_valid){ ts.add(arr.get(i)); } } arr = new ArrayList<>(); for(int e: ts){ arr.add(e); } n = arr.size(); // System.out.println(arr.toString()); int mod = (int)(1e9+7); int ptr = 0; long dp[] = new long[p+1]; for(int i=0; i<=p; i++){ dp[i] = (dp[i] + (i-1>=0? dp[i-1]: 0))%mod; dp[i] = (dp[i] + (i-2>=0? dp[i-2]: 0))%mod; while(ptr<n && arr.get(ptr)<(1L<<(i+1)) ){ dp[i]++; ptr++; } dp[i] %= mod; } // System.out.println(Arrays.toString(dp)); long ans = 0; for(int i=0; i<p; i++){ ans = (ans + dp[i])%mod; } System.out.println(ans); // System.out.println(sb); } }
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 8
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
2f07d2b9f9bfb105021766dc882ef6dd
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.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc=new Scanner(System.in); long mod=1000000007; int n=sc.nextInt(),p=sc.nextInt(); long[] a=new long[n]; for(int i=0;i<n;i++) a[i]=sc.nextLong(); Arrays.sort(a); HashSet<Long> set=new HashSet<>(); for(int i=0;i<n;i++) { if(check(set,a[i])) set.add(a[i]); } long[] dp=new long[p+1]; for(long key:set) { int length=Long.toString(key,2).length(); if(length<=p) dp[length]++; } for(int i=1;i<=p;i++) { if(i+1<=p) dp[i+1]+=dp[i]; if(i+2<=p) dp[i+2]+=dp[i]; if(i+1<=p) dp[i+1]%=mod; if(i+2<=p) dp[i+2]%=mod; } long ans=0; for(int i=1;i<=p;i++) ans+=dp[i]; ans%=mod; System.out.println(ans); sc.close(); } private static boolean check(HashSet<Long> set,long n) { while(n>0 && !set.contains(n)) { if(n%4==0) { n/=4; continue; } else if(n%2!=0) { n/=2; continue; } break; } if(set.contains(n)) return false; return true; } }
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 8
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
c4da9bcd9146cb3461199dc4951493ca
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.*; //import java.math.BigInteger; public class code{ public static class Pair{ int a; int b; Pair(int i,int j){ a=i; b=j; } } public static int GCD(int a, int b) { if (b == 0) return a; return GCD(b, a % b); } public static void shuffle(int a[], int n) { for (int i = 0; i < n; i++) { // getting the random index int t = (int)Math.random() * a.length; // and swapping values a random index // with the current index int x = a[t]; a[t] = a[i]; a[i] = x; } } public static PrintWriter out = new PrintWriter(System.out); public static long mod=(long)(1e9+7); @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); FastScanner in=new FastScanner(); int t=1; while(t-- > 0){ int n=in.nextInt(); long p=in.nextLong()+1; long[] arr=new long[n]; Set<Long> set=new HashSet<Long>(); long res=0; for(int i=0;i<n;i++) arr[i]=in.nextLong(); Arrays.sort(arr); for(int i=0;i<n;i++){ if(check(set,arr[i])) continue; long k=(long)(Math.log(2*arr[i])/Math.log(2)); //out.println("1: "+arr[i]+" "+k); if(k>p) continue; if(k==p){ if(arr[i]==(long)Math.pow(2,p-1)){ res=(res+1)%mod; set.add(arr[i]); } else{ continue; } } else{ long val=calculateSum((int)(p-k)); //out.println("val[]: "+arr[i]+" "+(p-k)+" "+val); res=(res+val+mod)%mod; } //out.println(res); set.add(arr[i]); } // for(int i=1;i<10;i++){ // out.println(i+" "+calculateSum(i)); // } out.println(res); } out.flush(); } public static boolean check(Set<Long> set,long k){ while(k>0){ if(set.contains(k)) return true; if(k%2==0) { if(k%4==0) k/=4; else return false; } else{ k--; k/=2; } } return false; } public static long []f = new long[200005]; public static long calculateSum(int n) { return fib(n+2) - 1; } public static long fib(int n) { // Base cases if (n == 0) return 0; if (n == 1 || n == 2) return (f[n] = 1); // If fib(n) is already computed if (f[n]>0) return f[n]; int k = ((n & 1)>0)? (n+1)/2 : n/2; // Applying above formula [Note value n&1 is 1 // if n is odd, else 0]. f[n] = (n & 1)>0? ((fib(k)*fib(k))%mod + (fib(k-1)*fib(k-1))%mod)%mod : ((((2*fib(k-1))%mod + fib(k))%mod)*fib(k))%mod; return f[n]; } } class Fenwick{ int[] bit; public Fenwick(int n){ bit=new int[n]; //int sum=0; } public void update(int index,int val){ index++; for(;index<bit.length;index += index&(-index)) bit[index]+=val; } public int presum(int index){ int sum=0; for(;index>0;index-=index&(-index)) sum+=bit[index]; return sum; } public int sum(int l,int r){ return presum(r+1)-presum(l); } } class FastScanner { private int BS = 1 << 16; private char NC = (char) 0; private byte[] buf = new byte[BS]; private int bId = 0, size = 0; private char c = NC; private double cnt = 1; private BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } private char getChar() { while (bId == size) { try { size = in.read(buf); } catch (Exception e) { return NC; } if (size == -1) return NC; bId = 0; } return (char) buf[bId++]; } public int nextInt() { return (int) nextLong(); } public int[] nextInts(int N) { int[] res = new int[N]; for (int i = 0; i < N; i++) { res[i] = (int) nextLong(); } return res; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } public long nextLong() { cnt = 1; boolean neg = false; if (c == NC) c = getChar(); for (; (c < '0' || c > '9'); c = getChar()) { if (c == '-') neg = true; } long res = 0; for (; c >= '0' && c <= '9'; c = getChar()) { res = (res << 3) + (res << 1) + c - '0'; cnt *= 10; } return neg ? -res : res; } public double nextDouble() { double cur = nextLong(); return c != '.' ? cur : cur + nextLong() / cnt; } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } public String next() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c > 32) { res.append(c); c = getChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while (c <= 32) c = getChar(); while (c != '\n') { res.append(c); c = getChar(); } return res.toString(); } public boolean hasNext() { if (c > 32) return true; while (true) { c = getChar(); if (c == NC) return false; else if (c > 32) return true; } } }
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 8
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
2d2f86c7ddd4c2706330dfd0ec2aecf7
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 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=1; sb=new StringBuilder(2000000); o: while(tq-->0) { int n=i();int p=i(); long f[]=new long[p+1]; f[0]=f[1]=1l; long pp[]=new long[p+1]; for(int x=2;x<=p;x++)f[x]=(f[x-1]+f[x-2])%mod; pp[0]=1l; for(int x=1;x<=p;x++)pp[x]=(pp[x-1]+f[x])%mod; int ar[]=ari(n); so(ar); HashSet<Integer> h=new HashSet<>(); for(int x:ar) { int v=x; int c=0; while(v>0) { if(h.contains(v)) { c=1; break; } if(v%4==0)v/=4; else if((v-1)%2==0)v=(v-1)/2; else break; } if(c==0)h.add(x); } long c=0l; for(int e:h) { int v=l(e); if(p-v-1>=0)c=(c+pp[p-v-1])%mod; } sl(c); } p(sb); } 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 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}}; void f(){out.flush();} int p(int i,int p[]){return p[i]<0?i:(p[i]=p(p[i],p));} boolean c(int x,int y,int n,int m){return x>=0&&x<n&&y>=0&&y<m;} 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 p(Object p) {out.print(p);}void pl(Object p) {out.println(p);}void pl() {out.println();} 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) {s(s);sb.append("\n");} void sl(int s) {s(s);sb.append("\n");} void sl(long s) {s(s);sb.append("\n");} void sl(char s) {s(s);sb.append("\n");} void sl(double s) {s(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);} int gcd(int a, int b) { while (b > 0) { int c = a % b; a = b; b = c; } return a; } long gcd(long a, long b) { while (b > 0l) { long c = a % b; a = b; b = c; } return a; } boolean p(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] = 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 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(char a[]) { for (char e : a){sb.append(e);sb.append(' ');} sb.append("\n"); } void s(int ar[][]) {for (int a[] : ar) s(a);} void s(long ar[][]) {for (long a[] : ar) s(a);} void s(char ar[][]) {for (char a[] : ar) s(a);} 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; } 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; } 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; } 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; } 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; } int[][] ari(int n, int m) throws IOException { int ar[][] = new int[n][m]; for (int x = 0; x < n; x++)ar[x]=ari(m); return ar; } long[][] arl(int n, int m) throws IOException { long ar[][] = new long[n][m]; for (int x = 0; x < n; x++)ar[x]=arl(m); return ar; } char[][] arc(int n, int m) throws IOException { char ar[][] = new char[n][m]; for (int x = 0; x < n; x++)ar[x]=arc(m); return ar; } double[][] ard(int n, int m) throws IOException { double ar[][] = new double[n][m]; for (int x = 0; x < n; x++)ar[x]=ard(m); return ar; } void p(int ar[]) { sb = new StringBuilder(11 * ar.length); for (int a : ar) {sb.append(a);sb.append(' ');} out.println(sb); } void p(long ar[]) { StringBuilder sb = new StringBuilder(20 * ar.length); for (long a : ar){sb.append(a);sb.append(' ');} out.println(sb); } void p(double ar[]) { StringBuilder sb = new StringBuilder(22 * ar.length); for (double a : ar){sb.append(a);sb.append(' ');} out.println(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(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(int ar[][]) { StringBuilder sb = new StringBuilder(11 * 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(20 * 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(double ar[][]) { StringBuilder sb = new StringBuilder(22 * 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 * ar[0].length); for (char a[] : ar) { for (char aa : a){sb.append(aa);sb.append(' ');} sb.append("\n"); } p(sb); } void pl(Object... ar) {for (Object e : ar) p(e + " ");pl();} }
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 8
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
8462191999d4be5dceb928990b69e02c
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 { public static void main(String[] args) throws Exception { long[]pre=new long[200009]; pre[0]=1; pre[1]=1; for(int i=2;i<200009;i++){ pre[i]+=pre[i-1]; pre[i]+=pre[i-2]; while(pre[i]>=mod)pre[i]-=mod; } for(int i=1;i<200009;i++){ pre[i]+=pre[i-1]; while(pre[i]>=mod)pre[i]-=mod; } // for(int i=0;i<10;i++)pw.println(pre[i]); int n=sc.nextInt(); int p=sc.nextInt(); int[]a=sc.nextIntArray(n); TreeSet<Long>vi=new TreeSet<>(Collections.reverseOrder()); for(int i=0;i<n;i++){ vi.add(1l*a[i]); } long ans=0; out: while(!vi.isEmpty()){ long i=vi.pollFirst(); long j=i; while(j!=0){ if(vi.contains(j))continue out; if((j&1)==0){ if((j&2)!=0)break; j>>=2; }else{ j>>=1; } } if(p<31&&(i>=1l<<p))continue out; int cnt=1; long v=i; // pw.println("* "+v); while(v!=1){ v>>=1l; cnt++; } cnt=p-cnt; ans+=pre[cnt]; while(ans>=mod)ans-=mod; } while(ans>=mod)ans-=mod; pw.println(ans); pw.close(); } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } 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 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(); } } 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 long mod = 1000000007; static Random rn = new Random(); static Scanner sc = new Scanner(System.in); static PrintWriter pw = new PrintWriter(System.out); }
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 8
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
f881efcc2c0fda05d230eb7cfaa344f8
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.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_D { 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=22; int VMAX=10; for (int t=0;t<NTESTS;t++) { } log("testing done"); } static class Composite implements Comparable<Composite>{ int a; int b; public int compareTo(Composite X) { int diff=b-a; int Xdiff=X.b-X.a; if (diff!=Xdiff) return diff-Xdiff; return a-X.a; } public Composite(int a, int b) { this.a = a; this.b = b; } } static int MX=100; static ArrayList<Integer> generate(int x) { ArrayList<Integer> lst=new ArrayList<Integer>(); int[] mem=new int[MX]; mem[x]=1; for (int u=1;u<MX;u++) { if (mem[u]==1) { int y=2*u+1; if (y<MX) mem[y]=1; y=4*u; if (y<MX) mem[y]=1; } } for (int u=0;u<MX;u++) { if (mem[u]==1) lst.add(u); } return lst; } static void explore() { int VX=100; //for (int u=2;u<VX;u++) int u=3; { ArrayList<Integer> lstu=generate(u); log(lstu); for (int v=u+1;v<VX;v++) { ArrayList<Integer> lstv=generate(v); loop:for (int x:lstv) { if (Collections.binarySearch(lstu, x)>=0) { log("collision between u:"+u+" and v:"+v); int bob=Collections.binarySearch(lstu,v); if (bob<0) { log("============ and this was not true from the begining !!!"); } break loop; } } } } } // is y descendant of x static boolean descendant(int x,int y) { String X=Integer.toBinaryString(x); String Y=Integer.toBinaryString(y); //log(X+" "+Y); int L=X.length(); for (int i=0;i<L;i++) { if (X.charAt(i)!=Y.charAt(i)) return false; } int LL=Y.length(); int i=L; while (i<LL) { if (Y.charAt(i)=='0') { if (i+1==LL) return false; if (Y.charAt(i+1)!='0') return false; i+=2; } else i++; } return true; } static long mod=1000000007; static void process() throws Exception { out = new BufferedWriter(new OutputStreamWriter(System.out)); reader = new InputReader(System.in); /* log(generate(3)); for (int u=3;u<100;u++) { if (descendant(3,u)) { log(u); } } */ int n=reader.readInt(); int p=reader.readInt(); int PX=p+1; if (PX<3) PX=3; long[] znx=new long[PX]; znx[0]=1; znx[1]=1; znx[2]=2; for (int u=3;u<PX;u++) { znx[u]=znx[u-1]+znx[u-2]; if (znx[u]>=mod) znx[u]-=mod; } long cur=0; long[] sum=new long[PX]; for (int u=0;u<PX;u++) { cur+=znx[u]; if (cur>=mod) cur-=mod; sum[u]=cur; } int[] a=new int[n]; for (int i=0;i<n;i++) a[i]=reader.readInt(); Arrays.sort(a); TreeSet<Integer> good=new TreeSet<Integer>(); for (int y:a) { int x=y; x>>=1; boolean ok=true; while (x>0) { if (good.contains(x) && descendant(x,y)) { ok=false; break; } x>>=1; } if (ok) good.add(y); } long ans=0; //log("good:"+good); //log(znx); for (int x:good) { String X=Integer.toBinaryString(x); int L=X.length(); int rem=p-L; if (rem>=0) { ans+=sum[rem]; if (ans>=mod) ans-=mod; } } output(ans); 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; } } } /* 1 10 1 0 0 0 0 1 0 0 1 1 1 12 1 0 0 1 0 0 1 0 0 0 0 1 1 12 1 0 0 1 0 0 1 0 0 0 0 1 3 12 1 0 0 1 0 0 1 0 0 0 0 1 10 1 0 0 0 0 1 0 0 1 1 12 1 0 0 1 0 0 1 0 0 0 0 1 1 11 1 0 0 0 0 0 0 0 1 1 1 19 1 1 0 1 1 1 1 1 1 0 1 0 0 0 0 1 0 1 1 19 1 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 0 1 0 19 1 0 0 0 1 1 0 0 1 1 0 0 0 1 1 0 0 1 0 1 20 1 1 0 0 0 1 0 1 1 1 0 1 1 1 1 1 0 0 1 0 */
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 8
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
ab0f0050f1be651471eb3bb53a902737
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 javax.swing.*; import java.lang.reflect.Array; import java.text.DecimalFormat; import java.util.*; import java.lang.*; import java.io.*; import java.math.*; import java.util.stream.Stream; // Please name your class Main public class Main { static FastScanner fs=new FastScanner(); static class FastScanner { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st=new StringTokenizer(""); public String next() { while (!st.hasMoreElements()) try { st=new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } int Int() { return Integer.parseInt(next()); } long Long() { return Long.parseLong(next()); } String Str(){ return next(); } } public static void main (String[] args) throws java.lang.Exception { PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); //reading /writing file //Scanner in=new Scanner(System.in); //Scanner in=new Scanner(new File("input.txt")); //PrintWriter pr=new PrintWriter("output.txt") int T=1; for(int t=0;t<T;t++){ Solution sol1=new Solution(out,fs); sol1.solution(T,t); } out.flush(); } public static int[] Arr(int n){ int A[]=new int[n]; for(int i=0;i<n;i++)A[i]=Int(); return A; } public static int Int(){ return fs.Int(); } public static long Long(){ return fs.Long(); } public static String Str(){ return fs.Str(); } } class Solution { PrintWriter out; int INF = Integer.MAX_VALUE; int NINF = Integer.MIN_VALUE; int MOD = 998244353; int mod = 1000000007; //int mod = 998244353; Main.FastScanner fs; public Solution(PrintWriter out, Main.FastScanner fs) { this.out = out; this.fs = fs; } public int[] arr(int n) { int A[] = new int[n]; for(int i = 0; i < n; i++) { A[i] = fs.Int(); } return A; } public void msg(String s) { System.out.println(s); } public void print(int A[]){ StringBuilder str = new StringBuilder(); for(int i: A)str.append(i).append(" "); out.println(str.toString()); } public boolean check(long A[]) { for(int i = 1; i < A.length; i++) { if(A[i] < A[i - 1]) { return false; } } return true; } public void solution(int all, int testcase) { int n = fs.Int(); int p = fs.Int(); long res = 0; int A[] = new int[n]; for(int i = 0; i < n; i++) { A[i] = fs.Int(); } Arrays.sort(A); boolean seen[] = new boolean[n]; Set<Integer>set = new HashSet<>(); for(int i : A) { set.add(i); } for(int i = 0; i < n; i++) { int cur = A[i]; while(cur > 1) { if(cur % 2 == 1) { cur--; cur /= 2; } else { if(cur % 4 != 0) { break; }else { cur /= 4; } } if(set.contains(cur)) { seen[i] = true; break; } } } //System.out.println(Arrays.toString(A)); //System.out.println(Arrays.toString(seen)); long dp[] = new long[p + 1]; int j = 0; long pow = 1; for(int i = 1; i < dp.length; i++) { if(i <= 40) { pow *= 2; } while(j < n) { if(seen[j]) { j++; } else { if(A[j] < pow) { dp[i]++; j++; }else{ break; } } } dp[i] += dp[i - 1]; dp[i] %= mod; if(i - 2 >= 0) { dp[i] += dp[i - 2]; dp[i] %= mod; } } for(long i : dp){ res += i; res %= mod; } out.println(res); } }
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 8
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
204bef51f8f5f1821c02b7b0b9eeefa4
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
/* Goal: Become better in CP! Key: Consistency and Discipline Desire: SDE @ Google USA Motto: Do what i Love <=> Love what i do If you don't use your brain 100%, it deteriorates gradually */ import java.util.*; import java.io.*; import java.math.*; public class Solution { static StringBuffer str=new StringBuffer(); static BufferedReader bf; static PrintWriter pw; static int n, p; static long a[]; static long dp[]; static long mod=(long)1e9+7; static Map<Long, Boolean> map; static Set<Long> set; static boolean canGet(long ele){ if(ele==0) return false; if(set.contains(ele)) return true; if(map.containsKey(ele)) return map.get(ele); boolean poss=false; if(ele%2==1) poss|=canGet((ele-1)/2); if(ele%4==0) poss|=canGet(ele/4); map.put(ele, poss); return poss; } static void templateSortArray(long a[]) { int n=a.length; List < Long > l = new ArrayList < > (); for (int i = 0; i < n; i++) l.add(a[i]); Collections.sort(l); for (int i = 0; i < n; i++) a[i] = l.get(i); } static void solve(int te) throws Exception{ templateSortArray(a); map=new HashMap<>(); dp=new long[p+1]; set=new HashSet<>(); // dp[i]=# of elements in the set < 2^i and >=2^(i-1) for(int i=0;i<n;i++){ int j=(int)(Math.log(a[i])/Math.log(2)); if(j>=p) break; boolean poss=true; if(a[i]%2==1) poss&=!canGet((a[i]-1)/2); if(a[i]%4==0) poss&=!canGet(a[i]/4); if(poss) dp[j+1]++; set.add(a[i]); } for(int i=2;i<=p;i++){ dp[i]+=dp[i-1]+dp[i-2]; if(dp[i]<0) dp[i]+=mod; dp[i]%=mod; } long ans=0; for(int i=1;i<=p;i++){ ans+=dp[i]; if(ans<0) ans+=mod; ans%=mod; } str.append(ans); } public static void main(String[] args) throws java.lang.Exception { boolean lenv=false; int te=1; 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 q1 = Integer.parseInt(bf.readLine().trim()); // for(te=1;te<=q1;te++) { String s[]=bf.readLine().trim().split("\\s+"); n=Integer.parseInt(s[0]); p=Integer.parseInt(s[1]); a=new long[n]; s=bf.readLine().trim().split("\\s+"); for(int i=0;i<n;i++){ a[i]=Long.parseLong(s[i]); } solve(te); // } pw.print(str); pw.flush(); // System.out.println(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 8
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
8f240e7ed3aa4b8f03fab958bab07e5f
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 javax.management.openmbean.KeyAlreadyExistsException; // import java.lang.invoke.ConstantBootstraps; // import java.math.BigInteger; // import java.beans.IndexedPropertyChangeEvent; import java.io.*; @SuppressWarnings("unchecked") public class Main implements Runnable { static FastReader in; static PrintWriter out; static int bit(long n) { return (n == 0) ? 0 : (1 + bit(n & (n - 1))); } static void p(Object o) { out.print(o); } static void pn(Object o) { out.println(o); } static void pni(Object o) { out.println(o); out.flush(); } static String n() throws Exception { return in.next(); } static String nln() throws Exception { return in.nextLine(); } static int ni() throws Exception { return Integer.parseInt(in.next()); } static long nl() throws Exception { return Long.parseLong(in.next()); } static double nd() throws Exception { return Double.parseDouble(in.next()); } static class FastReader { static BufferedReader br; static StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String s) throws Exception { br = new BufferedReader(new FileReader(s)); } String next() throws Exception { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new Exception(e.toString()); } } return st.nextToken(); } String nextLine() throws Exception { String str = ""; try { str = br.readLine(); } catch (IOException e) { throw new Exception(e.toString()); } return str; } } static long power(long a, long b) { if (a == 0L) return 0L; if (b == 0) return 1; long val = power(a, b / 2); val = val * val; if ((b % 2) != 0) val = val * a; return val; } static long power(long a, long b, long mod) { if (a == 0L) return 0L; if (b == 0) return 1; long val = power(a, b / 2L, mod) % mod; val = (val * val) % mod; if ((b % 2) != 0) val = (val * a) % mod; return val; } static ArrayList<Long> prime_factors(long n) { ArrayList<Long> ans = new ArrayList<Long>(); while (n % 2 == 0) { ans.add(2L); n /= 2L; } for (long i = 3; i * i <= n; i++) { while (n % i == 0) { ans.add(i); n /= i; } } if (n > 2) { ans.add(n); } return ans; } static void sort(ArrayList<Long> a) { Collections.sort(a); } static void reverse_sort(ArrayList<Long> a) { Collections.sort(a, Collections.reverseOrder()); } static void swap(long[] a, int i, int j) { long temp = a[i]; a[i] = a[j]; a[j] = temp; } static void swap(List<Long> a, int i, int j) { long temp = a.get(i); a.set(j, a.get(i)); a.set(j, temp); } static void sieve(boolean[] prime) { int n = prime.length - 1; Arrays.fill(prime, true); for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = 2 * i; j <= n; j += i) { prime[j] = false; } } } } static long gcd(long a, long b) { if (a < b) { long temp = a; a = b; b = temp; } if (b == 0) return a; return gcd(b, a % b); } static HashMap<Long, Long> map_prime_factors(long n) { HashMap<Long, Long> map = new HashMap<>(); while (n % 2 == 0) { map.put(2L, map.getOrDefault(2L, 0L) + 1L); n /= 2L; } for (long i = 3; i * i <= n; i++) { while (n % i == 0) { map.put(i, map.getOrDefault(i, 0L) + 1L); n /= i; } } if (n > 2) { map.put(n, map.getOrDefault(n, 0L) + 1L); } return map; } static List<Long> divisor(long n) { List<Long> ans = new ArrayList<>(); ans.add(1L); long count = 0; for (long i = 2L; i * i <= n; i++) { if (n % i == 0) { if (i == n / i) ans.add(i); else { ans.add(i); ans.add(n / i); } } } return ans; } static void sum_of_divisors(int n) { int[] dp = new int[n + 1]; for (int i = 1; i <= n; i++) { dp[i] += i; for (int j = i + i; j <= n; j += i) { dp[j] += i; } } } static void prime_factorization_using_sieve(int n) { int[] dp = new int[n + 1]; Arrays.fill(dp, Integer.MAX_VALUE); for (int i = 2; i <= n; i++) { dp[i] = Math.min(dp[i], i);// dp[i] stores smallest prime number which divides number i for (int j = 2 * i; j <= n; j++) {// can calculate prime factorization in O(logn) time by dividing // val/=dp[val]; till 1 is obtained dp[j] = Math.min(dp[j], i); } } } /* * ----------------------------------------------------Sorting------------------ * ------------------------------------------------ */ public static void sort(long[] arr, int l, int r) { if (l >= r) return; int mid = (l + r) / 2; sort(arr, l, mid); sort(arr, mid + 1, r); merge(arr, l, mid, r); } public static void sort(int[] arr, int l, int r) { if (l >= r) return; int mid = (l + r) / 2; sort(arr, l, mid); sort(arr, mid + 1, r); merge(arr, l, mid, r); } static void merge(int[] arr, int l, int mid, int r) { int[] left = new int[mid - l + 1]; int[] right = new int[r - mid]; for (int i = l; i <= mid; i++) { left[i - l] = arr[i]; } for (int i = mid + 1; i <= r; i++) { right[i - (mid + 1)] = arr[i]; } int left_start = 0; int right_start = 0; int left_length = mid - l + 1; int right_length = r - mid; int temp = l; while (left_start < left_length && right_start < right_length) { if (left[left_start] < right[right_start]) { arr[temp] = left[left_start++]; } else { arr[temp] = right[right_start++]; } temp++; } while (left_start < left_length) { arr[temp++] = left[left_start++]; } while (right_start < right_length) { arr[temp++] = right[right_start++]; } } static void merge(long[] arr, int l, int mid, int r) { long[] left = new long[mid - l + 1]; long[] right = new long[r - mid]; for (int i = l; i <= mid; i++) { left[i - l] = arr[i]; } for (int i = mid + 1; i <= r; i++) { right[i - (mid + 1)] = arr[i]; } int left_start = 0; int right_start = 0; int left_length = mid - l + 1; int right_length = r - mid; int temp = l; while (left_start < left_length && right_start < right_length) { if (left[left_start] < right[right_start]) { arr[temp] = left[left_start++]; } else { arr[temp] = right[right_start++]; } temp++; } while (left_start < left_length) { arr[temp++] = left[left_start++]; } while (right_start < right_length) { arr[temp++] = right[right_start++]; } } // static int[] smallest_prime_factor; // static int count = 1; // static int[] p = new int[100002]; // static long[] flat_tree = new long[300002]; // static int[] in_time = new int[1000002]; // static int[] out_time = new int[1000002]; // static long[] subtree_gcd = new long[100002]; // static int w = 0; // static boolean poss = true; /* * (a^b^c)%mod * Using fermats Little theorem * x^(mod-1)=1(mod) * so b^c can be written as b^c=x*(mod-1)+y * then (a^(x*(mod-1)+y))%mod=(a^(x*(mod-1))*a^(y))mod * the term (a^(x*(mod-1)))%mod=a^(mod-1)*a^(mod-1) * */ // ---------------------------------------------------Segment_Tree----------------------------------------------------------------// // static class comparator implements Comparator<node> { // public int compare(node a, node b) { // return a.a - b.a > 0 ? 1 : -1; // } // } static class Segment_Tree { private long[] segment_tree; public Segment_Tree(int n) { this.segment_tree = new long[4 * n + 1]; } void build(int index, int left, int right, int[] a) { if (left == right) { segment_tree[index] = a[left]; return; } int mid = (left + right) / 2; build(2 * index + 1, left, mid, a); build(2 * index + 2, mid + 1, right, a); segment_tree[index] = segment_tree[2 * index + 1] + segment_tree[2 * index + 2]; } long query(int index, int left, int right, int l, int r) { if (left > right) return 0; if (left >= l && r >= right) { return segment_tree[index]; } if (l > right || left > r) return 0; int mid = (left + right) / 2; return query(2 * index + 1, left, mid, l, r) + query(2 * index + 2, mid + 1, right, l, r); } void update(int index, int left, int right, int node, int val) { if (left == right) { segment_tree[index] = val; return; } int mid = (left + right) / 2; if (node <= mid) update(2 * index + 1, left, mid, node, val); else update(2 * index + 2, mid + 1, right, node, val); segment_tree[index] = segment_tree[2 * index + 1] + segment_tree[2 * index + 2]; } } static class min_Segment_Tree { private long[] segment_tree; public min_Segment_Tree(int n) { this.segment_tree = new long[4 * n + 1]; } void build(int index, int left, int right, int[] a) { if (left == right) { segment_tree[index] = a[left]; return; } int mid = (left + right) / 2; build(2 * index + 1, left, mid, a); build(2 * index + 2, mid + 1, right, a); segment_tree[index] = Math.min(segment_tree[2 * index + 1], segment_tree[2 * index + 2]); } long query(int index, int left, int right, int l, int r) { if (left > right) return Integer.MAX_VALUE; if (left >= l && r >= right) { return segment_tree[index]; } if (l > right || left > r) return Integer.MAX_VALUE; int mid = (left + right) / 2; return Math.min(query(2 * index + 1, left, mid, l, r), query(2 * index + 2, mid + 1, right, l, r)); } void update(int index, int left, int right, int node, int val) { if (left == right) { segment_tree[index] = val; return; } int mid = (left + right) / 2; if (node <= mid) update(2 * index + 1, left, mid, node, val); else update(2 * index + 2, mid + 1, right, node, val); segment_tree[index] = Math.min(segment_tree[2 * index + 1], segment_tree[2 * index + 2]); } } static class max_Segment_Tree { public long[] segment_tree; public max_Segment_Tree(int n) { this.segment_tree = new long[4 * n + 1]; } void build(int index, int left, int right, long[] a) { // pn(index+" "+left+" "+right); if (left == right) { segment_tree[index] = a[left]; return; } int mid = (left + right) / 2; build(2 * index + 1, left, mid, a); build(2 * index + 2, mid + 1, right, a); segment_tree[index] = Math.max(segment_tree[2 * index + 1], segment_tree[2 * index + 2]); } long query(int index, int left, int right, int l, int r) { if (left >= l && r >= right) { return segment_tree[index]; } if (l > right || left > r) return Long.MIN_VALUE; int mid = (left + right) / 2; long ans1 = query(2 * index + 1, left, mid, l, r); long ans2 = query(2 * index + 2, mid + 1, right, l, r); long max = Math.max(ans1, ans2); // pn(index+" "+left+" "+right+" "+max+" "+l+" "+r+" "+ans1+" "+ans2); return max; } void update(int index, int left, int right, int node, int val) { if (left == right) { segment_tree[index] += val; return; } int mid = (left + right) / 2; if (node <= mid) update(2 * index + 1, left, mid, node, val); else update(2 * index + 2, mid + 1, right, node, val); segment_tree[index] = Math.max(segment_tree[2 * index + 1], segment_tree[2 * index + 2]); } } // // ------------------------------------------------------ DSU // // --------------------------------------------------------------------// static class dsu { private int[] parent; private int[] rank; private int[] size; public dsu(int n) { this.parent = new int[n + 1]; this.rank = new int[n + 1]; this.size = new int[n + 1]; for (int i = 0; i <= n; i++) { parent[i] = i; rank[i] = 1; size[i] = 1; } } int findParent(int a) { if (parent[a] == a) return a; else return parent[a] = findParent(parent[a]); } void join(int a, int b) { int parent_a = findParent(a); int parent_b = findParent(b); if (parent_a == parent_b) return; if (rank[parent_a] > rank[parent_b]) { parent[parent_b] = parent_a; size[parent_a] += size[parent_b]; } else if (rank[parent_a] < rank[parent_b]) { parent[parent_a] = parent_b; size[parent_b] += size[parent_a]; } else { parent[parent_a] = parent_b; size[parent_b] += size[parent_a]; rank[parent_b]++; } } } // ------------------------------------------------Comparable---------------------------------------------------------------------// public static class rectangle { int x1, x3, y1, y3;// lower left and upper rigth coordinates int x2, y2, x4, y4;// remaining coordinates /* * (x4,y4) (x3,y3) * ____________ * | | * |____________| * * (x1,y1) (x2,y2) */ public rectangle(int x1, int y1, int x3, int y3) { this.x1 = x1; this.y1 = y1; this.x3 = x3; this.y3 = y3; this.x2 = x3; this.y2 = y1; this.x4 = x1; this.y4 = y3; } public long area() { if (x3 < x1 || y3 < y1) return 0; return (long) Math.abs(x1 - x3) * (long) Math.abs(y1 - y3); } } static long intersection(rectangle a, rectangle b) { if (a.x3 < a.x1 || a.y3 < a.y1 || b.x3 < b.x1 || b.y3 < b.y1) return 0; long l1 = ((long) Math.min(a.x3, b.x3) - (long) Math.max(a.x1, b.x1)); long l2 = ((long) Math.min(a.y3, b.y3) - (long) Math.max(a.y1, b.y1)); if (l1 < 0 || l2 < 0) return 0; long area = ((long) Math.min(a.x3, b.x3) - (long) Math.max(a.x1, b.x1)) * ((long) Math.min(a.y3, b.y3) - (long) Math.max(a.y1, b.y1)); if (area < 0) return 0; return area; } // --------------------------------------------------------------Multiset---------------------------------------------------------------// public static class multiset { public TreeMap<Integer, Integer> map; public int size = 0; public multiset() { map = new TreeMap<>(); } public multiset(int[] a) { map = new TreeMap<>(); size = a.length; for (int i = 0; i < a.length; i++) { map.put(a[i], map.getOrDefault(a[i], 0) + 1); } } void add(int a) { size++; map.put(a, map.getOrDefault(a, 0) + 1); } void remove(int a) { size--; int val = map.get(a); map.put(a, val - 1); if (val == 1) map.remove(a); } void removeAll(int a) { if (map.containsKey(a)) { size -= map.get(a); map.remove(a); } } int ceiling(int a) { if (map.ceilingKey(a) != null) { int find = map.ceilingKey(a); return find; } else return Integer.MIN_VALUE; } int floor(int a) { if (map.floorKey(a) != null) { int find = map.floorKey(a); return find; } else return Integer.MAX_VALUE; } int lower(int a) { if (map.lowerKey(a) != null) { int find = map.lowerKey(a); return find; } else return Integer.MAX_VALUE; } int higher(int a) { if (map.higherKey(a) != null) { int find = map.higherKey(a); return find; } else return Integer.MIN_VALUE; } int first() { return map.firstKey(); } int last() { return map.lastKey(); } boolean contains(int a) { if (map.containsKey(a)) return true; return false; } int size() { return size; } void clear() { map.clear(); } int poll() { if (map.size() == 0) { return Integer.MAX_VALUE; } size--; int first = map.firstKey(); if (map.get(first) == 1) { map.pollFirstEntry(); } else map.put(first, map.get(first) - 1); return first; } int polllast() { if (map.size() == 0) { return Integer.MAX_VALUE; } size--; int last = map.lastKey(); if (map.get(last) == 1) { map.pollLastEntry(); } else map.put(last, map.get(last) - 1); return last; } } static class pair implements Comparable<pair> { int a; int b; int dir; public pair(int a, int b, int dir) { this.a = a; this.b = b; this.dir = dir; } public int compareTo(pair p) { // if (this.b == Integer.MIN_VALUE || p.b == Integer.MIN_VALUE) // return (int) (this.index - p.index); return (int) (this.a - p.a); } } static class pair2 implements Comparable<pair2> { long a; int index; public pair2(long a, int index) { this.a = a; this.index = index; } public int compareTo(pair2 p) { return (int) (this.a - p.a); } } static class node implements Comparable<node> { int l; int r; public node(int l, int r) { this.l = l; this.r = r; } public int compareTo(node a) { if (this.l == a.l) { return this.r - a.r; } return (int) (this.l - a.l); } } static long ans = 0; static int leaf = 0; static boolean poss = true; static long mod = 1000000007L; static int[] dx = { -1, 0, 0, 1, -1, -1, 1, 1 }; static int[] dy = { 0, -1, 1, 0, -1, 1, -1, 1 }; static Set<Integer> path_nodes; int count = 0; public static void main(String[] args) throws Exception { // new Thread(null,new Main(), "1", 1 << 26).start(); long start = System.nanoTime(); in = new FastReader(); out = new PrintWriter(System.out, false); int tc = 1; boolean[] sieve=new boolean[100009]; sieve(sieve); while (tc-- > 0) { int n=ni(); int p=ni(); int[] a=new int[n]; Map<Integer,Integer> map=new HashMap<>(); Set<Integer> set=new HashSet<>(); for(int i=0;i<n;i++){ a[i]=ni(); set.add(a[i]); map.put(a[i], 1); } sort(a, 0, n-1); //dp[i] represents total count of numbers which are present in between 2**i and 2**(i+1){non-inclusive} //a number z which is 2**x will generate two numbers 2*z+1 and 4*z which is the 2**(x+1) and 2**(x+2) //so a number with power x will generate two numbers with power x+1 and x+2 //every number will generate a tree whose each element is goind to be unique //but a tree generated from one number can be a subtree of another number, in this case we will overcount that subtree values //to avoid that we will remove larger values which can be generated from samller values //so for a number y we check if (y-1)/2 or y/4 is present in the array if yes we can remove y //here we can only go in one direction if y is even we can do towards (y-1)/2 and if y is odd we cant go towards y/4 //so at max log2(a) time is required to delete particular number for(int i=n-1;i>=0;i--){ int x=a[i]; if(!set.contains(a[i]))continue; while(x>0 && ((x!=1 && (((x-1)%2)==0)) || (((x)%4)==0))){ if(((x-1)%2)==0){ if(set.contains((x-1)/2)){ set.remove(a[i]); break; } x=(x-1)/2; // pn("1 "+x); } if(x>0 && (x%4)==0){ if(set.contains(x/4)){ set.remove(a[i]); break; } x/=4; // pn("2 "+x); } } } long ans=0; long[] dp=new long[p+1]; for(int x:set){ int find=-1; for(int i=31;i>=0;i--){ if(((1<<i)&x)!=0){ find=i; break; } } // pn(x+" "+find); if(find>=p)continue; dp[find]++; } dp[1]=(dp[0]+dp[1])%mod; for(int i=2;i<p;i++){ long val=(dp[i-1]+dp[i-2])%mod; dp[i]=(dp[i]+val)%mod; } for(int i=0;i<p;i++){ ans=(ans+dp[i])%mod; } pn(ans); } long end = System.nanoTime(); // pn((end-start)*1.0/1000000000); out.flush(); out.close(); } static void find(int i,int sum,int[] r,int[] b){ } public void run() { try { in = new FastReader(); out = new PrintWriter(System.out); int tc = ni(); while (tc-- > 0) { } out.flush(); } catch (Exception e) { } } static boolean dfs(int i, int p, int x, int y, Set<Integer> k_nodes, boolean[] visited, List<TreeSet<Integer>> arr, int[] dp) { visited[i] = true; boolean found = false; if (k_nodes.contains(i)) { found = true; } List<Integer> remove = new ArrayList<>(); for (int nei : arr.get(i)) { if (visited[nei] || nei == p) continue; boolean yes = dfs(nei, i, x, y, k_nodes, visited, arr, dp); if (!yes) remove.add(nei); // pn(i+" "+nei+" "+yes); found = found || yes; } for (int nei : remove) { arr.get(i).remove(nei); } return found; } static boolean inside(int i, int j, int n, int m) { if (i >= 0 && j >= 0 && i < n && j < m) return true; return false; } static long ncm(long[] fact, long[] fact_inv, int n, int m) { if (n < m) return 0L; long a = fact[n]; long b = fact_inv[n - m]; long c = fact_inv[m]; a = (a * b) % mod; return (a * c) % mod; } static int binary_search(int[] a, int val) { int l = 0; int r = a.length - 1; int ans = 0; while (l <= r) { int mid = l + (r - l) / 2; if (a[mid] <= val) { ans = mid; l = mid + 1; } else r = mid - 1; } return ans; } static int[] longest_common_prefix(String s) { int m = s.length(); int[] lcs = new int[m]; int len = 0; int i = 1; lcs[0] = 0; while (i < m) { if (s.charAt(i) == s.charAt(len)) { lcs[i++] = ++len; } else { if (len == 0) { lcs[i] = 0; i++; } else len = lcs[len - 1]; } } return lcs; } static void swap(char[] a, char[] b, int i, int j) { char temp = a[i]; a[i] = b[j]; b[j] = temp; } static void factorial(long[] fact, long[] fact_inv, int n, long mod) { fact[0] = 1; for (int i = 1; i < n; i++) { fact[i] = (i * fact[i - 1]) % mod; } for (int i = 0; i < n; i++) { fact_inv[i] = power(fact[i], mod - 2, mod);// (1/x)%m can be calculated by fermat's little theoram which is // (x**(m-2))%m when m is prime } // (a^(b^c))%m is equal to, let res=(b^c)%(m-1) then (a^res)%m // https://www.geeksforgeeks.org/find-power-power-mod-prime/?ref=rp } static void find(int i, int n, int[] row, int[] col, int[] d1, int[] d2) { if (i >= n) { ans++; return; } for (int j = 0; j < n; j++) { if (col[j] == 0 && d1[i - j + n - 1] == 0 && d2[i + j] == 0) { col[j] = 1; d1[i - j + n - 1] = 1; d2[i + j] = 1; find(i + 1, n, row, col, d1, d2); col[j] = 0; d1[i - j + n - 1] = 0; d2[i + j] = 0; } } } static int answer(int l, int r, int[][] dp) { if (l > r) return 0; if (l == r) { dp[l][r] = 1; return 1; } if (dp[l][r] != -1) return dp[l][r]; int val = Integer.MIN_VALUE; int mid = l + (r - l) / 2; val = 1 + Math.max(answer(l, mid - 1, dp), answer(mid + 1, r, dp)); return dp[l][r] = val; } static void print(int[] a) { for (int i = 0; i < a.length; i++) p(a[i] + " "); pn(""); } static long count(long n) { long count = 0; while (n != 0) { count += n % 10; n /= 10; } return count; } static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } static int LcsOfPrefix(String a, String b) { int i = 0; int j = 0; int count = 0; while (i < a.length() && j < b.length()) { if (a.charAt(i) == b.charAt(j)) { j++; count++; } i++; } return a.length() + b.length() - 2 * count; } static void reverse(int[] a, int n) { for (int i = 0; i < n / 2; i++) { int temp = a[i]; a[i] = a[n - i - 1]; a[n - i - 1] = temp; } } static char get_char(int a) { return (char) (a + 'a'); } static int find1(int[] a, int val) { int ans = -1; int l = 0; int r = a.length - 1; while (l <= r) { int mid = l + (r - l) / 2; if (a[mid] <= val) { l = mid + 1; ans = mid; } else r = mid - 1; } return ans; } static int find2(int[] a, int val) { int l = 0; int r = a.length - 1; int ans = -1; while (l <= r) { int mid = l + (r - l) / 2; if (a[mid] <= val) { ans = mid; l = mid + 1; } else r = mid - 1; } return ans; } // static void dfs(List<List<Integer>> arr, int node, int parent, long[] val) { // p[node] = parent; // in_time[node] = count; // flat_tree[count] = val[node]; // subtree_gcd[node] = val[node]; // count++; // for (int adj : arr.get(node)) { // if (adj == parent) // continue; // dfs(arr, adj, node, val); // subtree_gcd[node] = gcd(subtree_gcd[adj], subtree_gcd[node]); // } // out_time[node] = count; // flat_tree[count] = val[node]; // count++; // } }
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 8
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
24ccc2cd7c87a64e7172f8303f220a7d
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 { private static final int MOD = 1_000_000_007; private static final int MANUAL_P_MAX = 30; public static void solve(FastIO io) { final int N = io.nextInt(); final int P = io.nextInt(); final int[] A = io.nextIntArray(N); HashSet<Integer> allNums = new HashSet<>(); for (int x : A) { allNums.add(x); } ArrayList<Integer> sourceNums = new ArrayList<>(); for (int x : A) { if (!creatable(x, allNums)) { sourceNums.add(x); } } int dpLen = 2 + Math.max(P + 2, MANUAL_P_MAX); int[] dp = new int[dpLen]; for (int x : sourceNums) { int numBits = Integer.SIZE - Integer.numberOfLeadingZeros(x); ++dp[numBits]; } for (int i = 1; i < P; ++i) { dp[i + 1] = add(dp[i + 1], dp[i]); dp[i + 2] = add(dp[i + 2], dp[i]); } int ans = 0; for (int i = 1; i <= P; ++i) { ans = add(ans, dp[i]); } io.println(ans); } private static int add(int a, int b) { int res = a + b; if (res >= MOD) { res -= MOD; } return res; } private static boolean creatable(int x, HashSet<Integer> nums) { int xOrig = x; int lastDigit = -1; int lastCount = 0; while (x > 0) { int d = x & 1; x >>= 1; if (d == lastDigit) { ++lastCount; } else { if (lastDigit == 0 && (lastCount & 1) == 1) { return false; } lastDigit = d; lastCount = 1; } if (nums.contains(x) && (lastDigit == 1 || (lastCount & 1) == 0)) { return true; } } return false; } public static class FastIO { private InputStream reader; private PrintWriter writer; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public FastIO(InputStream r, OutputStream w) { reader = r; writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(w))); } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = reader.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 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; } // TODO: read this byte-by-byte like the other read functions. public double nextDouble() { return Double.parseDouble(nextString()); } public int[] nextIntArray(int n) { return nextIntArray(n, 0); } public int[] nextIntArray(int n, int off) { int[] arr = new int[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextInt(); } return arr; } public long[] nextLongArray(int n) { return nextLongArray(n, 0); } public long[] nextLongArray(int n, int off) { long[] arr = new long[n + off]; for (int i = 0; i < n; i++) { arr[i + off] = nextLong(); } return arr; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) { if (i != 0) { writer.print(' '); } writer.print(arr[i]); } } public void printlnArray(int[] arr) { printArray(arr); writer.println(); } public void printlnArray(long[] arr) { printArray(arr); writer.println(); } public void printf(String format, Object... args) { print(String.format(format, args)); } public void flush() { writer.flush(); } } public static void main(String[] args) { FastIO io = new FastIO(System.in, System.out); solve(io); io.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 8
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
23529b9a8ab0126cd04fb5c4911a4e31
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.IOException; import java.io.InputStream; import java.util.*; public class Main { static InputReader sc = new InputReader(System.in); public static void main(String[] args) { solve(); } private static void solve() { int n=sc.nextInt(); int p=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); } Arrays.sort(a); double mod=1e9+7; int maxn=200005; int dp[]=new int[maxn]; Set<Integer> useful =new HashSet<Integer>(); for(int i=0;i<n;i++){ int x=a[i]; boolean flag=false; while(x>0){ if(useful.contains(x)){ flag=true; break; } if((x&1)==1){ x>>=1; }else if((x&2)!=0){ break; }else{ x>>=2; } } if(!flag){ useful.add(a[i]); } } for(int i:useful){ //System.out.println(i+" "+cal(i)); dp[cal(i)]++; } dp[1]+=dp[0]; for(int i=2;i<p;i++){ dp[i]+=dp[i-1]+dp[i-2]; dp[i]%=mod; } int ans=0; for(int i=0;i<p;i++){ ans+=dp[i]; ans%=mod; } System.out.println(ans); } private static int cal(int i) { int ans=0; while(i>1){ i>>=1; ans++; } return ans; } } class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } }
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 8
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
fd19d4329d2ceeb926cb9c972e793b47
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 final class Main { //int 2e9 - long 9e18 static PrintWriter out = new PrintWriter(System.out); static FastReader in = new FastReader(); static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)}; static int mod = (int) (1e9 + 7); static int mod2 = 998244353; public static void main(String[] args) { int tt = 1; while (tt-- > 0) { solve(); } out.flush(); } public static void solve() { int n = i(); int p = i(); int[] a = input(n); shuffleAndSort(a); Set<Integer> set = new HashSet<>(); for (int b : a) { int x = b; boolean flag = true; while (b > 0) { if (set.contains(b)) { flag = false; break; } if (b % 4 == 0) { b = b / 4; } else if (b % 2 == 1) { b = (b - 1) / 2; } else { break; } } if (flag) { set.add(x); } } int[] dpsum = new int[p]; int[] dp = new int[p]; dp[0] = 1; dpsum[0] = 1; // xx1 xx00 for (int i = 1; i < p; i++) { dp[i] = dp[i - 1] + ((i - 2 >= 0) ? dp[i - 2] : 0); dp[i] %= mod; dpsum[i] = dpsum[i - 1] + dp[i]; dpsum[i] %= mod; } int ans = 0; for (int x : set) { int len = Integer.toBinaryString(x).length(); if(len>p){continue;} ans += dpsum[p - len]; ans %= mod; } out.println(ans); } // (10,5) = 2 ,(11,5) = 3 static long upperDiv(long a, long b) { return (a / b) + ((a % b == 0) ? 0 : 1); } static long sum(int[] a) { long sum = 0; for (int x : a) { sum += x; } return sum; } static int[] preint(int[] a) { int[] pre = new int[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] pre(int[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static long[] post(int[] a) { long[] post = new long[a.length + 1]; post[0] = 0; for (int i = 0; i < a.length; i++) { post[i + 1] = post[i] + a[a.length - 1 - i]; } return post; } static long[] pre(long[] a) { long[] pre = new long[a.length + 1]; pre[0] = 0; for (int i = 0; i < a.length; i++) { pre[i + 1] = pre[i] + a[i]; } return pre; } static void print(char A[]) { for (char c : A) { out.print(c); } out.println(); } static void print(boolean A[]) { for (boolean c : A) { out.print(c + " "); } out.println(); } static void print(int A[]) { for (int c : A) { out.print(c + " "); } out.println(); } static void print(long A[]) { for (long i : A) { out.print(i + " "); } out.println(); } static void print(List<Integer> A) { for (int a : A) { out.print(a + " "); } } static int i() { return in.nextInt(); } static long l() { return in.nextLong(); } static double d() { return in.nextDouble(); } static String s() { return in.nextLine(); } static String c() { return in.next(); } static int[][] inputWithIdx(int N) { int A[][] = new int[N][2]; for (int i = 0; i < N; i++) { A[i] = new int[]{i, in.nextInt()}; } return A; } 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; } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long GCD(long a, long b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } static long LCM(int a, int b) { return (long) a / GCD(a, b) * b; } static long LCM(long a, long b) { return a / GCD(a, b) * b; } // find highest i which satisfy a[i]<=x static int lowerbound(int[] a, int x) { int l = 0; int r = a.length - 1; while (l < r) { int m = (l + r + 1) / 2; if (a[m] <= x) { l = m; } else { r = m - 1; } } return l; } static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } } static void shuffleAndSort(int[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); int[] temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr, comparator); } static void shuffleAndSort(long[] arr) { for (int i = 0; i < arr.length; i++) { int rand = (int) (Math.random() * arr.length); long temp = arr[rand]; arr[rand] = arr[i]; arr[i] = temp; } Arrays.sort(arr); } static boolean isPerfectSquare(double number) { double sqrt = Math.sqrt(number); return ((sqrt - Math.floor(sqrt)) == 0); } static void swap(int A[], int a, int b) { int t = A[a]; A[a] = A[b]; A[b] = t; } static void swap(char A[], int a, int b) { char t = A[a]; A[a] = A[b]; A[b] = t; } static long pow(long a, long b, int mod) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow = (pow * x) % mod; } x = (x * x) % mod; b /= 2; } return pow; } static long pow(long a, long b) { long pow = 1; long x = a; while (b != 0) { if ((b & 1) != 0) { pow *= x; } x = x * x; b /= 2; } return pow; } static long modInverse(long x, int mod) { return pow(x, mod - 2, mod); } 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 = i + 6) { if (N % i == 0 || N % (i + 2) == 0) { return false; } } return true; } public static String reverse(String str) { if (str == null) { return null; } return new StringBuilder(str).reverse().toString(); } public static void reverse(int[] arr) { for (int i = 0; i < arr.length / 2; i++) { int tmp = arr[i]; arr[arr.length - 1 - i] = tmp; arr[i] = arr[arr.length - 1 - i]; } } public static String repeat(char ch, int repeat) { if (repeat <= 0) { return ""; } final char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; i--) { buf[i] = ch; } return new String(buf); } public static int[] manacher(String s) { char[] chars = s.toCharArray(); int n = s.length(); int[] d1 = new int[n]; for (int i = 0, l = 0, r = -1; i < n; i++) { int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1); while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) { k++; } d1[i] = k--; if (i + k > r) { l = i - k; r = i + k; } } return d1; } public static int[] kmp(String s) { int n = s.length(); int[] res = new int[n]; for (int i = 1; i < n; ++i) { int j = res[i - 1]; while (j > 0 && s.charAt(i) != s.charAt(j)) { j = res[j - 1]; } if (s.charAt(i) == s.charAt(j)) { ++j; } res[i] = j; } return res; } } class Pair { int i; int j; Pair(int i, int j) { this.i = i; this.j = j; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Pair pair = (Pair) o; return i == pair.i && j == pair.j; } @Override public int hashCode() { return Objects.hash(i, j); } } class ThreePair { int i; int j; int k; ThreePair(int i, int j, int k) { this.i = i; this.j = j; this.k = k; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ThreePair pair = (ThreePair) o; return i == pair.i && j == pair.j && k == pair.k; } @Override public int hashCode() { return Objects.hash(i, j); } } 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; } } class Node { int val; public Node(int val) { this.val = val; } } class ST { int n; Node[] st; ST(int n) { this.n = n; st = new Node[4 * Integer.highestOneBit(n)]; } void build(Node[] nodes) { build(0, 0, n - 1, nodes); } private void build(int id, int l, int r, Node[] nodes) { if (l == r) { st[id] = nodes[l]; return; } int mid = (l + r) >> 1; build((id << 1) + 1, l, mid, nodes); build((id << 1) + 2, mid + 1, r, nodes); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } void update(int i, Node node) { update(0, 0, n - 1, i, node); } private void update(int id, int l, int r, int i, Node node) { if (i < l || r < i) { return; } if (l == r) { st[id] = node; return; } int mid = (l + r) >> 1; update((id << 1) + 1, l, mid, i, node); update((id << 1) + 2, mid + 1, r, i, node); st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]); } Node get(int x, int y) { return get(0, 0, n - 1, x, y); } private Node get(int id, int l, int r, int x, int y) { if (x > r || y < l) { return new Node(0); } if (x <= l && r <= y) { return st[id]; } int mid = (l + r) >> 1; return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y)); } Node comb(Node a, Node b) { if (a == null) { return b; } if (b == null) { return a; } return new Node(GCD(a.val, b.val)); } static int GCD(int a, int b) { if (b == 0) { return a; } else { return GCD(b, a % b); } } }
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 8
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
db6811e2fab2580a9c23d6fdac54dbd9
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 D_Infinite_Set{ public static void solve(){ } public static void main(String args[])throws IOException{ Reader sc=new Reader(); int n=sc.nextInt(); int p=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=sc.nextInt(); HashSet<Integer> set=new HashSet<>(); Arrays.sort(a); BitManipulation bit=new BitManipulation(); for(int i=0;i<n;i++){ int ele=a[i]; boolean flag=false; while(ele>0){ if(set.contains(ele)){ flag=true; } if((ele&1)==1){ ele=ele>>1; } else if((ele&3)>0){ break; } else{ ele>>=2; } } if(flag==false)set.add(a[i]); } int count[]=new int[34]; for(int i:set){ int pos=bit.getGreatestSetBit(i); count[pos]++; } long mod=(long)1e9+7; long dp[]=new long[p+1]; for(int i=0;i<p;i++){ if(i<32) dp[i]+=count[i]; if(i>=1)dp[i]=(dp[i]%mod+dp[i-1]%mod)%mod; if(i>=2)dp[i]=(dp[i]%mod+dp[i-2]%mod)%mod; } long res=0; for(int i=0;i<p;i++) res=(res%mod+dp[i]%mod)%mod; System.out.println(res); } public static <K,V> Map<K,V> getLimitedSizedCache(int size){ /*Returns an unlimited sized map*/ if(size==0){ return (LinkedHashMap<K, V>) Collections.synchronizedMap(new LinkedHashMap<K, V>()); } /*Returns the map with the limited size*/ Map<K, V> linkedHashMap = Collections.synchronizedMap(new LinkedHashMap<K, V>() { protected boolean removeEldestEntry(Map.Entry<K, V> eldest) { return size() > size; } }); return linkedHashMap; } } class BitManipulation { public int getGreatestSetBit(int num){//Return lsb starts from 0; int pos=0; while(num>0){ num=num>>1; pos++; } return pos-1; } } class BinarySearch<T extends Comparable<T>> { T ele[]; int n; public BinarySearch(T ele[],int n){ this.ele=(T[]) ele; Arrays.sort(this.ele); this.n=n; } public int lower_bound(T x){ //Return next smallest element greater than ewqual to the current element int left=0; int right=n-1; while(left<=right){ int mid=left+(right-left)/2; if(x.compareTo(ele[mid])==0)return mid; if(x.compareTo(ele[mid])>0)left=mid+1; else right=mid-1; } if(left ==n)return -1; return left; } public int upper_bound(T x){ //Returns the highest element lss than equal to the current element int left=0; int right=n-1; while(left<=right){ int mid=left+(right-left)/2; if(x.compareTo(ele[mid])==0)return mid; if(x.compareTo(ele[mid])>0)left=mid+1; else right=mid-1; } return right; } } 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[1000000]; // line length int cnt = 0, c; while ((c = read()) != -1) { if (c == '\n') { if (cnt != 0) { break; } else { continue; } } 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(); } } class Data implements Comparable<Data>{ int num; public Data(int num){ this.num=num; } public int compareTo(Data o){ return -o.num+num; } public String toString(){ return num+" "; } } class Binary{ public String convertToBinaryString(long ele){ StringBuffer res=new StringBuffer(); while(ele>0){ if(ele%2==0)res.append(0+""); else res.append(1+""); ele=ele/2; } return res.reverse().toString(); } } class FenwickTree{ int bit[]; int size; FenwickTree(int n){ this.size=n; bit=new int[size]; } public void modify(int index,int value){ while(index<size){ bit[index]+=value; index=(index|(index+1)); } } public int get(int index){ int ans=0; while(index>=0){ ans+=bit[index]; index=(index&(index+1))-1; } return ans; } } class PAndC{ long c[][]; long mod; public PAndC(int n,long mod){ c=new long[n+1][n+1]; this.mod=mod; build(n); } public void build(int n){ for(int i=0;i<=n;i++){ c[i][0]=1; c[i][i]=1; for(int j=1;j<i;j++){ c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod; } } } } class Trie{ int trie[][]; int revind[]; int root[]; int tind,n; int sz[]; int drev[]; public Trie(){ trie=new int[1000000][2]; root=new int[600000]; sz=new int[1000000]; tind=0; n=0; revind=new int[1000000]; drev=new int[20]; } public void add(int ele){ // System.out.println(root[n]+" "); n++; tind++; revind[tind]=n; root[n]=tind; addimpl(root[n-1],root[n],ele); } public void addimpl(int prev_root,int cur_root,int ele){ for(int i=18;i>=0;i--){ int edge=(ele&(1<<i))>0?1:0; trie[cur_root][1-edge]=trie[prev_root][1-edge]; sz[cur_root]=sz[trie[cur_root][1-edge]]; tind++; drev[i]=cur_root; revind[tind]=n; trie[cur_root][edge]=tind; cur_root=tind; prev_root=trie[prev_root][edge]; } sz[cur_root]+=sz[prev_root]+1; for(int i=0;i<=18;i++){ sz[drev[i]]=sz[trie[drev[i]][0]]+sz[trie[drev[i]][1]]; } } public void findmaxxor(int l,int r,int x){ int ans=0; int cur_root=root[r]; for(int i=18;i>=0;i--){ int edge=(x&(1<<i))>0?1:0; if(revind[trie[cur_root][1-edge]]>=l){ cur_root=trie[cur_root][1-edge]; ans+=(1-edge)*(1<<i); }else{ cur_root=trie[cur_root][edge]; ans+=(edge)*(1<<i); } } System.out.println(ans); } public void findKthStatistic(int l,int r,int k){ //System.out.println("In 3"); int curr=root[r]; int curl=root[l-1]; int ans=0; for(int i=18;i>=0;i--){ for(int j=0;j<2;j++){ if(sz[trie[curr][j]]-sz[trie[curl][j]]<k) k-=sz[trie[curr][j]]-sz[trie[curl][j]]; else{ curr=trie[curr][j]; curl=trie[curl][j]; ans+=(j)*(1<<i); break; } } } System.out.println(ans); } public void findSmallest(int l,int r,int x){ //System.out.println("In 4"); int curr=root[r]; int curl=root[l-1]; int countl=0,countr=0; // System.out.println(curl+" "+curr); for(int i=18;i>=0;i--){ int edge=(x&(1<<i))>0?1:0; // System.out.println(trie[curl][edge]+" "+trie[curr][edge]+" "+sz[curl]+" "+sz[curr]); if(edge==1){ countr+=sz[trie[curr][0]]; countl+=sz[trie[curl][0]]; } curr=trie[curr][edge]; curl=trie[curl][edge]; } countl+=sz[curl]; countr+=sz[curr]; System.out.println(countr-countl); } } class Printer{ public <T> T printArray(T obj[] ,String details){ System.out.println(details); for(int i=0;i<obj.length;i++) System.out.print(obj[i]+" "); System.out.println(); return obj[0]; } public <T> void print(T obj,String details){ System.out.println(details+" "+obj); } } class Node{ long weight; int vertex; public Node(int vertex,long weight){ this.vertex=vertex; this.weight=weight; } public String toString(){ return vertex+" "+weight; } } class Graph{ int nv; //0 indexing i.e vertices starts from 0 input as 1 indexed for add Edge List<List<Node>> adj; boolean visited[]; public Graph(int n){ adj=new ArrayList<>(); this.nv=n; // visited=new boolean[nv]; for(int i=0;i<n;i++) adj.add(new ArrayList<Node>()); } public void addEdge(int u,int v,long weight){ u--;v--; Node first=new Node(v,weight); Node second=new Node(u,weight); adj.get(v).add(second); adj.get(u).add(first); } public void dfscheck(int u,long curweight){ visited[u]=true; for(Node i:adj.get(u)){ if(visited[i.vertex]==false&&(i.weight|curweight)==curweight) dfscheck(i.vertex,curweight); } } long maxweight; public void clear(){ this.adj=null; this.nv=0; } public void solve() { maxweight=(1l<<32)-1; dfsutil(31); System.out.println(maxweight); } public void dfsutil(int msb){ if(msb<0)return; maxweight-=(1l<<msb); visited=new boolean[nv]; dfscheck(0,maxweight); for(int i=0;i<nv;i++) { if(visited[i]==false) {maxweight+=(1<<msb); break;} } dfsutil(msb-1); } // public boolean TopologicalSort() { // top=new int[nv]; // int cnt=0; // int indegree[]=new int[nv]; // for(int i=0;i<nv;i++){ // for(int j:adj.get(i)){ // indegree[j]++; // } // } // Deque<Integer> q=new LinkedList<Integer>(); // for(int i=0;i<nv;i++){ // if(indegree[i]==0){ // q.addLast(i); // } // } // while(q.size()>0){ // int tele=q.pop(); // top[tele]=cnt++; // for(int j:adj.get(tele)){ // indegree[j]--; // if(indegree[j]==0) // q.addLast(j); // } // } // return cnt==nv; // } // public boolean isBipartiteGraph(){ // col=new Integer[nv]; // visited=new boolean[nv]; // for(int i=0;i<nv;i++){ // if(visited[i]==false){ // col[i]=0; // dfs(i); // } // } }
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 8
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
f674d18494d434e5e4adcd6adce7146f
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 Solution { public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringBuffer out = new StringBuffer(); int T = 1; OUTER: while (T-->0) { int n = in.nextInt(), p = in.nextInt(); Integer[] a = new Integer[n]; for(int i=0; i<n; i++) { a[i] = in.nextInt(); } Arrays.sort(a); HashSet<Integer> set = new HashSet<>(); INNER: for(int item: a) { int item_ = item; while(item>0) { if(set.contains(item)) continue INNER; if(item%2==1) { item = (item-1)/2; } else if(item%4==0) { item /= 4; } else { break; } } set.add(item_); } int[] dp = new int[p+1]; for(int item: set) { int pos = nearestPow2(item); if(pos<=p) dp[pos] += 1; } // System.out.println(Arrays.toString(dp)); int total = 0; for(int i=0; i<=p; i++) { if(i>=2) { dp[i] = (dp[i] + dp[i-1] + dp[i-2])%1_000_000_007; } total = (total + dp[i])%1_000_000_007; } // System.out.println(Arrays.toString(dp)); out.append(total+"\n"); } System.out.print(out); } private static int nearestPow2(int n) { int cnt=0; while(n!=0) { n/=2; cnt+=1; } return cnt; } private static int countBit(int n) { int cnt = 0; while(n!=0) { n/=2; cnt+=1; } return cnt; } private static long gcd(long a, long b) { if (a==0) return b; return gcd(b%a, a); } // private static <T> void printArr(T arr) { // println(Arrays.toString(arr)); // } private static int toInt(String s) { return Integer.parseInt(s); } private static long toLong(String s) { return Long.parseLong(s); } private static void print(String s) { System.out.print(s); } private static void println(String s) { System.out.println(s); } }
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 8
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
48ff4c46b6e1640b65daf89f89ebfd80
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 Hiking { static Scanner sc; static PrintWriter pw; static int mod=(int)1e9+7; public static void sort(int[]in) { shuffle(in); Arrays.sort(in); } public static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } public static void main(String[] args) throws IOException, InterruptedException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int maxN=(int)2e5+5; long[] fib=new long[maxN+1]; fib[0]=fib[1]=1; for(int i=2;i<=maxN;i++) { fib[i]=fib[i-1]+fib[i-2]; fib[i]%=mod; } int n=sc.nextInt(); int p=sc.nextInt(); int []arr=sc.nextIntArray(n); sort(arr); HashSet<Integer> done=new HashSet<>(); long ans=0; for(int i=0;i<n;i++) { int temp=arr[i]; int bits=(int)(Math.log(arr[i])/Math.log(2))+1; boolean f=true; while(temp>0) { if(done.contains(temp)) { f=false; break; } if((temp&1)==1) temp>>=1; else if(temp%4==0) { temp>>=2; } else { break; } } if(f) { done.add(arr[i]); if(p-bits>=0) { ans+=fib[p-bits+2]-1; ans%=mod; } } } pw.println(ans); pw.flush(); } static class pair { // long x,y; int x, 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 Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } 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 String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { 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
["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 8
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
02ef3d14616dc39b75ffba39c9d837d9
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 cravuri on 2/21/22 */ import java.util.Arrays; import java.util.Scanner; public class D { static final long MOD = (long) 1e9 + 7; static int numDigits(int a) { int count = 0; while (a != 0) { a = a >> 1; count++; } return count; } static int binSearch(int[] a, int lo, int hi, int val) { while (lo <= hi) { int mi = (lo + hi) / 2; if (a[mi] == val) { return mi; } else if (a[mi] < val) { lo = mi + 1; } else { hi = mi - 1; } } return -1; } static boolean isFib(int val, int pow) { int curZeros = 0; for (int i = 0; i < pow; i++) { int dig = val % 2; val = val >> 1; if (dig == 0) curZeros++; else { if (curZeros % 2 != 0) { return false; } curZeros = 0; } } return curZeros % 2 == 0; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int P = sc.nextInt(); int[] a = new int[N]; for (int i = 0; i < N; i++) { a[i] = sc.nextInt(); } Arrays.sort(a); // precalculate all fibonacci numbers, and sum of fibs long[] fib = new long[P + 1]; long[] sum = new long[P + 1]; fib[0] = 1; sum[0] = 1; fib[1] = 1; sum[1] = 2; for (int i = 2; i < fib.length; i++) { fib[i] = (fib[i - 1] + fib[i - 2]) % MOD; sum[i] = (fib[i] + sum[i - 1]) % MOD; } long count = 0; for (int i = 0; i < N; i++) { // check if a[i] overlaps with a previous element int val = a[i]; int pow = 1; boolean overlap = false; while (val != 0) { int remainder = a[i] % (1 << pow); val = val >> 1; if (isFib(remainder, pow) && binSearch(a, 0, i - 1, val) != -1) { overlap = true; break; } pow++; } if (overlap) continue; // if not, add fibonacci number to count if (numDigits(a[i]) <= P) { count += sum[P - numDigits(a[i])]; count %= MOD; } } System.out.println(count); } }
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 8
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
e266ebc475e9c8633c6bb0fa5760cf79
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 Hiking { static Scanner sc; static PrintWriter pw; static int mod=(int)1e9+7; public static void sort(int[]in) { shuffle(in); Arrays.sort(in); } public static void shuffle(int[]in) { for(int i=0;i<in.length;i++) { int idx=(int)(Math.random()*in.length); int tmp=in[i]; in[i]=in[idx]; in[idx]=tmp; } } public static void main(String[] args) throws IOException, InterruptedException { sc = new Scanner(System.in); pw = new PrintWriter(System.out); int maxN=(int)2e5+5; long[] fib=new long[maxN+1]; fib[0]=fib[1]=1; for(int i=2;i<=maxN;i++) { fib[i]=fib[i-1]+fib[i-2]; fib[i]%=mod; } int n=sc.nextInt(); int p=sc.nextInt(); int []arr=sc.nextIntArray(n); sort(arr); HashSet<Integer> done=new HashSet<>(); long ans=0; for(int i=0;i<n;i++) { int temp=arr[i]; int bits=(int)(Math.log(arr[i])/Math.log(2))+1; boolean f=true; while(temp>0) { if(done.contains(temp)) { f=false; break; } if((temp&1)==1) temp>>=1; else if(temp%4==0) { temp>>=2; } else { break; } } if(f) { done.add(arr[i]); if(p-bits>=0) { ans+=fib[p-bits+2]-1; ans%=mod; } } } pw.println(ans); pw.flush(); } static class pair { // long x,y; int x, 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 Long(x).hashCode() * 31 + new Long(y).hashCode(); } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader r) { br = new BufferedReader(r); } 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 String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { 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
["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 8
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
3f0c9305e4508b74ccf31acdedb1ebcf
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.*; public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int n = cin.nextInt(); int p = cin.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = cin.nextInt(); } System.out.println(new Solution().func(a, p)); } } class Solution { public long func(int[] a, int p) { long mod = (long) (1e9 + 7); Set<Integer> set = new HashSet<>(); for (int e : a) { set.add(e); } cc: for (int e : a) { int t = e; while (t > 0) { if (t % 4 == 0) { t /= 4; } else if (t % 2 == 1) { t = (t - 1) / 2; } else { continue cc; } if (set.contains(t)) { set.remove(e); continue cc; } } } List<Integer> list = new ArrayList<>(set); list.sort(Comparator.comparingInt(o -> o)); long[] dp = new long[p + 1]; int j = 0; for (int i = 1; i < dp.length; i++) { while (j < list.size()) { Integer v = list.get(j); if (v < 1 << i) { dp[i]++; j++; } else { break; } } long t = 0; if (i > 1) { t = dp[i - 2]; } dp[i] += dp[i - 1] + t; dp[i] %= mod; } long cnt = 0; for (long l : dp) { cnt += l; cnt %= mod; } return cnt; } }
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 8
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
4289a54da1957f8db0336d8a088f9afb
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.lang.*; import java.io.*; public class Codechef { static long fans[] = new long[200001]; static long inv[] = new long[200001]; static long mod = 1000000007; static void init() { fans[0] = 1; inv[0] = 1; fans[1] = 1; inv[1] = 1; for (int i = 2; i < 200001; i++) { fans[i] = ((long) i * fans[i - 1]) % mod; inv[i] = power(fans[i], mod - 2); } } static long ncr(int n, int r) { return (((fans[n] * inv[n - r]) % mod) * (inv[r])) % mod; } public static void main(String[] args) throws java.lang.Exception { FastReader in = new FastReader(System.in); StringBuilder sb = new StringBuilder(); int t = 1; //t = in.nextInt(); while (t > 0) { --t; int n = in.nextInt(); int p = in.nextInt(); TreeSet<Integer> set = new TreeSet<>(); for(int i = 0;i<n;i++) set.add(in.nextInt()); TreeSet<Integer> fset = new TreeSet<>(); for(int e : set) { int k = e; boolean can = false; while((k%2 == 1 && k>1) || (k%4 == 0 && k>=4)) { if(k%4 == 0) { k/=4; } else k/=2; if(set.contains(k)) { can = true; break; } } if(!can) fset.add(e); } long dp[] = new long[p+1]; long ans = 0; for(int i = 0;i<p;i++) { if(i<35) { long bound = (1l<<(i+1)); while(fset.size()>0 && fset.first()<bound) { ++dp[i]; fset.pollFirst(); } } if(i-1>=0) dp[i]+=dp[i-1]; if(i-2>=0) dp[i]+=dp[i-2]; dp[i]%=mod; ans = (ans+dp[i])%mod; } sb.append(ans+"\n"); } System.out.print(sb); } static long power(long x, long y) { long res = 1; // Initialize result while (y > 0) { // If y is odd, multiply x with result if ((y & 1) != 0) res = ((res % mod) * (x % mod)) % mod; // y must be even now y = y >> 1; // y = y/2 x = ((x % mod) * (x % mod)) % mod; // Change x to x^2 } return res; } static long[] generateArray(FastReader in, int n) throws IOException { long arr[] = new long[n]; for (int i = 0; i < n; i++) arr[i] = in.nextLong(); return arr; } static long[][] generatematrix(FastReader in, int n, int m) throws IOException { long arr[][] = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = in.nextLong(); } } return arr; } static long gcd(long a, long b) { if (a == 0) return b; else return gcd(b % a, a); } static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } 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); } } 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(); } String nextLine() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; StringBuilder sb = new StringBuilder(); for (; c != 10 && c != 13; c = scan()) { sb.append((char) c); } return sb.toString(); } char nextChar() throws IOException { int c; for (c = scan(); c <= 32; c = scan()) ; return (char) c; } 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; } }
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 8
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
354519bfb3e0c3dad3f5d983d60a4a16
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.*; import javafx.util.*; public class Solution { 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') { if (cnt != 0) { break; } else { continue; } } 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 int st[]; static int sz = 1; static int max(int l , int r) { return max(0,sz-1,l,r,0); } static int max(int lx , int rx , int l , int r , int x) { if(lx > r || rx < l) return -1; if(lx >= l && rx <= r) return st[x]; int mid = (lx+rx)/2; return Math.max(max(lx,mid,l,r,2*x+1),max(mid+1,rx,l,r,2*x+2)); } static void update(int i , int v) { update(0,sz-1,i,v,0); } static void update(int lx , int rx , int i , int v , int x) { if(i < lx || i > rx) return; if(lx == rx) { st[x] = v; return; } int mid = (lx+rx)/2; update(lx,mid,i,v,2*x+1); update(mid+1,rx,i,v,2*x+2); st[x] = Math.max(st[2*x+1],st[2*x+2]); } static void change(int l , int r , int v, long ans[]) { if(l > r) return; ans[l] += v; if(r+1 > ans.length-1) return; ans[r+1] -= v; } static long mod = 1000000007; static long pow(long a , long b) { if(b == 0) return 1; long ans = pow(a,b/2); if(b%2 == 0) return ans*ans%mod; return ans*ans%mod*a%mod; } public static void main(String []args) throws IOException { Reader sc = new Reader(); int n = sc.nextInt(); int p = sc.nextInt(); ArrayList<Integer> ls = new ArrayList<Integer>(); int arr[] = new int[n]; HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i = 0 ; i < n ; i++) { arr[i] = sc.nextInt(); map.put(arr[i],1); } for(int i = 0 ; i < n ; i++) { boolean is = true; int x = arr[i]; while(arr[i] > 0) { // int x = arr[i]; if(arr[i]%2 == 1) { arr[i] = (arr[i]-1)/2; if(map.containsKey(arr[i])) is = false; } else { if(arr[i]%4 == 0) { arr[i] /= 4; if(map.containsKey(arr[i])) is = false; } else { break; } } if(!is) break; } if(is) ls.add(x); } int cnt[] = new int[p]; long dp[] = new long[p]; long ans = 0; long mod = 1000000007; for(Integer x : ls) { int msb = 0; for(int i = 0 ; i <= 30 ; i++) { if((x&(1<<i)) > 0) msb = i; } if(msb < p) cnt[msb]++; } for(int i = 0 ; i < p ; i++) { if(i > 0) dp[i] += dp[i-1]; if(i > 1) dp[i] += dp[i-2]; dp[i] %= mod; dp[i] += cnt[i]; dp[i] %= mod; ans += dp[i]; 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 8
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
2ecbd790bd8b0dd107ef042dce11fd69
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 final class D { public static void main(String[] args) { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int n = fs.nextInt(); int p = fs.nextInt(); int[] a = fs.readArray(n); sort(a); int[] dp = new int[p+ 1]; dp[0] = 1; dp[1] = 1; for(int i = 2; i <= p; ++i){ dp[i] =(int) add(dp[i-1], dp[i-2]); } for(int i = 1; i <= p; ++i){ dp[i] = (int)add(dp[i] , dp[i-1]); } int cnt = 0; HashSet<Integer> set= new HashSet<>(); for(int i = 0; i <n; ++i){ if(present(a[i],set)) continue;; set.add(a[i]); int len = p - msb(a[i]); if(len < 0) continue; cnt = (int) add(cnt, dp[len]); } System.out.println(cnt); } static boolean present(int x, HashSet<Integer> set){ while (x != 0){ if(set.contains(x)) return true; if((x & 1) == 1){ x >>= 1; }else if(x % 4 != 0) return false; else{ x >>= 2; } } return false; } static int msb(int n){ for(int i = 31; i >= 0; --i){ if(((n >> i) & 1) != 0) return i + 1; } return 1; } static final Random random = new Random(); static final int mod = 1_000_000_007; static long add(long a, long b) { return (a + b) % mod; } static long sub(long a, long b) { return ((a - b) % mod + mod) % mod; } 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 long mul(long a, long b) { return (a * b) % mod; } static long exp(long base, long exp) { if (exp == 0) return 1; long half = exp(base, exp / 2); if (exp % 2 == 0) return mul(half, half); return mul(half, mul(half, base)); } static long[] factorials = new long[2_000_001]; static long[] invFactorials = new long[2_000_001]; static void precompFacts() { factorials[0] = invFactorials[0] = 1; for (int i = 1; i < factorials.length; i++) factorials[i] = mul(factorials[i - 1], i); invFactorials[factorials.length - 1] = exp(factorials[factorials.length - 1], mod - 2); for (int i = invFactorials.length - 2; i >= 0; i--) invFactorials[i] = mul(invFactorials[i + 1], i + 1); } static long nCk(int n, int k) { return mul(factorials[n], mul(invFactorials[k], invFactorials[n - k])); } 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 8
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
46d0951259f695d5d686f4a4c39ab260
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.*; // BEFORE 31ST MARCH 2022 !! //MAX RATING EVER ACHIEVED-1622(LETS SEE WHEN WILL I GET TO CHANGE THIS) ////*************************************************************************** /* public class E_Gardener_and_Tree implements Runnable{ public static void main(String[] args) throws Exception { new Thread(null, new E_Gardener_and_Tree(), "E_Gardener_and_Tree", 1<<28).start(); } public void run(){ WRITE YOUR CODE HERE!!!! JUST WRITE EVERYTHING HERE WHICH YOU WRITE IN MAIN!!! } } */ /////************************************************************************** public class D_Infinite_Set{ public static void main(String[] args) { FastScanner s= new FastScanner(); //PrintWriter out=new PrintWriter(System.out); //end of program //out.println(answer); //out.close(); StringBuilder res = new StringBuilder(); long mod=power(10,9)+7; int n=s.nextInt(); int p=s.nextInt(); long array[]= new long[n]; HashMap<Long,Long> map = new HashMap<Long,Long>(); for(int i=0;i<n;i++){ array[i]=s.nextLong(); map.put(array[i],1L); } long well[]= new long[p+4]; well[0]=1; well[1]=2; for(int i=2;i<=p;i++){ well[i]=(well[i-1]+well[i-2]+1)%mod; } int check[]= new int[n]; for(int i=0;i<n;i++){ long num=array[i]; int flag=0; while(num>0){ if((num&1)==1){ num=num>>1L; if(map.containsKey(num)){ flag=1; break; } } else{ num=num>>1L; if((num&1)==1){ break; } else{ num=num>>1L; if(map.containsKey(num)){ flag=1; break; } } } } if(flag==1){ check[i]=1; } } // { // //printing // for(int i=0;i<=p;i++){ // System.out.print(well[i]+" "); // } // System.out.println(); // } long ans=0; for(int i=0;i<n;i++){ if(check[i]==1){ continue; } long num=array[i]; int count=0; while(num>0){ count++; num=num>>1L; } /// System.out.println("count "+count); if(count<=p){ int hh=p-count; ans=(ans+well[hh])%mod; } } res.append(ans+" \n"); System.out.println(res); } static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } 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(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } static long modpower(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; } // SIMPLE POWER FUNCTION=> static long power(long x, long y) { long res = 1; // Initialize result 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/2 x = x * x; // Change x to x^2 } return res; } }
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 8
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
4450046378dea9b83323731980357314
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 D1635 { static int mod = (int) 1e9 + 7; public static void main(String[] args) throws IOException { Scanner sc = new Scanner(System.in); PrintWriter pw = new PrintWriter(System.out); long[] fib = new long[(int) 1e6]; fib[0] = 1; fib[1] = 1; for (int i = 2; i < fib.length; i++) { fib[i] = (fib[i - 1] + fib[i - 2]) % mod; } for (int i = 1; i < fib.length; i++) { fib[i] = (fib[i - 1] + fib[i]) % mod; } int n = sc.nextInt(); int p = sc.nextInt(); int[] a = sc.nextIntArr(n); HashSet<Integer> hs = new HashSet<>(); for (int x : a) hs.add(x); long ans = 0; loop: for (int x : a) { int ogx = x; for (int i = 0; i < 32; i++) { if (x % 4 == 0) { x /= 4; } else if (x % 2 == 1) { x /= 2; } else { break; } if (hs.contains(x)) { continue loop; } } x = ogx; int bits = 0; while (x > 0) { x /= 2; bits++; } if (p - bits >= 0) ans += fib[p - bits]; ans %= mod; } pw.println(ans); pw.close(); } static class Scanner { BufferedReader br; StringTokenizer st; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public Scanner(FileReader f) { br = new BufferedReader(f); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public double nextDouble() throws IOException { return Double.parseDouble(next()); } public int[] nextIntArr(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = Integer.parseInt(next()); } return arr; } } }
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 8
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
ff38f55a47170be1554e587cdfbcca3d
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 D { // @formatter:off static long mod=(int)(1e9)+7; static FastScanner in; static class FastScanner{ private InputStream stream; private byte[] buf = new byte[1 << 16]; private int curChar; private int numChars; // standard input public FastScanner() { this(System.in); } public FastScanner(InputStream i) { stream = i; } // file input public FastScanner(String i) throws IOException { 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 int nextInt() { // nextLong() would be implemented similarly 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; } public double nextDouble() { return Double.parseDouble(next()); } } static void setIO() {try {in=new FastScanner("test.in");}catch(Exception e) {in=new FastScanner(System.in);}}// @formatter:on public static void main(String[] args) { setIO(); 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); Set<Integer>starting=new HashSet<>(); for(int val:a)starting.add(val); for(int i=n-1;i>=0;i--) { if(dfs(a[i],starting,0)) { starting.remove(a[i]); } } Long[]dp=new Long[p]; // up to p dp[p-1]=1l; if(p-2>=0) dp[p-2]=2l; solve(0,dp); long ret=0; for(int start:starting) { int log=log2(start); if(log>=p) continue; ret+=dp[log]; ret%=mod; } System.out.println(ret); } static boolean dfs(int curr, Set<Integer>values,int it) { if(it!=0&&values.contains(curr)) return true; if(curr<=0) return false; if(curr%2==1) return dfs(curr/2,values,it+1); else if(curr%4==0) return dfs(curr/4,values,it+1); return false; } static int log2(int a) { int ret=0; while(a>1) { a/=2; ret++; } return ret; } static long solve(int curr, Long[]dp) { if(curr>=dp.length) return 0; if(dp[curr]!=null) return dp[curr]; dp[curr]=(1+solve(curr+1,dp)+solve(curr+2,dp))%mod; return dp[curr]; } }
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
158aab31cee643d3adf8900644416b99
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 codeforces_772_D { private static long mod = 1_000_000_007; private static long[] fib = getFib(200_001); private static long[] sumFib = getSumFib(200_001); private static long[] getSumFib(int sz) { var ans = new long[sz]; ans[0] = fib[0]; for (int i = 1; i < sz; i++) { ans[i] = (fib[i] + ans[i - 1]) % mod; } return ans; } private static long[] getFib(int sz) { var ans = new long[sz]; ans[0] = 1; ans[1] = 1; for (int i = 2; i < sz; i++) { ans[i] = (ans[i - 1] + ans[i - 2]) % mod; } return ans; } private static void solve(FastIOAdapter in, PrintWriter out) { int n = in.nextInt(); int p = in.nextInt(); int[] a = in.readArray(n); HashSet<Integer> unique = new HashSet<>(); for (int i = 0; i < n; i++) { unique.add(a[i]); } for (int i = 0; i < n; i++) { int cur = a[i]; while (cur > 0) { if (cur % 2 == 1) { cur /= 2; } else if (cur % 4 == 0) { cur /= 4; } else break; if (unique.contains(cur)) { unique.remove(a[i]); break; } } } long ans = 0; for (Integer cur : unique) { int i = Integer.highestOneBit(cur); int ind = -1; for (int j = 0; j < 32; j++) { if (1 << j == i) { ind = j; break; } } if (p > ind) { int len = p - ind - 1; ans = (ans + sumFib[len]) % mod; } } out.println(ans); } public static void main(String[] args) throws Exception { try (FastIOAdapter ioAdapter = new FastIOAdapter()) { int count = 1; //count = ioAdapter.nextInt(); while (count-- > 0) { solve(ioAdapter, ioAdapter.out); } } } static void ruffleSort(int[] arr) { int n = arr.length; Random rnd = new Random(); for (int i = 0; i < n; ++i) { int tmp = arr[i]; int randomPos = i + rnd.nextInt(n - i); arr[i] = arr[randomPos]; arr[randomPos] = tmp; } Arrays.sort(arr); } static class FastIOAdapter implements AutoCloseable { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out)))); StringTokenizer st = new StringTokenizer(""); String next() { while (!st.hasMoreTokens()) try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } return st.nextToken(); } String nextLine() { try { return br.readLine(); } catch (IOException e) { e.printStackTrace(); return null; } } 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[] readArrayLong(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()); } @Override public void close() throws Exception { out.flush(); out.close(); br.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
fb4e9dff39313e97db9bede14bd28080
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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class TaskD { private static long MOD = 1_000_000_007L; public static void main(String[] arg) { final FastScanner in = new FastScanner(System.in); final PrintWriter out = new PrintWriter(System.out); final int n = in.nextInt(); final int p = in.nextInt(); final int[] a = in.readIntArr(n); final Solution sol = new Solution(n, p, a); out.println(sol.solution()); out.flush(); out.close(); in.close(); } private static class Solution { final int n; final int p; final int[] a; public Solution(int n, int p, int[] a) { this.n = n; this.p = p; this.a = a; } private long solution() { long[] dp = new long[p + 128]; shuffle(a); Arrays.sort(a); Set<Integer> acc = new HashSet<>(); for (int i = 0; i < a.length; i++) { int parent = a[i]; while (parent > 0 && (parent % 4 == 0 || parent % 2 == 1) && acc.contains(parent) == false) { if (parent % 4 == 0) { parent = parent / 4; } else if (parent % 2 == 1) { parent = (parent - 1) / 2; } else { break; } } if (acc.contains(parent)) { continue; } acc.add(a[i]); int size = 0; int nn = a[i]; while (nn > 0) { size++; nn /= 2; } dp[size]++; } long sum = 0; sum += dp[0]; for (int i = 1; i <= p; i++) { dp[i] += dp[i - 1]; if (i >= 2) { dp[i] += dp[i - 2]; } dp[i] %= MOD; sum += dp[i]; sum %= MOD; } return sum; } } public static String arrToString(final int[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { sb.append(a[i]).append(' '); } return sb.toString(); } public static String arrToString(final long[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { sb.append(a[i]).append(' '); } return sb.toString(); } public static void shuffle(int[] nums) { for (int i = 0; i < nums.length; i++) { int idx = (int) ((nums.length - i) * Math.random()) + i; int t = nums[idx]; nums[idx] = nums[i]; nums[i] = t; } } public static int max(int[] a) { int max = a[0]; for (int i = 0; i < a.length; i++) { if (max < a[i]) { max = a[i]; } } return max; } public static int gcd(int a, int b) { int t; while (b > 0) { a %= b; t = a; a = b; b = t; } return a; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArr(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = Integer.parseInt(next()); } return result; } long[] readLongArr(int n) { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = Long.parseLong(next()); } return result; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } 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
f8a3e33a79c0fe2af44b71b20caf20a1
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.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.*; public class TaskD { private static long MOD = 1_000_000_007L; public static void main(String[] arg) { final FastScanner in = new FastScanner(System.in); final PrintWriter out = new PrintWriter(System.out); final int n = in.nextInt(); final int p = in.nextInt(); final int[] a = in.readIntArr(n); final Solution sol = new Solution(n, p, a); out.println(sol.solution()); out.flush(); out.close(); in.close(); } private static class Solution { final int n; final int p; final int[] a; public Solution(int n, int p, int[] a) { this.n = n; this.p = p; this.a = a; } private long solution() { long[] dp = new long[p + 128]; shuffle(a); Arrays.sort(a); Set<Integer> acc = new HashSet<>(); for (int i = 0; i < a.length; i++) { int parent = a[i]; boolean skip = false; while (parent > 0 && (parent % 4 == 0 || parent % 2 == 1)) { if (acc.contains(parent)) { skip = true; break; } if (parent % 4 == 0) { parent = parent / 4; } else if (parent % 2 == 1) { parent = (parent - 1) / 2; } else { break; } if (acc.contains(parent)) { skip = true; break; } } acc.add(a[i]); if (skip) { continue; } int size = 0; int nn = a[i]; while (nn > 0) { size++; nn /= 2; } dp[size]++; } long sum = 0; sum += dp[0]; for (int i = 1; i <= p; i++) { dp[i] += dp[i - 1]; if (i >= 2) { dp[i] += dp[i - 2]; } dp[i] %= MOD; sum += dp[i]; sum %= MOD; } return sum; } } public static String arrToString(final int[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { sb.append(a[i]).append(' '); } return sb.toString(); } public static String arrToString(final long[] a) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < a.length; i++) { sb.append(a[i]).append(' '); } return sb.toString(); } public static void shuffle(int[] nums) { for (int i = 0; i < nums.length; i++) { int idx = (int) ((nums.length - i) * Math.random()) + i; int t = nums[idx]; nums[idx] = nums[i]; nums[i] = t; } } public static int max(int[] a) { int max = a[0]; for (int i = 0; i < a.length; i++) { if (max < a[i]) { max = a[i]; } } return max; } public static int gcd(int a, int b) { int t; while (b > 0) { a %= b; t = a; a = b; b = t; } return a; } private static class FastScanner { BufferedReader br; StringTokenizer st; FastScanner(InputStream stream) { try { br = new BufferedReader(new InputStreamReader(stream)); } catch (Exception e) { e.printStackTrace(); } } String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } int[] readIntArr(int n) { int[] result = new int[n]; for (int i = 0; i < n; i++) { result[i] = Integer.parseInt(next()); } return result; } long[] readLongArr(int n) { long[] result = new long[n]; for (int i = 0; i < n; i++) { result[i] = Long.parseLong(next()); } return result; } void close() { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } 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
41938f52e0b208b4af8cf2596d147186
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 _1635_D { static long MOD = 1000000007; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); StringTokenizer line = new StringTokenizer(in.readLine()); int n = Integer.parseInt(line.nextToken()); int p = Integer.parseInt(line.nextToken()); long[] dp = new long[p + 1]; long[] sum = new long[p + 1]; dp[0] = 1; dp[1] = 1; sum[0] = 1; sum[1] = 2; for(int i = 2; i < p + 1; i++) { dp[i] = modadd(dp[i - 1], dp[i - 2]); sum[i] = modadd(sum[i - 1], dp[i]); } int[] a = new int[n]; HashSet<Integer> seta = new HashSet<Integer>(); line = new StringTokenizer(in.readLine()); for(int i = 0; i < n; i++) { a[i] = Integer.parseInt(line.nextToken()); seta.add(a[i]); } long res = 0; for(int i = 0; i < n; i++) { int val = a[i]; boolean repeat = false; while(val > 0 && (val % 4 == 0 || val % 2 == 1)) { if(val % 4 == 0) { val /= 4; }else if(val % 2 == 1) { val /= 2; } if(seta.contains(val)) { repeat = true; break; } } int digits = 0; val = a[i]; while(val > 0) { digits++; val /= 2; } if(!repeat && p >= digits) { res = modadd(res, sum[p - digits]); } } out.println(res); in.close(); out.close(); } static long modadd(long a, long b) { if(a + b > MOD) { return a + b - MOD; } return a + b; } }
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
c65acb319db1a059bb9c30e44895ab8c
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 DD { { MULTI_TEST = false; FILE_NAME = ""; NEED_FILE_IO = false; INF = Long.MAX_VALUE; } //// fields int log(int x) { int log = 0; while(x > 0) { x /= 2; log++; } return log; } final int MOD = (int) 1e9 + 7; void solve() { int n = ri(), p = ri(); int[] a = ria(n); sort(a); int[] dp = new int[p]; TreeSet<Integer> was = new TreeSet<>(); for(int i = 0; i < n; i++) { int x = a[i]; while(true) { if(was.contains(x)) break; if(x % 2 == 1 && x >= 3) { x/=2; } else if(x % 4 == 0) { x/=4; } else { int index = log(a[i]) - 1; if(index < p) dp[index]++; was.add(a[i]); break; } } } for(int i = 0; i < p; i++) { try { dp[i] += dp[i - 1]; dp[i] %= MOD; dp[i] += dp[i - 2]; dp[i] %= MOD; } catch (Exception e) {} } long sum = 0; for(int i = 0; i < p; i++) { sum = (sum + dp[i]) % MOD; } out.println(sum); } public static void main(String[] args) { new DD().run(); } //main @SuppressWarnings("unused") long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } @SuppressWarnings("unused") long lcm(long a, long b) { return a / gcd(a, b) * b; } @SuppressWarnings("unused") int gcd(int a, int b) { return (int) gcd((long) a, b); } @SuppressWarnings("unused") int lcm(int a, int b) { return (int) lcm(a, (long) b); } @SuppressWarnings("unused") int sqrtInt(int x) { return (int) sqrtLong(x); } @SuppressWarnings("unused") long sqrtLong(long x) { long root = (long) Math.sqrt(x); while (root * root > x) --root; while ((root + 1) * (root + 1) <= x) ++root; return root; } @SuppressWarnings("unused") int cbrtLong(long x) { int cbrt = (int) Math.cbrt(x); while ((long) cbrt * cbrt >= x) { cbrt--; } while ((long) (cbrt + 1) * (cbrt + 1) <= x) cbrt++; return cbrt; } @SuppressWarnings("unused") long binpow(long a, long power) { return binpow(a, power, INF); } @SuppressWarnings("unused") long binpow(long a, long power, long modulo) { if (power == 0) return 1 % modulo; if (power % 2 == 1) { long b = binpow(a, power - 1, modulo) % modulo; return ((a % modulo) * b) % modulo; } else { long b = binpow(a, power / 2, modulo) % modulo; return (b * b) % modulo; } } @SuppressWarnings("unused") long fastMod(String s1, long n2) { long num = 0; for (int i = 0; i < s1.length() - 1; i++) { num += Integer.parseInt(String.valueOf(s1.charAt(i))); num *= 10; if (num >= n2) { num = num % n2; } } return (num + Integer.parseInt(String.valueOf(s1.charAt(s1.length() - 1)))) % n2; } @SuppressWarnings("unused") long factorialMod(long n, long p) { long res = 1; while (n > 1) { res = (res * ((n / p) % 2 == 1 ? p - 1 : 1)) % p; for (int i = 2; i <= n % p; ++i) res = (res * i) % p; n /= p; } return res % p; } @SuppressWarnings("unused") boolean isPrime(int number) { for (int i = 2; i * i <= number; i++) { if (number % i == 0) { return false; } } return number > 1; } @SuppressWarnings("unused") boolean[] primes(int border) { boolean[] isPrimes = new boolean[border + 1]; Arrays.fill(isPrimes, true); isPrimes[0] = false; isPrimes[1] = false; for (int i = 2; i < border + 1; i++) { if (!isPrimes[i]) continue; for (int k = i * 2; k < border; k += i) { isPrimes[k] = false; } } return isPrimes; } //Number theory @SuppressWarnings("unused") void sort(int[] a) { int n = a.length; Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < n; i++) { a[i] = arr[i]; } } @SuppressWarnings("unused") void sort(long[] a) { int n = a.length; Long[] arr = new Long[n]; for (int i = 0; i < n; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < n; i++) { a[i] = arr[i]; } } @SuppressWarnings("unused") int max(int[] a) { int n = a.length; int max = Integer.MIN_VALUE; for (int j : a) { if (j > max) max = j; } return max; } @SuppressWarnings("unused") long max(long[] a) { int n = a.length; long max = Long.MIN_VALUE; for (long l : a) { if (l > max) max = l; } return max; } @SuppressWarnings("unused") int maxIndex(int[] a) { int n = a.length; int max = Integer.MIN_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int maxIndex(long[] a) { int n = a.length; long max = Long.MIN_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int min(int[] a) { int n = a.length; int min = Integer.MAX_VALUE; for (int j : a) { if (j < min) min = j; } return min; } @SuppressWarnings("unused") int minIndex(int[] a) { int n = a.length; int min = Integer.MAX_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] < min) { min = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int minIndex(long[] a) { int n = a.length; long min = Long.MAX_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] < min) { min = a[i]; index = i; } } return index; } @SuppressWarnings("unused") long min(long[] a) { int n = a.length; long min = Long.MAX_VALUE; for (long l : a) { if (l < min) min = l; } return min; } @SuppressWarnings("unused") long sum(int[] a) { int n = a.length; long sum = 0; for (int j : a) { sum += j; } return sum; } @SuppressWarnings("unused") long sum(long[] a) { int n = a.length; long sum = 0; for (long l : a) { sum += l; } return sum; } //Arrays Operations @SuppressWarnings("unused") String readLine() { try { return in.readLine(); } catch (Exception e) { throw new InputMismatchException(); } } @SuppressWarnings("unused") String rs() { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(readLine()); } return tok.nextToken(); } @SuppressWarnings("unused") int ri() { return Integer.parseInt(rs()); } @SuppressWarnings("unused") long rl() { return Long.parseLong(rs()); } @SuppressWarnings("unused") double rd() { return Double.parseDouble(rs()); } @SuppressWarnings("unused") int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ri(); } return a; } @SuppressWarnings("unused") int[] riawd(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ri() - 1; } return a; } @SuppressWarnings("unused") long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = rl(); } return a; } @SuppressWarnings("unused") private boolean yesNo(boolean yes, String yesString, String noString) { out.println(yes ? yesString : noString); return yes; } //fastIO void run() { try { long start = System.currentTimeMillis(); initIO(); if (MULTI_TEST) { long t = rl(); while (t-- > 0) { solve(); } } else { solve(); } out.close(); System.err.println("Time(ms) - " + (System.currentTimeMillis() - start)); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void initConsoleIO() { out = new PrintWriter(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } void initFileIO(String inName, String outName) throws FileNotFoundException { out = new PrintWriter(outName); in = new BufferedReader(new FileReader(inName)); } void initIO() throws FileNotFoundException { if (!FILE_NAME.isEmpty()) { initFileIO(FILE_NAME + ".in", FILE_NAME + ".out"); } else { if (NEED_FILE_IO && new File("input.txt").exists()) { initFileIO("input.txt", "output.txt"); } else { initConsoleIO(); } } tok = new StringTokenizer(""); } //initIO private final String FILE_NAME; private final boolean MULTI_TEST; private final boolean NEED_FILE_IO; private final long INF; private BufferedReader in; private PrintWriter out; private StringTokenizer tok; //fields }
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
6472410a27ddb21bb45fba276f82bf3b
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.Arrays; import java.util.HashSet; import java.util.InputMismatchException; import java.util.StringTokenizer; public class DD { { MULTI_TEST = false; FILE_NAME = ""; NEED_FILE_IO = false; INF = Long.MAX_VALUE; } //// fields int log(int x) { int log = 0; while(x > 0) { x /= 2; log++; } return log; } final int MOD = (int) 1e9 + 7; void solve() { int n = ri(), p = ri(); int[] a = ria(n); sort(a); int[] dp = new int[p]; HashSet<Integer> was = new HashSet<>(); for(int i = 0; i < n; i++) { int x = a[i]; while(true) { if(was.contains(x)) break; if(x % 2 == 1 && x >= 3) { x/=2; } else if(x % 4 == 0) { x/=4; } else { int index = log(a[i]) - 1; if(index < p) dp[index]++; was.add(a[i]); break; } } } for(int i = 0; i < p; i++) { try { dp[i] += dp[i - 1]; dp[i] %= MOD; dp[i] += dp[i - 2]; dp[i] %= MOD; } catch (Exception e) {} } long sum = 0; for(int i = 0; i < p; i++) { sum = (sum + dp[i]) % MOD; } out.println(sum); } public static void main(String[] args) { new DD().run(); } //main @SuppressWarnings("unused") long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } @SuppressWarnings("unused") long lcm(long a, long b) { return a / gcd(a, b) * b; } @SuppressWarnings("unused") int gcd(int a, int b) { return (int) gcd((long) a, b); } @SuppressWarnings("unused") int lcm(int a, int b) { return (int) lcm(a, (long) b); } @SuppressWarnings("unused") int sqrtInt(int x) { return (int) sqrtLong(x); } @SuppressWarnings("unused") long sqrtLong(long x) { long root = (long) Math.sqrt(x); while (root * root > x) --root; while ((root + 1) * (root + 1) <= x) ++root; return root; } @SuppressWarnings("unused") int cbrtLong(long x) { int cbrt = (int) Math.cbrt(x); while ((long) cbrt * cbrt >= x) { cbrt--; } while ((long) (cbrt + 1) * (cbrt + 1) <= x) cbrt++; return cbrt; } @SuppressWarnings("unused") long binpow(long a, long power) { return binpow(a, power, INF); } @SuppressWarnings("unused") long binpow(long a, long power, long modulo) { if (power == 0) return 1 % modulo; if (power % 2 == 1) { long b = binpow(a, power - 1, modulo) % modulo; return ((a % modulo) * b) % modulo; } else { long b = binpow(a, power / 2, modulo) % modulo; return (b * b) % modulo; } } @SuppressWarnings("unused") long fastMod(String s1, long n2) { long num = 0; for (int i = 0; i < s1.length() - 1; i++) { num += Integer.parseInt(String.valueOf(s1.charAt(i))); num *= 10; if (num >= n2) { num = num % n2; } } return (num + Integer.parseInt(String.valueOf(s1.charAt(s1.length() - 1)))) % n2; } @SuppressWarnings("unused") long factorialMod(long n, long p) { long res = 1; while (n > 1) { res = (res * ((n / p) % 2 == 1 ? p - 1 : 1)) % p; for (int i = 2; i <= n % p; ++i) res = (res * i) % p; n /= p; } return res % p; } @SuppressWarnings("unused") boolean isPrime(int number) { for (int i = 2; i * i <= number; i++) { if (number % i == 0) { return false; } } return number > 1; } @SuppressWarnings("unused") boolean[] primes(int border) { boolean[] isPrimes = new boolean[border + 1]; Arrays.fill(isPrimes, true); isPrimes[0] = false; isPrimes[1] = false; for (int i = 2; i < border + 1; i++) { if (!isPrimes[i]) continue; for (int k = i * 2; k < border; k += i) { isPrimes[k] = false; } } return isPrimes; } //Number theory @SuppressWarnings("unused") void sort(int[] a) { int n = a.length; Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < n; i++) { a[i] = arr[i]; } } @SuppressWarnings("unused") void sort(long[] a) { int n = a.length; Long[] arr = new Long[n]; for (int i = 0; i < n; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < n; i++) { a[i] = arr[i]; } } @SuppressWarnings("unused") int max(int[] a) { int n = a.length; int max = Integer.MIN_VALUE; for (int j : a) { if (j > max) max = j; } return max; } @SuppressWarnings("unused") long max(long[] a) { int n = a.length; long max = Long.MIN_VALUE; for (long l : a) { if (l > max) max = l; } return max; } @SuppressWarnings("unused") int maxIndex(int[] a) { int n = a.length; int max = Integer.MIN_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int maxIndex(long[] a) { int n = a.length; long max = Long.MIN_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int min(int[] a) { int n = a.length; int min = Integer.MAX_VALUE; for (int j : a) { if (j < min) min = j; } return min; } @SuppressWarnings("unused") int minIndex(int[] a) { int n = a.length; int min = Integer.MAX_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] < min) { min = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int minIndex(long[] a) { int n = a.length; long min = Long.MAX_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] < min) { min = a[i]; index = i; } } return index; } @SuppressWarnings("unused") long min(long[] a) { int n = a.length; long min = Long.MAX_VALUE; for (long l : a) { if (l < min) min = l; } return min; } @SuppressWarnings("unused") long sum(int[] a) { int n = a.length; long sum = 0; for (int j : a) { sum += j; } return sum; } @SuppressWarnings("unused") long sum(long[] a) { int n = a.length; long sum = 0; for (long l : a) { sum += l; } return sum; } //Arrays Operations @SuppressWarnings("unused") String readLine() { try { return in.readLine(); } catch (Exception e) { throw new InputMismatchException(); } } @SuppressWarnings("unused") String rs() { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(readLine()); } return tok.nextToken(); } @SuppressWarnings("unused") int ri() { return Integer.parseInt(rs()); } @SuppressWarnings("unused") long rl() { return Long.parseLong(rs()); } @SuppressWarnings("unused") double rd() { return Double.parseDouble(rs()); } @SuppressWarnings("unused") int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ri(); } return a; } @SuppressWarnings("unused") int[] riawd(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ri() - 1; } return a; } @SuppressWarnings("unused") long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = rl(); } return a; } @SuppressWarnings("unused") private boolean yesNo(boolean yes, String yesString, String noString) { out.println(yes ? yesString : noString); return yes; } //fastIO void run() { try { long start = System.currentTimeMillis(); initIO(); if (MULTI_TEST) { long t = rl(); while (t-- > 0) { solve(); } } else { solve(); } out.close(); System.err.println("Time(ms) - " + (System.currentTimeMillis() - start)); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void initConsoleIO() { out = new PrintWriter(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } void initFileIO(String inName, String outName) throws FileNotFoundException { out = new PrintWriter(outName); in = new BufferedReader(new FileReader(inName)); } void initIO() throws FileNotFoundException { if (!FILE_NAME.isEmpty()) { initFileIO(FILE_NAME + ".in", FILE_NAME + ".out"); } else { if (NEED_FILE_IO && new File("input.txt").exists()) { initFileIO("input.txt", "output.txt"); } else { initConsoleIO(); } } tok = new StringTokenizer(""); } //initIO private final String FILE_NAME; private final boolean MULTI_TEST; private final boolean NEED_FILE_IO; private final long INF; private BufferedReader in; private PrintWriter out; private StringTokenizer tok; //fields }
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
8b9de0e36b38085d192cc6f6bceaa298
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 com.sun.source.tree.Tree; import java.io.*; import java.util.*; public class DD { { MULTI_TEST = false; FILE_NAME = ""; NEED_FILE_IO = false; INF = Long.MAX_VALUE; } //// fields int log(int x) { int log = 0; while(x > 0) { x /= 2; log++; } return log; } final int MOD = (int) 1e9 + 7; void solve() { int n = ri(), p = ri(); int[] a = ria(n); sort(a); int[] dp = new int[p]; TreeSet<Integer> was = new TreeSet<>(); //O(n) for(int i = 0; i < n; i++) { int x = a[i]; while(true) { if(was.contains(x)) break; if(x % 2 == 1 && x >= 3) { //log(A), A = 1e9 x/=2; } else if(x % 4 == 0) { x/=4; } else { try { dp[log(a[i]) - 1]++; //log(A), A = 1e9 } catch (Exception e){} was.add(a[i]); break; } } } //O(p) for(int i = 0; i < p; i++) { try { dp[i] += dp[i - 1]; dp[i] %= MOD; dp[i] += dp[i - 2]; dp[i] %= MOD; } catch (Exception e) {} } long sum = 0; for(int i = 0; i < p; i++) { sum = (sum + dp[i]) % MOD; } out.println(sum); } public static void main(String[] args) { new DD().run(); } //main @SuppressWarnings("unused") long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } @SuppressWarnings("unused") long lcm(long a, long b) { return a / gcd(a, b) * b; } @SuppressWarnings("unused") int gcd(int a, int b) { return (int) gcd((long) a, b); } @SuppressWarnings("unused") int lcm(int a, int b) { return (int) lcm(a, (long) b); } @SuppressWarnings("unused") int sqrtInt(int x) { return (int) sqrtLong(x); } @SuppressWarnings("unused") long sqrtLong(long x) { long root = (long) Math.sqrt(x); while (root * root > x) --root; while ((root + 1) * (root + 1) <= x) ++root; return root; } @SuppressWarnings("unused") int cbrtLong(long x) { int cbrt = (int) Math.cbrt(x); while ((long) cbrt * cbrt >= x) { cbrt--; } while ((long) (cbrt + 1) * (cbrt + 1) <= x) cbrt++; return cbrt; } @SuppressWarnings("unused") long binpow(long a, long power) { return binpow(a, power, INF); } @SuppressWarnings("unused") long binpow(long a, long power, long modulo) { if (power == 0) return 1 % modulo; if (power % 2 == 1) { long b = binpow(a, power - 1, modulo) % modulo; return ((a % modulo) * b) % modulo; } else { long b = binpow(a, power / 2, modulo) % modulo; return (b * b) % modulo; } } @SuppressWarnings("unused") long fastMod(String s1, long n2) { long num = 0; for (int i = 0; i < s1.length() - 1; i++) { num += Integer.parseInt(String.valueOf(s1.charAt(i))); num *= 10; if (num >= n2) { num = num % n2; } } return (num + Integer.parseInt(String.valueOf(s1.charAt(s1.length() - 1)))) % n2; } @SuppressWarnings("unused") long factorialMod(long n, long p) { long res = 1; while (n > 1) { res = (res * ((n / p) % 2 == 1 ? p - 1 : 1)) % p; for (int i = 2; i <= n % p; ++i) res = (res * i) % p; n /= p; } return res % p; } @SuppressWarnings("unused") boolean isPrime(int number) { for (int i = 2; i * i <= number; i++) { if (number % i == 0) { return false; } } return number > 1; } @SuppressWarnings("unused") boolean[] primes(int border) { boolean[] isPrimes = new boolean[border + 1]; Arrays.fill(isPrimes, true); isPrimes[0] = false; isPrimes[1] = false; for (int i = 2; i < border + 1; i++) { if (!isPrimes[i]) continue; for (int k = i * 2; k < border; k += i) { isPrimes[k] = false; } } return isPrimes; } //Number theory @SuppressWarnings("unused") void sort(int[] a) { int n = a.length; Integer[] arr = new Integer[n]; for (int i = 0; i < n; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < n; i++) { a[i] = arr[i]; } } @SuppressWarnings("unused") void sort(long[] a) { int n = a.length; Long[] arr = new Long[n]; for (int i = 0; i < n; i++) { arr[i] = a[i]; } Arrays.sort(arr); for (int i = 0; i < n; i++) { a[i] = arr[i]; } } @SuppressWarnings("unused") int max(int[] a) { int n = a.length; int max = Integer.MIN_VALUE; for (int j : a) { if (j > max) max = j; } return max; } @SuppressWarnings("unused") long max(long[] a) { int n = a.length; long max = Long.MIN_VALUE; for (long l : a) { if (l > max) max = l; } return max; } @SuppressWarnings("unused") int maxIndex(int[] a) { int n = a.length; int max = Integer.MIN_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int maxIndex(long[] a) { int n = a.length; long max = Long.MIN_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] > max) { max = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int min(int[] a) { int n = a.length; int min = Integer.MAX_VALUE; for (int j : a) { if (j < min) min = j; } return min; } @SuppressWarnings("unused") int minIndex(int[] a) { int n = a.length; int min = Integer.MAX_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] < min) { min = a[i]; index = i; } } return index; } @SuppressWarnings("unused") int minIndex(long[] a) { int n = a.length; long min = Long.MAX_VALUE; int index = 0; for (int i = 0; i < n; i++) { if (a[i] < min) { min = a[i]; index = i; } } return index; } @SuppressWarnings("unused") long min(long[] a) { int n = a.length; long min = Long.MAX_VALUE; for (long l : a) { if (l < min) min = l; } return min; } @SuppressWarnings("unused") long sum(int[] a) { int n = a.length; long sum = 0; for (int j : a) { sum += j; } return sum; } @SuppressWarnings("unused") long sum(long[] a) { int n = a.length; long sum = 0; for (long l : a) { sum += l; } return sum; } //Arrays Operations @SuppressWarnings("unused") String readLine() { try { return in.readLine(); } catch (Exception e) { throw new InputMismatchException(); } } @SuppressWarnings("unused") String rs() { while (!tok.hasMoreTokens()) { tok = new StringTokenizer(readLine()); } return tok.nextToken(); } @SuppressWarnings("unused") int ri() { return Integer.parseInt(rs()); } @SuppressWarnings("unused") long rl() { return Long.parseLong(rs()); } @SuppressWarnings("unused") double rd() { return Double.parseDouble(rs()); } @SuppressWarnings("unused") int[] ria(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ri(); } return a; } @SuppressWarnings("unused") int[] riawd(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = ri() - 1; } return a; } @SuppressWarnings("unused") long[] rla(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = rl(); } return a; } @SuppressWarnings("unused") private boolean yesNo(boolean yes, String yesString, String noString) { out.println(yes ? yesString : noString); return yes; } //fastIO void run() { try { long start = System.currentTimeMillis(); initIO(); if (MULTI_TEST) { long t = rl(); while (t-- > 0) { solve(); } } else { solve(); } out.close(); System.err.println("Time(ms) - " + (System.currentTimeMillis() - start)); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } } void initConsoleIO() { out = new PrintWriter(System.out); in = new BufferedReader(new InputStreamReader(System.in)); } void initFileIO(String inName, String outName) throws FileNotFoundException { out = new PrintWriter(outName); in = new BufferedReader(new FileReader(inName)); } void initIO() throws FileNotFoundException { if (!FILE_NAME.isEmpty()) { initFileIO(FILE_NAME + ".in", FILE_NAME + ".out"); } else { if (NEED_FILE_IO && new File("input.txt").exists()) { initFileIO("input.txt", "output.txt"); } else { initConsoleIO(); } } tok = new StringTokenizer(""); } //initIO private final String FILE_NAME; private final boolean MULTI_TEST; private final boolean NEED_FILE_IO; private final long INF; private BufferedReader in; private PrintWriter out; private StringTokenizer tok; //fields }
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
b0c19f10316b148c088a70c4d8713e97
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 final int MOD = (int) (1e9 + 7); static FastReader sc; static void solve() { int n = sc.nextInt(); int p = sc.nextInt(); Integer[] a = new Integer[n]; Set<Integer> set = new TreeSet<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); a[i] = x; // set.add(x); } Arrays.stream(a) .forEach(set::add); Arrays.sort(a, (i1, i2) -> { return i2 - i1; }); set.add(0); for (int i = 0; i < n; i++) { int x = a[i]; set.remove(a[i]); while (true) { if (set.contains(x)) break; if (x % 4 == 0) { x /= 4; } else if (x % 2 == 0) break; else x /= 2; } if (!set.contains(x) || x == 0) set.add(a[i]); } set.remove(0); long[] dp = new long[p + 255]; for (Integer i : set) { int j = 0; while ((1L << j) <= i) j++; dp[j]++; } long ans = 0; for (int i = 0; i <= p; i++) { ans += dp[i]; ans %= MOD; dp[i + 1] += dp[i]; dp[i + 1] %= MOD; dp[i + 2] += dp[i]; dp[i + 2] %= MOD; } System.out.println(ans); } public static void main(String[] args) throws FileNotFoundException { if (args.length >= 1 && args[0].equals("666EGOR777")) { sc = new FastReader("input.txt"); } else { sc = new FastReader(); } int tests = 1; // tests = sc.nextInt(); for (int test = 0; test < tests; test++) { solve(); System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } 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
cef11a149106601a755389c2d3915eb2
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 final int MOD = (int) (1e9 + 7); static FastReader sc; static void solve() { int n = sc.nextInt(); int p = sc.nextInt(); Integer[] a = new Integer[n]; Set<Integer> set = new TreeSet<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); a[i] = x; // set.add(x); } Arrays.stream(a) .forEach(set::add); // Arrays.sort(a, (i1, i2) -> { // return i1 - i2; // }); set.add(0); for (int i = 0; i < n; i++) { int x = a[i]; set.remove(a[i]); while (true) { if (set.contains(x)) break; if (x % 4 == 0) { x /= 4; } else if (x % 2 == 0) break; else x /= 2; } if (!set.contains(x) || x == 0) set.add(a[i]); } set.remove(0); long[] dp = new long[p + 255]; for (Integer i : set) { int j = 0; while ((1L << j) <= i) j++; dp[j]++; } long ans = 0; for (int i = 0; i <= p; i++) { ans += dp[i]; ans %= MOD; dp[i + 1] += dp[i]; dp[i + 1] %= MOD; dp[i + 2] += dp[i]; dp[i + 2] %= MOD; } System.out.println(ans); } public static void main(String[] args) throws FileNotFoundException { if (args.length >= 1 && args[0].equals("666EGOR777")) { sc = new FastReader("input.txt"); } else { sc = new FastReader(); } int tests = 1; // tests = sc.nextInt(); for (int test = 0; test < tests; test++) { solve(); System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } 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
c7621d0e860870f42d1bf85d7a504b0f
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 final int MOD = (int) (1e9 + 7); static FastReader sc; static void solve() { int n = sc.nextInt(); int p = sc.nextInt(); Integer[] a = new Integer[n]; Set<Integer> set = new TreeSet<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); a[i] = x; // set.add(x); } Arrays.stream(a) .forEach(set::add); Arrays.sort(a, (i1, i2) -> { return i1 - i2; }); set.add(0); for (int i = 0; i < n; i++) { int x = a[i]; set.remove(a[i]); while (true) { if (set.contains(x)) break; if (x % 4 == 0) { x /= 4; } else if (x % 2 == 0) break; else x /= 2; } if (!set.contains(x) || x == 0) set.add(a[i]); } set.remove(0); long[] dp = new long[p + 255]; for (Integer i : set) { int j = 0; while ((1L << j) <= i) j++; dp[j]++; } long ans = 0; for (int i = 0; i <= p; i++) { ans += dp[i]; ans %= MOD; dp[i + 1] += dp[i]; dp[i + 1] %= MOD; dp[i + 2] += dp[i]; dp[i + 2] %= MOD; } System.out.println(ans); } public static void main(String[] args) throws FileNotFoundException { if (args.length >= 1 && args[0].equals("666EGOR777")) { sc = new FastReader("input.txt"); } else { sc = new FastReader(); } int tests = 1; // tests = sc.nextInt(); for (int test = 0; test < tests; test++) { solve(); System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } 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
e7ef9aa0aa850c4af5acfb375dc26ddf
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 final int MOD = (int) (1e9 + 7); static FastReader sc; static void solve() { int n = sc.nextInt(); int p = sc.nextInt(); Integer[] a = new Integer[n]; Set<Integer> set = new TreeSet<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); a[i] = x; set.add(x); } Arrays.sort(a, (i1, i2) -> { return i1 - i2; }); set.add(0); for (int i = 0; i < n; i++) { int x = a[i]; set.remove(a[i]); while (true) { if (set.contains(x)) break; if (x % 4 == 0) { x /= 4; } else if (x % 2 == 0) break; else x /= 2; } if (!set.contains(x) || x == 0) set.add(a[i]); } set.remove(0); long[] dp = new long[p + 255]; for (Integer i : set) { int j = 0; while ((1L << j) <= i) j++; dp[j]++; } long ans = 0; for (int i = 0; i <= p; i++) { ans += dp[i]; ans %= MOD; dp[i + 1] += dp[i]; dp[i + 1] %= MOD; dp[i + 2] += dp[i]; dp[i + 2] %= MOD; } System.out.println(ans); } public static void main(String[] args) throws FileNotFoundException { if (args.length >= 1 && args[0].equals("egorTriesJava")) { sc = new FastReader("input.txt"); } else { sc = new FastReader(); } int tests = 1; // tests = sc.nextInt(); for (int test = 0; test < tests; test++) { solve(); System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } 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
bbd75eb0d785d38397575856d49c1cda
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 final int MOD = (int) (1e9 + 7); static FastReader sc; static void solve() { int n = sc.nextInt(); int p = sc.nextInt(); Integer[] a = new Integer[n]; Set<Integer> set = new TreeSet<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); a[i] = x; set.add(x); } // Arrays.sort(a, (i1, i2) -> { // return i1 - i2; // }); set.add(0); for (int i = 0; i < n; i++) { int x = a[i]; set.remove(a[i]); while (true) { if (set.contains(x)) break; if (x % 4 == 0) { x /= 4; } else if (x % 2 == 0) break; else x /= 2; } if (!set.contains(x) || x == 0) set.add(a[i]); } set.remove(0); long[] dp = new long[p + 255]; for (Integer i : set) { int j = 0; while ((1L << j) <= i) j++; dp[j]++; } long ans = 0; for (int i = 0; i <= p; i++) { ans += dp[i]; ans %= MOD; dp[i + 1] += dp[i]; dp[i + 1] %= MOD; dp[i + 2] += dp[i]; dp[i + 2] %= MOD; } System.out.println(ans); } public static void main(String[] args) throws FileNotFoundException { if (args.length >= 1 && args[0].equals("egorTriesJava")) { sc = new FastReader("input.txt"); } else { sc = new FastReader(); } int tests = 1; // tests = sc.nextInt(); for (int test = 0; test < tests; test++) { solve(); System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } 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
043378526f0ee047f0320b0b866e3c30
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 final int MOD = (int) (1e9 + 7); static FastReader sc; static void solve() { int n = sc.nextInt(); int p = sc.nextInt(); Integer[] a = new Integer[n]; Set<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); a[i] = x; set.add(x); } Arrays.sort(a, (i1, i2) -> { return i1 - i2; }); set.add(0); for (int i = 0; i < n; i++) { int x = a[i]; set.remove(a[i]); while (true) { if (set.contains(x)) break; if (x % 4 == 0) { x /= 4; } else if (x % 2 == 0) break; else x /= 2; } if (!set.contains(x) || x == 0) set.add(a[i]); } set.remove(0); long[] dp = new long[p + 255]; for (Integer i : set) { int j = 0; while ((1L << j) <= i) j++; dp[j]++; } long ans = 0; for (int i = 0; i <= p; i++) { ans += dp[i]; ans %= MOD; dp[i + 1] += dp[i]; dp[i + 1] %= MOD; dp[i + 2] += dp[i]; dp[i + 2] %= MOD; } System.out.println(ans); } public static void main(String[] args) throws FileNotFoundException { if (args.length >= 1 && args[0].equals("egorTriesJava")) { sc = new FastReader("input.txt"); } else { sc = new FastReader(); } int tests = 1; // tests = sc.nextInt(); for (int test = 0; test < tests; test++) { solve(); System.out.println(); } } static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastReader(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } 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
343070406cf631c80c6430f4b9daf009
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.*; public class Main { static final int MOD = (int) (1e9 + 7); static Scanner sc = new Scanner(System.in); static void solve() { int n = sc.nextInt(); int p = sc.nextInt(); Integer[] a = new Integer[n]; Set<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { int x = sc.nextInt(); a[i] = x; set.add(x); } Arrays.sort(a, (i1, i2) -> { return i1 - i2; }); set.add(0); for (int i = 0; i < n; i++) { int x = a[i]; set.remove(a[i]); while (true) { if (set.contains(x)) break; if (x % 4 == 0) { x /= 4; } else if (x % 2 == 0) break; else x /= 2; } if (!set.contains(x) || x == 0) set.add(a[i]); } set.remove(0); long[] dp = new long[p + 255]; for (Integer i : set) { int j = 0; while ((1L << j) <= i) j++; dp[j]++; } long ans = 0; for (int i = 0; i <= p; i++) { ans += dp[i]; ans %= MOD; dp[i + 1] += dp[i]; dp[i + 1] %= MOD; dp[i + 2] += dp[i]; dp[i + 2] %= MOD; } System.out.println(ans); } public static void main(String[] args) { int tests = 1; // tests = sc.nextInt(); for (int test = 0; test < tests; test++) { solve(); System.out.println(); } } }
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
e286a118b62a9c9908070afe7fc62c7f
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 on: 10:40:04 PM | 20-Feb-2022 import java.util.*; import java.io.*; import java.math.*; import java.lang.*; public class Solution { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int caseNo = 1; solveIt(caseNo); } // Solution static void solveIt(int t) { int n = sc.nextInt(); int p = sc.nextInt(); long a[] = sc.readLongArray(n); long fib[] = new long[2 * (int) (1e5 + 5)]; fib[0] = 1l; fib[1] = 1l; for (int i = 2; i < fib.length; i++) { fib[i] = (long) (fib[i - 1] + fib[i - 2]) % (long) (1e9 + 7); } for (int i = 1; i < fib.length; i++) { fib[i] = (fib[i] + fib[i - 1]) % (long) (1e9 + 7); } Arrays.sort(a); HashSet<Long> set = new HashSet<>(); long ans = 0; out: for (long i : a) { long x = i; while (x > 0) { if (set.contains(x)) continue out; if ((x & 1) == 1) x >>= 1; else if ((x & 3) == 0) x >>= 2; else break; } set.add(i); int len = Long.toBinaryString(i).length(); if (len <= p) { ans = (ans + (long) fib[p - len]) % (long) (1e9 + 7); } } System.out.println(ans); } static long reduce(long a) { while (a > 1) { if ((a & 1) == 1) a >>= 1; else if ((a & 3) == 0) a >>= 2; else break; } return a; } static void revArray(int a[], int i, int j) { while (i <= j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; i++; j--; } } static boolean isSorted(int a[], int s, int e, boolean strict) { for (int i = s + 1; i <= e; i++) { if (strict && a[i - 1] >= a[i]) return false; if (!strict && a[i - 1] > a[i]) return false; } return true; } static class DSU { int rank[]; int parent[]; DSU(int n) { rank = new int[n + 1]; parent = new int[n + 1]; for (int i = 1; i <= n; i++) { parent[i] = i; } } int findParent(int node) { if (parent[node] == node) return node; return parent[node] = findParent(parent[node]); } boolean union(int x, int y) { int px = findParent(x); int py = findParent(y); if (px == py) return false; if (rank[px] < rank[py]) { parent[px] = py; } else if (rank[px] > rank[py]) { parent[py] = px; } else { parent[px] = py; rank[py]++; } 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 boolean[] seiveOfEratosthenes(int n) { boolean[] isPrime = new boolean[n + 1]; Arrays.fill(isPrime, true); for (int i = 2; i * i <= n; i++) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } return isPrime; } public static int[] seiveOnlyPrimes(int n) { if (n <= 32) { int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; for (int i = 0; i < primes.length; i++) { if (n < primes[i]) { return Arrays.copyOf(primes, i); } } return primes; } int u = n + 32; double lu = Math.log(u); int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)]; ret[0] = 2; int pos = 1; int[] isp = new int[(n + 1) / 32 / 2 + 1]; int sup = (n + 1) / 32 / 2 + 1; int[] tprimes = { 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; for (int tp : tprimes) { ret[pos++] = tp; int[] ptn = new int[tp]; for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31); for (int i = 0; i < tp; i++) { for (int j = i; j < sup; j += tp) isp[j] |= ptn[i]; } } int[] magic = { 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 }; int h = n / 2; for (int i = 0; i < sup; i++) { for (int j = ~isp[i]; j != 0; j &= j - 1) { int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27]; int p = 2 * pp + 3; if (p > n) break; ret[pos++] = p; for (int q = pp; q <= h; q += p) isp[q >> 5] |= 1 << (q & 31); } } return Arrays.copyOf(ret, pos); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static boolean isPrime(long n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); sc.tr(G - S + "ms"); } static class sc { private static boolean endOfFile() { if (bufferLength == -1) return true; int lptr = ptrbuf; while (lptr < bufferLength) if (!isThisTheSpaceCharacter(inputBufffferBigBoi[lptr++])) return false; try { is.mark(1000); while (true) { int b = is.read(); if (b == -1) { is.reset(); return true; } else if (!isThisTheSpaceCharacter(b)) { is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inputBufffferBigBoi = new byte[1024]; static int bufferLength = 0, ptrbuf = 0; private static int justReadTheByte() { if (bufferLength == -1) throw new InputMismatchException(); if (ptrbuf >= bufferLength) { ptrbuf = 0; try { bufferLength = is.read(inputBufffferBigBoi); } catch (IOException e) { throw new InputMismatchException(); } if (bufferLength <= 0) return -1; } return inputBufffferBigBoi[ptrbuf++]; } private static boolean isThisTheSpaceCharacter(int c) { return !(c >= 33 && c <= 126); } private static int skipItBishhhhhhh() { int b; while ((b = justReadTheByte()) != -1 && isThisTheSpaceCharacter(b)) ; return b; } private static double nextDouble() { return Double.parseDouble(next()); } private static char nextChar() { return (char) skipItBishhhhhhh(); } private static String next() { int b = skipItBishhhhhhh(); StringBuilder sb = new StringBuilder(); while (!(isThisTheSpaceCharacter(b))) { sb.appendCodePoint(b); b = justReadTheByte(); } return sb.toString(); } private static char[] readCharArray(int n) { char[] buf = new char[n]; int b = skipItBishhhhhhh(), p = 0; while (p < n && !(isThisTheSpaceCharacter(b))) { buf[p++] = (char) b; b = justReadTheByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] readCharMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = readCharArray(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } private static int nextInt() { int num = 0, b; boolean minus = false; while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = justReadTheByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = justReadTheByte(); } } private static long nextLong() { long num = 0; int b; boolean minus = false; while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = justReadTheByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = justReadTheByte(); } } private static void tr(Object... o) { if (INPUT.length() != 0) System.out.println(Arrays.deepToString(o)); } } }
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
22bbeac54ae217594af4b28da284c171
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 on: 10:40:04 PM | 20-Feb-2022 import java.util.*; import java.io.*; import java.math.*; import java.lang.*; public class Solution { static InputStream is; static PrintWriter out; static String INPUT = ""; static void solve() { int caseNo = 1; solveIt(caseNo); } // Solution static void solveIt(int t) { int n = sc.nextInt(); int p = sc.nextInt(); long a[] = sc.readLongArray(n); long fib[] = new long[2 * (int) (1e5 + 5)]; long MD = (long) (1e9 + 7); fib[0] = 1l; fib[1] = 1l; for (int i = 2; i < fib.length; i++) { fib[i] = (long) (fib[i - 1] + fib[i - 2]) % MD; } for (int i = 1; i < fib.length; i++) { fib[i] = (fib[i] + fib[i - 1]) % MD; } Arrays.sort(a); HashSet<Long> set = new HashSet<>(); long ans = 0; out: for (long i : a) { for (long aa = i; aa > 0;) { if (set.contains(aa)) continue out; if ((aa & 1) == 1) aa >>= 1; else if ((aa & 3) == 0) aa >>= 2; else break; } set.add(i); int k = p; for (long aa = i; aa > 0; aa >>= 1) k--; if (k < 0) break; ans = (ans + fib[k]) % MD; } System.out.println(ans); } static long reduce(long a) { while (a > 1) { if ((a & 1) == 1) a >>= 1; else if ((a & 3) == 0) a >>= 2; else break; } return a; } static void revArray(int a[], int i, int j) { while (i <= j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; i++; j--; } } static boolean isSorted(int a[], int s, int e, boolean strict) { for (int i = s + 1; i <= e; i++) { if (strict && a[i - 1] >= a[i]) return false; if (!strict && a[i - 1] > a[i]) return false; } return true; } static class DSU { int rank[]; int parent[]; DSU(int n) { rank = new int[n + 1]; parent = new int[n + 1]; for (int i = 1; i <= n; i++) { parent[i] = i; } } int findParent(int node) { if (parent[node] == node) return node; return parent[node] = findParent(parent[node]); } boolean union(int x, int y) { int px = findParent(x); int py = findParent(y); if (px == py) return false; if (rank[px] < rank[py]) { parent[px] = py; } else if (rank[px] > rank[py]) { parent[py] = px; } else { parent[px] = py; rank[py]++; } 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 boolean[] seiveOfEratosthenes(int n) { boolean[] isPrime = new boolean[n + 1]; Arrays.fill(isPrime, true); for (int i = 2; i * i <= n; i++) { for (int j = i * i; j <= n; j += i) { isPrime[j] = false; } } return isPrime; } public static int[] seiveOnlyPrimes(int n) { if (n <= 32) { int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; for (int i = 0; i < primes.length; i++) { if (n < primes[i]) { return Arrays.copyOf(primes, i); } } return primes; } int u = n + 32; double lu = Math.log(u); int[] ret = new int[(int) (u / lu + u / lu / lu * 1.5)]; ret[0] = 2; int pos = 1; int[] isp = new int[(n + 1) / 32 / 2 + 1]; int sup = (n + 1) / 32 / 2 + 1; int[] tprimes = { 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 }; for (int tp : tprimes) { ret[pos++] = tp; int[] ptn = new int[tp]; for (int i = (tp - 3) / 2; i < tp << 5; i += tp) ptn[i >> 5] |= 1 << (i & 31); for (int i = 0; i < tp; i++) { for (int j = i; j < sup; j += tp) isp[j] |= ptn[i]; } } int[] magic = { 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 }; int h = n / 2; for (int i = 0; i < sup; i++) { for (int j = ~isp[i]; j != 0; j &= j - 1) { int pp = i << 5 | magic[(j & -j) * 0x076be629 >>> 27]; int p = 2 * pp + 3; if (p > n) break; ret[pos++] = p; for (int q = pp; q <= h; q += p) isp[q >> 5] |= 1 << (q & 31); } } return Arrays.copyOf(ret, pos); } static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } static boolean isPrime(long n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); solve(); out.flush(); long G = System.currentTimeMillis(); sc.tr(G - S + "ms"); } static class sc { private static boolean endOfFile() { if (bufferLength == -1) return true; int lptr = ptrbuf; while (lptr < bufferLength) if (!isThisTheSpaceCharacter(inputBufffferBigBoi[lptr++])) return false; try { is.mark(1000); while (true) { int b = is.read(); if (b == -1) { is.reset(); return true; } else if (!isThisTheSpaceCharacter(b)) { is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inputBufffferBigBoi = new byte[1024]; static int bufferLength = 0, ptrbuf = 0; private static int justReadTheByte() { if (bufferLength == -1) throw new InputMismatchException(); if (ptrbuf >= bufferLength) { ptrbuf = 0; try { bufferLength = is.read(inputBufffferBigBoi); } catch (IOException e) { throw new InputMismatchException(); } if (bufferLength <= 0) return -1; } return inputBufffferBigBoi[ptrbuf++]; } private static boolean isThisTheSpaceCharacter(int c) { return !(c >= 33 && c <= 126); } private static int skipItBishhhhhhh() { int b; while ((b = justReadTheByte()) != -1 && isThisTheSpaceCharacter(b)) ; return b; } private static double nextDouble() { return Double.parseDouble(next()); } private static char nextChar() { return (char) skipItBishhhhhhh(); } private static String next() { int b = skipItBishhhhhhh(); StringBuilder sb = new StringBuilder(); while (!(isThisTheSpaceCharacter(b))) { sb.appendCodePoint(b); b = justReadTheByte(); } return sb.toString(); } private static char[] readCharArray(int n) { char[] buf = new char[n]; int b = skipItBishhhhhhh(), p = 0; while (p < n && !(isThisTheSpaceCharacter(b))) { buf[p++] = (char) b; b = justReadTheByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] readCharMatrix(int n, int m) { char[][] map = new char[n][]; for (int i = 0; i < n; i++) map[i] = readCharArray(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } private static int nextInt() { int num = 0, b; boolean minus = false; while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = justReadTheByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = justReadTheByte(); } } private static long nextLong() { long num = 0; int b; boolean minus = false; while ((b = justReadTheByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')) ; if (b == '-') { minus = true; b = justReadTheByte(); } while (true) { if (b >= '0' && b <= '9') { num = num * 10 + (b - '0'); } else { return minus ? -num : num; } b = justReadTheByte(); } } private static void tr(Object... o) { if (INPUT.length() != 0) System.out.println(Arrays.deepToString(o)); } } }
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
e9e939c37bb6fbbfbf1dc98bba309027
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.Arrays; import java.util.HashSet; import java.util.InputMismatchException; public class E1635D { static int MOD = (int) 1e9 + 7; public static void main(String[] args) { FastIO io = new FastIO(); int n = io.nextInt(); int p = io.nextInt(); long[] arr = new long[n]; HashSet<Long> s = new HashSet<>(); for (int i = 0; i < n; i++) { arr[i] = io.nextInt(); } Arrays.sort(arr); out: for (long v : arr) { long c = v; while (true) { if (c % 4 == 0) c /= 4; if (s.contains(c)) continue out; if (c % 2 == 1 && c != 1) c = (c - 1) / 2; if (s.contains(c)) continue out; if (c % 4 != 0 && c % 2 == 0 || c == 1) { s.add(v); break; } } } long[] dp = new long[(int) 2e5 + 1]; for (long v : s) dp[63 - Long.numberOfLeadingZeros(v)]++; for (int i = 1; i < p; i++) { dp[i] += dp[i - 1]; if (i > 1) dp[i] += dp[i - 2]; dp[i] %= MOD; } long sum = 0; for (int i = 0; i < p; i++) sum = (sum + dp[i]) % MOD; io.println(sum); io.close(); } private static class FastIO extends PrintWriter { private final InputStream stream; private final 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 int nextInt() { // nextLong() would be implemented similarly 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() { // nextLong() would be implemented similarly 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; } public 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
6e7d75ebf82579263fda8d5ab05f51ab
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 = 1000000007; public static void main (String[] args) throws IOException { Kattio io = new Kattio(); int n = io.nextInt(); int p = io.nextInt(); TreeSet<Long> set = new TreeSet<Long>(); for (int i=0; i<n; i++) { long x = io.nextLong(); set.add(x); } TreeSet<Long> useful = new TreeSet<Long>(); for (long i : set) { long temp = i; boolean add = true; while (i%4 == 0 || i%2 == 1) { if (i == 0 || i == 1) break; if (i%4 == 0) { i /= 4; } else if (i%2 == 1) { i /= 2; } if (useful.contains(i)) { add = false; break; } } if (add) { useful.add(temp); } } int MAXP = 200005; long[] dp = new long[MAXP]; Arrays.fill(dp, 0); for (long i : useful) { double base = log(2, (double) i); //System.out.println(i + " " + base); dp[(int) base]++; } for (int pow=1; pow<=p; pow++) { if (pow >= 2) { dp[pow] = (dp[pow] + dp[pow-1] + dp[pow-2]) % MOD; } else { dp[pow] = (dp[pow] + dp[pow-1])%MOD; } } long ans = 0; for (int i=0; i<p; i++) { //System.out.println(i + " " + dp[i]); ans = (ans + dp[i]) % MOD; } System.out.println(ans); } static double log(double base, double x) { return Math.log(x)/Math.log(base); } static class Kattio extends PrintWriter { private BufferedReader r; private StringTokenizer st; // standard input public Kattio() { this(System.in, System.out); } public Kattio(InputStream i, OutputStream o) { super(o); r = new BufferedReader(new InputStreamReader(i)); } // USACO-style file input public Kattio(String problemName) throws IOException { super(new FileWriter(problemName + ".out")); r = new BufferedReader(new FileReader(problemName + ".in")); } // returns null if no more input public String next() { try { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(r.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()); } } }
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
d907eb83a3416dee62c04717132151ff
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 Code { //<--------------------------------FAST------------------------------------------------>// static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer tok = new StringTokenizer(""); static int nextInt() throws IOException{ return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static String next() throws IOException{ if (!tok.hasMoreTokens()) tok = new StringTokenizer(in.readLine()); return tok.nextToken(); } static int[] nextIntArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i < A.length; i++) A[i] = nextInt(); return A; } static long[] nextLongArray(long n) throws IOException { long[] A = new long[(int) n]; for (int i = 0; i < A.length; i++) A[i] = nextLong(); return A; } //<------------------------------------------------------------------------------------>// public static void main(String[] args) throws IOException { int t = 1; //t = nextInt(); while(t-->0) { test(); out.flush(); } } static void test() throws IOException { long n = nextLong() , p = nextLong(); long[] v = nextLongArray(n) ; long[] dp = new long[200001]; Arrays.sort(v); for (var num : v) { if (useless(num)) continue; all.add(num); int cnt = 0 ; int x = 1; while(x <= num) { x = x*2; cnt++; } dp[cnt] ++; } for (int i=2 ; i<=p ; i++) { dp[i] = getSum(dp[i],getSum(dp[i-1] , dp[i-2])); } long ans = 0; for (int i=1 ; i<=p ; i++) { ans = getSum(ans,dp[i]); } out.print(ans); } public static Set<Long> all = new HashSet<>(); public static long mod = (long)1e9+7; public static boolean useless(long x) { if (all.contains(x)) return true; if (x==0) return false; boolean ret = false; if (x%4==0) ret = useless(x / 4); else if (x%2==1) ret = useless((x - 1) / 2); return ret; } public static long getSum(long a , long b) { return (a % mod + b % mod)%mod; } }
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
b040a88b2815babb714b30061df1138c
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 Code { //<--------------------------------FAST------------------------------------------------>// static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer tok = new StringTokenizer(""); static int nextInt() throws IOException{ return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static String next() throws IOException{ if (!tok.hasMoreTokens()) tok = new StringTokenizer(in.readLine()); return tok.nextToken(); } static int[] nextIntArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i < A.length; i++) A[i] = nextInt(); return A; } static long[] nextLongArray(long n) throws IOException { long[] A = new long[(int) n]; for (int i = 0; i < A.length; i++) A[i] = nextLong(); return A; } //<------------------------------------------------------------------------------------>// public static void main(String[] args) throws IOException { int t = 1; //t = nextInt(); while(t-->0) { test(); out.flush(); } } static void test() throws IOException { long n = nextLong() , p = nextLong(); long[] v = nextLongArray(n) ; long[] dp = new long[200001]; Arrays.sort(v); for (var num : v) { if (solve(num)) continue; all.add(num); int cnt = 0 ; int x = 1; while(x <= num) { x = x*2; cnt++; } dp[cnt] ++; } for (int i=2 ; i<=p ; i++) { dp[i] = getSum(dp[i],getSum(dp[i-1] , dp[i-2])); } long ans = 0; for (int i=1 ; i<=p ; i++) { ans = getSum(ans,dp[i]); } out.print(ans); } public static HashMap<Long,Boolean> anotherDp = new HashMap<>(); public static Set<Long> all = new HashSet<>(); public static long mod = (long)1e9+7; public static boolean solve(long x) { if (all.contains(x)) return true; if (x==0) return false; boolean ret = false; if (anotherDp.containsKey(x)) return anotherDp.get(x); if (x%4==0) ret = solve(x / 4); else if (x%2==1) ret = solve((x - 1) / 2); anotherDp.put(x,ret); return ret; } public static long getSum(long a , long b) { return (a % mod + b % mod)%mod; } }
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
280e4f60d7469304b5df4fcad3e0be01
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 Code { //<--------------------------------FAST------------------------------------------------>// static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static StringTokenizer tok = new StringTokenizer(""); static int nextInt() throws IOException{ return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static String next() throws IOException{ if (!tok.hasMoreTokens()) tok = new StringTokenizer(in.readLine()); return tok.nextToken(); } static int[] nextIntArray(int n) throws IOException { int[] A = new int[n]; for (int i = 0; i < A.length; i++) A[i] = nextInt(); return A; } static long[] nextLongArray(long n) throws IOException { long[] A = new long[(int) n]; for (int i = 0; i < A.length; i++) A[i] = nextLong(); return A; } //<------------------------------------------------------------------------------------>// public static void main(String[] args) throws IOException { int t = 1; //t = nextInt(); while(t-->0) { test(); out.flush(); } } static void test() throws IOException { // weighted graph // shorted path // WeightedGraph graph = new WeightedGraph(); // graph.addNode("A"); // graph.addNode("B"); // graph.addNode("C"); // graph.addEdge("A","B",3); // graph.addEdge("A","C",2); // graph.print(); // dp[powerOfTwo] // 7 * 2 + 1 = 15 add one doesn't change power of 2 or log of two // so dp[2^4] = dp[2^2] + dp[2^3] // dp[i] = dp[i-2] + dp[i-1] long n = nextLong(); long p = nextLong(); long[] v = nextLongArray(n); long[] dp = new long[200001]; Arrays.sort(v); for (var num : v) { if (solve(num)) { continue; } all.add(num); int cnt = 0 ; int x = 1; while(x <= num) { x = x*2; cnt++; } dp[cnt] ++; } // 1 -> 0 2 -> 1 4-> 1 8-> 3 // 4 should be 2 -> 4 > 3 , 1 // cnt 0 x = 1 // cnt 1 x = 2 // cnt 2 x = 4 //out.println(Arrays.toString(dp)); for (int i=2 ; i<=p ; i++) { dp[i] = getSum(dp[i],getSum(dp[i-1] , dp[i-2])); } long ans = 0; for (int i=1 ; i<=p ; i++) { ans = getSum(ans,dp[i]); } //out.println(Arrays.toString(dp)); //out.print(dp[(int)p]); out.print(ans); } public static HashMap<Long,Boolean> anotherDp = new HashMap<>(); public static Set<Long> all = new HashSet<>(); public static long mod = (long)1e9+7; public static boolean solve(long x) { if (all.contains(x)) return true; if (x==0) return false; boolean ret = false; if (anotherDp.containsKey(x)) return anotherDp.get(x); if (x%4==0) { // 3 * 2 + 1 -> 7 ret = solve(x / 4); } else if (x%2==1) { ret = solve((x - 1) / 2); } anotherDp.put(x,ret); return ret; } public static long getSum(long a , long b) { return (a % mod + b % mod)%mod; } }
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
61dc7f0b98857b4b9fe7057f9ef2f006
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.lang.reflect.Array; import java.lang.reflect.GenericArrayType; import java.util.*; public class B { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int p = sc.nextInt(); int[] arr = new int[n]; HashSet<Integer> hs = new HashSet<>(); for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int[] dp = new int[p]; Arrays.sort(arr); for (int i = 0; i < n; i++) { int tmp = arr[i]; boolean flag = false; while (tmp > 0) { if (hs.contains(tmp)) { flag = true; break; } if ((tmp & 1) != 0) { tmp = tmp >> 1; } else if ((tmp & 3) != 0) { break; } else { tmp = tmp >> 2; } } if (!flag) { int wei = 0; tmp = arr[i]; while (tmp != 0) { tmp = tmp / 2; wei++; } hs.add(arr[i]); if (wei <= p) { dp[wei - 1] = dp[wei - 1] + 1; } } } long result = 0; for (int i = 0; i < p; i++) { if (i >= 1) { dp[i] += dp[i - 1]; dp[i] = dp[i] % 1000000007; } if (i >= 2) { dp[i] += dp[i - 2]; dp[i] = dp[i] % 1000000007; } result += dp[i]; result = result % 1000000007; } System.out.println(result); } }
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
5f28f498a3f62660f9a2d5eea3ce4a7b
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.io.PrintStream; import java.util.Arrays; import java.util.Random; import java.util.HashSet; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author sarthakmanna */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DInfiniteSet solver = new DInfiniteSet(); solver.solve(1, in, out); out.close(); } static class DInfiniteSet { final static Helper hp = new Helper(); int N; int P; long[] A; long MOD = hp.MOD; long[] fib; public void fillInputParams(FastReader in) { // for (int i = 1; i < 20; ++i) { // System.err.print(count(i) + " "); // } // System.err.println(); N = in.nextInt(); P = in.nextInt(); A = in.getLongArray(N); Arrays.sort(A); // for (long itr : A) { // System.err.print(Long.toBinaryString(itr) + " "); // } // System.err.println(); } Object solveOptimised(FastReader in) { int maxLen = 0; for (long itr : A) { maxLen = Math.max(maxLen, Long.toBinaryString(itr).length()); } HashSet<Long> set = new HashSet<>(); long ans = 0; for (long itr : A) { int len = Long.toBinaryString(itr).length(); if (len > P) continue; long left = 0, right = itr; int i; for (i = len - 1; i >= 0; --i) { left <<= 1; left |= hp.getBitAtPosition(itr, i) ? 1 : 0; right = hp.clearBitAtPosition(right, i); if (set.contains(left) && check(right, i)) break; } if (i < 0) { ans += count(P - Long.toBinaryString(itr).length()); //System.err.print(Long.toBinaryString(itr) + " --> "); } set.add(itr); //System.err.println(itr + " " + set); } return ans % MOD; } long count(int len) { if (fib == null) { fib = new long[hp.MAXN]; fib[0] = 2; fib[1] = 3; for (int i = 2; i < fib.length; ++i) { fib[i] = (fib[i - 1] + fib[i - 2]) % MOD; } } return fib[len] - 1; } boolean check(long val, int len) { //System.err.println("Here " + Long.toBinaryString(val) + " " + len); for (int i = 0; i < len; ) { int j; for (j = i; j < len && !hp.getBitAtPosition(val, j); ++j) ; if ((j - i) % 2 == 1) return false; i = Math.max(i + 1, j); } return true; } Object solveBrute(FastReader in) { return null; } public void solve(int testNumber, FastReader in, PrintWriter out) { // out.print("Case #" + testNumber + ": "); fillInputParams(in); Object outOptimised = solveOptimised(in); Object outBrute = solveBrute(in); if (outBrute == null) { out.println(outOptimised); } else if (outBrute.toString().equals(outOptimised.toString())) { System.err.println(testNumber + ". OK Checked"); } else { // print input params System.err.println("Actual = " + outOptimised); System.err.println("Expected = " + outBrute); System.err.println(); throw new ArithmeticException(); } } } static class FastReader { static final int BUFSIZE = 1 << 20; static byte[] buf; static int index; static int total; static InputStream in; public FastReader(InputStream is) { try { in = is; buf = new byte[BUFSIZE]; } catch (Exception e) { } } private int scan() { try { if (index >= total) { index = 0; total = in.read(buf); if (total <= 0) return -1; } return buf[index++]; } catch (Exception | Error e) { System.err.println(e.getMessage()); return 13 / 0; } } public int nextInt() { 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; } public long nextLong() { 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; } public long[] getLongArray(int size) { long[] ar = new long[size]; for (int i = 0; i < size; ++i) ar[i] = nextLong(); return ar; } } static class Helper { public final long MOD; public final int MAXN; final Random rnd; public Helper() { MOD = 1000_000_007; MAXN = 1000_006; rnd = new Random(); } public Helper(long mod, int maxn) { MOD = mod; MAXN = maxn; rnd = new Random(); } public boolean getBitAtPosition(long num, int pos) { return ((num >> pos) & 1) > 0; } public long clearBitAtPosition(long num, int pos) { return (Long.MAX_VALUE ^ (1l << pos)) & num; } } }
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
4b5f27390be64e6195527e98f2d25346
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 { public static void main(String[] args) { solve(); out.close(); } public static void solve() { int M = sc.nextInt(); int p = sc.nextInt(); int[] a = sc.readArray(M); long res = 0; HashSet<Integer> hs = new HashSet<>(); for (int i = 0; i < M; i++) { hs.add(a[i]); } for (int i = 0; i < M; i++) { int tmp = a[i]; while (tmp > 1) { if ((tmp & 1) == 1) { tmp >>= 1; } else if ((tmp & 3) == 0) { tmp >>= 2; } else { break; } if (hs.contains(tmp)) { hs.remove(a[i]); break; } } } long[] fiobo = new long[p + 1]; fiobo[1] = 1; if (p >= 2) { fiobo[2] = 2; } for (int i = 3; i <= p; i++) { fiobo[i] = fiobo[i - 1] + fiobo[i - 2] + 1; fiobo[i] %= MOD; } for (int i : hs) { int digit = 0; while (i > 0) { digit++; i >>= 1; } if (digit > p) { continue; } res += fiobo[p - digit + 1]; res %= MOD; } out.println(res); } private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); private static MyScanner sc = new MyScanner(); private final static int MOD = 1000000007; @SuppressWarnings("unused") private 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()); } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } 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
19fff2a1d9d95b0bba21b03154c127b8
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 javax.print.DocFlavor.INPUT_STREAM; import java.io.*; import java.math.*; import java.sql.Array; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.SQLIntegrityConstraintViolationException; public class Main { private static class MyScanner { private static final int BUF_SIZE = 2048; BufferedReader br; private MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } private boolean isSpace(char c) { return c == '\n' || c == '\r' || c == ' '; } String next() { try { StringBuilder sb = new StringBuilder(); int r; while ((r = br.read()) != -1 && isSpace((char)r)); if (r == -1) { return null; } sb.append((char) r); while ((r = br.read()) != -1 && !isSpace((char)r)) { sb.append((char)r); } return sb.toString(); } catch (IOException e) { e.printStackTrace(); } return null; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } } static class Reader{ BufferedReader br; StringTokenizer st; public Reader() { 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 long mod = (long)(1e9 + 7); static void sort(long[] arr ) { ArrayList<Long> al = new ArrayList<>(); for(long e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(int[] arr ) { ArrayList<Integer> al = new ArrayList<>(); for(int e:arr) al.add(e); Collections.sort(al); for(int i = 0 ; i<al.size(); i++) arr[i] = al.get(i); } static void sort(char[] arr) { ArrayList<Character> al = new ArrayList<Character>(); for(char cc:arr) al.add(cc); Collections.sort(al); for(int i = 0 ;i<arr.length ;i++) arr[i] = al.get(i); } static long mod_mul( long... a) { long ans = a[0]%mod; for(int i = 1 ; i<a.length ; i++) { ans = (ans * (a[i]%mod))%mod; } return ans; } static long mod_sum( long... a) { long ans = 0; for(long e:a) { ans = (ans + e)%mod; } return ans; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static void print(long[] arr) { System.out.println("---print---"); for(long e:arr) System.out.print(e+" "); System.out.println("-----------"); } static void print(int[] arr) { System.out.println("---print---"); for(long e:arr) System.out.print(e+" "); System.out.println("-----------"); } static boolean[] prime(int num) { boolean[] bool = new boolean[num]; for (int i = 0; i< bool.length; i++) { bool[i] = true; } for (int i = 2; i< Math.sqrt(num); i++) { if(bool[i] == true) { for(int j = (i*i); j<num; j = j+i) { bool[j] = false; } } } if(num >= 0) { bool[0] = false; bool[1] = false; } return bool; } static long modInverse(long a, long m) { long g = gcd(a, m); return power(a, m - 2); } static long lcm(long a , long b) { return (a*b)/gcd(a, b); } static int lcm(int a , int b) { return (int)((a*b)/gcd(a, b)); } static long power(long x, long y){ if(y<0) return 0; long m = mod; if (y == 0) return 1; long p = power(x, y / 2) % m; p = (int)((p * (long)p) % m); if (y % 2 == 0) return p; else return (int)((x * (long)p) % m); } static class Combinations{ private long[] z; // factorial private long[] z1; // inverse factorial private long[] z2; // incerse number private long mod; public Combinations(long N , long mod) { this.mod = mod; z = new long[(int)N+1]; z1 = new long[(int)N+1]; z[0] = 1; for(int i =1 ; i<=N ; i++) z[i] = (z[i-1]*i)%mod; z2 = new long[(int)N+1]; z2[0] = z2[1] = 1; for (int i = 2; i <= N; i++) z2[i] = z2[(int)(mod % i)] * (mod - mod / i) % mod; z1[0] = z1[1] = 1; for (int i = 2; i <= N; i++) z1[i] = (z2[i] * z1[i - 1]) % mod; } long fac(long n) { return z[(int)n]; } long invrsNum(long n) { return z2[(int)n]; } long invrsFac(long n) { return z1[(int)n]; } long ncr(long N, long R) { if(R<0 || R>N ) return 0; long ans = ((z[(int)N] * z1[(int)R]) % mod * z1[(int)(N - R)]) % mod; return ans; } } static class DisjointUnionSets { int[] rank, parent; int n; public DisjointUnionSets(int n) { rank = new int[n]; parent = new int[n]; this.n = n; makeSet(); } void makeSet() { for (int i = 0; i < n; i++) { parent[i] = i; } } int find(int x) { if (parent[x] != x) { parent[x] = find(parent[x]); } return parent[x]; } 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 { parent[yRoot] = xRoot; rank[xRoot] = rank[xRoot] + 1; } } } static int max(int... a ) { int max = a[0]; for(int e:a) max = Math.max(max, e); return max; } static long max(long... a ) { long max = a[0]; for(long e:a) max = Math.max(max, e); return max; } static int min(int... a ) { int min = a[0]; for(int e:a) min = Math.min(e, min); return min; } static long min(long... a ) { long min = a[0]; for(long e:a) min = Math.min(e, min); return min; } static int[] KMP(String str) { int n = str.length(); int[] kmp = new int[n]; for(int i = 1 ; i<n ; i++) { int j = kmp[i-1]; while(j>0 && str.charAt(i) != str.charAt(j)) { j = kmp[j-1]; } if(str.charAt(i) == str.charAt(j)) j++; kmp[i] = j; } return kmp; } /************************************************ Query **************************************************************************************/ /***************************************** Sparse Table ********************************************************/ static class SparseTable{ private long[][] st; SparseTable(long[] arr){ int n = arr.length; st = new long[n][25]; log = new int[n+2]; build_log(n+1); build(arr); } private void build(long[] arr) { int n = arr.length; for(int i = n-1 ; i>=0 ; i--) { for(int j = 0 ; j<25 ; j++) { int r = i + (1<<j)-1; if(r>=n) break; if(j == 0 ) st[i][j] = arr[i]; else st[i][j] = Math.min(st[i][j-1] , st[ i + ( 1 << (j-1) ) ][ j-1 ] ); } } } public long gcd(long a ,long b) { if(a == 0) return b; return gcd(b%a , a); } public long query(int l ,int r) { int w = r-l+1; int power = log[w]; return Math.min(st[l][power],st[r - (1<<power) + 1][power]); } private int[] log; void build_log(int n) { log[1] = 0; for(int i = 2 ; i<=n ; i++) { log[i] = 1 + log[i/2]; } } } /******************************************************** Segement Tree *****************************************************/ static class SegmentTree{ long[] tree; long[] arr; int n; SegmentTree(long[] arr){ this.n = arr.length; tree = new long[4*n+1]; this.arr = arr; buildTree(0, n-1, 1); } void buildTree(int s ,int e ,int index ) { if(s == e) { tree[index] = arr[s]; return; } int mid = (s+e)/2; buildTree( s, mid, 2*index); buildTree( mid+1, e, 2*index+1); tree[index] = gcd(tree[2*index] , tree[2*index+1]); } long query(int si ,int ei) { return query(0 ,n-1 , si ,ei , 1 ); } private long query( int ss ,int se ,int qs , int qe,int index) { if(ss>=qs && se<=qe) return tree[index]; if(qe<ss || se<qs) return (long)(0); int mid = (ss + se)/2; long left = query( ss , mid , qs ,qe , 2*index); long right= query(mid + 1 , se , qs ,qe , 2*index+1); return gcd(left, right); } public void update(int index , int val) { arr[index] = val; // for(long e:arr) System.out.print(e+" "); update(index , 0 , n-1 , 1); } private void update(int id ,int si , int ei , int index) { if(id < si || id>ei) return; if(si == ei ) { tree[index] = arr[id]; return; } if(si > ei) return; int mid = (ei + si)/2; update( id, si, mid , 2*index); update( id , mid+1, ei , 2*index+1); tree[index] = Math.min(tree[2*index] ,tree[2*index+1]); } } /* ***************************************************************************************************************************************************/ // static MyScanner sc = new MyScanner(); // only in case of less memory static Reader sc = new Reader(); static int TC; static StringBuilder sb = new StringBuilder(); static PrintWriter out=new PrintWriter(System.out); public static void main(String args[]) throws IOException { int tc = 1; // tc = sc.nextInt(); TC = 0; for(int i = 1 ; i<=tc ; i++) { TC++; // sb.append("Case #" + i + ": " ); // During KickStart && HackerCup TEST_CASE(); } System.out.print(sb); } static void TEST_CASE() throws IOException { int n = sc.nextInt(); int p = sc.nextInt(); int[] a = new int[n]; for(int i = 0 ; i<n ; i++) a[i] = sc.nextInt(); PriorityQueue<Long> tree1 = new PriorityQueue<>(); Set<Long> tree = new HashSet<>(); for(int e:a) tree1.add((long)e); while(!tree1.isEmpty()) { long e = tree1.poll(); if(get(e, tree)) continue; tree.add(e); } mod = (long)(1e9 + 7); long[] dp = new long[p+1]; dp[p-1] = 1l; for(int i = p-2 ; i>=0 ; i--) { dp[i] = (dp[i+1] + dp[i+2]+1)%mod; } // for(int i = 0 ; i<=p ; i++) System.out.println(i+" "+dp[i]); // System.out.println(tree); long ans = 0; for(long e:tree) { long in = bit(e); // System.out.println(e+" - " +in); if(in < p) { ans = (ans + dp[(int)in])%mod; } } sb.append(ans+"\n"); } static boolean get(long num , Set<Long> set) { if(set.contains(num)) return true; if(num %4 == 0 && get(num/4 , set ) ) return true; if((num-1)%2 == 0 && num>1 && get((num-1)/2, set)) return true; return false; } static long bit(long num) { int max = 0; for(int i =0 ; i<31 ; i++) { if((num&(1l<<i)) != 0) max = max( i , max); } return max; } } /*******************************************************************************************************************************************************/ /** */
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
38e4bf1e2e049b98331e38e81df0ede9
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
//Utilities import java.io.*; import java.util.*; public class a { static int t; static int n, p; static int[] a; static long[] dp; static long res; static final int MOD = (int)1e9+7; public static void main(String[] args) throws IOException { t = 1; while (t-- > 0) { n = in.iscan(); p = in.iscan(); a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.iscan(); } dp = new long[p]; for (int i = p-1; i >= 0; i--) { dp[i] = 1; if (i + 1 < p) dp[i] = (dp[i] + dp[i+1]) % MOD; if (i + 2 < p) dp[i] = (dp[i] + dp[i+2]) % MOD; } res = 0; HashSet<Long> set = new HashSet<Long>(); for (long x : a) { int pow2 = 0; while ((1L << (pow2+1)) <= x) { pow2++; } if (pow2 < p) { res = (res + dp[pow2]) % MOD; set.add(x); } } for (long x : set) { int pow2 = 0; while ((1L << (pow2+1)) <= x) { pow2++; } long tmp = x; while (true) { boolean flag = false; if ((tmp - 1) % 2 == 0 && (tmp - 1) / 2 >= 1) { tmp = (tmp - 1) / 2; flag = true; } else if (tmp % 4 == 0 && tmp / 4 >= 1) { tmp = tmp / 4; flag = true; } if (flag && set.contains(tmp)) { res = ((res - dp[pow2]) % MOD + MOD) % MOD; break; } if (!flag) break; } } out.println(res); } out.close(); } static INPUT in = new INPUT(System.in); static PrintWriter out = new PrintWriter(System.out); private static class INPUT { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar, numChars; public INPUT (InputStream stream) { this.stream = stream; } public INPUT (String file) throws IOException { this.stream = new FileInputStream (file); } public int cscan () throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read (buf); } if (numChars == -1) return numChars; return buf[curChar++]; } public int iscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } int res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public String sscan () throws IOException { int c = cscan (); while (space (c)) c = cscan (); StringBuilder res = new StringBuilder (); do { res.appendCodePoint (c); c = cscan (); } while (!space (c)); return res.toString (); } public double dscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } double res = 0; while (!space (c) && c != '.') { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); res *= 10; res += c - '0'; c = cscan (); } if (c == '.') { c = cscan (); double m = 1; while (!space (c)) { if (c == 'e' || c == 'E') return res * UTILITIES.fast_pow (10, iscan ()); m /= 10; res += (c - '0') * m; c = cscan (); } } return res * sgn; } public long lscan () throws IOException { int c = cscan (), sgn = 1; while (space (c)) c = cscan (); if (c == '-') { sgn = -1; c = cscan (); } long res = 0; do { res = (res << 1) + (res << 3); res += c - '0'; c = cscan (); } while (!space (c)); return res * sgn; } public boolean space (int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } public static class UTILITIES { static final double EPS = 10e-6; public static void sort(int[] a, boolean increasing) { ArrayList<Integer> arr = new ArrayList<Integer>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(long[] a, boolean increasing) { ArrayList<Long> arr = new ArrayList<Long>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static void sort(double[] a, boolean increasing) { ArrayList<Double> arr = new ArrayList<Double>(); int n = a.length; for (int i = 0; i < n; i++) { arr.add(a[i]); } Collections.sort(arr); for (int i = 0; i < n; i++) { if (increasing) { a[i] = arr.get(i); } else { a[i] = arr.get(n-1-i); } } } public static int lower_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= x) high = mid; else low = mid + 1; } return low; } public static int upper_bound (int[] arr, int x) { int low = 0, high = arr.length, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > x) high = mid; else low = mid + 1; } return low; } public static void updateMap(HashMap<Integer, Integer> map, int key, int v) { if (!map.containsKey(key)) { map.put(key, v); } else { map.put(key, map.get(key) + v); } if (map.get(key) == 0) { map.remove(key); } } public static long gcd (long a, long b) { return b == 0 ? a : gcd (b, a % b); } public static long lcm (long a, long b) { return a * b / gcd (a, b); } public static long fast_pow_mod (long b, long x, int mod) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow_mod (b * b % mod, x / 2, mod) % mod; return b * fast_pow_mod (b * b % mod, x / 2, mod) % mod; } public static long fast_pow (long b, long x) { if (x == 0) return 1; if (x == 1) return b; if (x % 2 == 0) return fast_pow (b * b, x / 2); return b * fast_pow (b * b, x / 2); } public static long choose (long n, long k) { k = Math.min (k, n - k); long val = 1; for (int i = 0; i < k; ++i) val = val * (n - i) / (i + 1); return val; } public static long permute (int n, int k) { if (n < k) return 0; long val = 1; for (int i = 0; i < k; ++i) val = (val * (n - i)); return val; } // start of permutation and lower/upper bound template public static void nextPermutation(int[] nums) { //find first decreasing digit int mark = -1; for (int i = nums.length - 1; i > 0; i--) { if (nums[i] > nums[i - 1]) { mark = i - 1; break; } } if (mark == -1) { reverse(nums, 0, nums.length - 1); return; } int idx = nums.length-1; for (int i = nums.length-1; i >= mark+1; i--) { if (nums[i] > nums[mark]) { idx = i; break; } } swap(nums, mark, idx); reverse(nums, mark + 1, nums.length - 1); } public static void swap(int[] nums, int i, int j) { int t = nums[i]; nums[i] = nums[j]; nums[j] = t; } public static void reverse(int[] nums, int i, int j) { while (i < j) { swap(nums, i, j); i++; j--; } } static int lower_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] >= cmp) high = mid; else low = mid + 1; } return low; } static int upper_bound (int[] arr, int hi, int cmp) { int low = 0, high = hi, mid = -1; while (low < high) { mid = (low + high) / 2; if (arr[mid] > cmp) high = mid; else low = mid + 1; } return low; } // end of permutation and lower/upper bound template } }
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
cbc2de81422591ceff5f48680b5a56e3
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.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.security.cert.X509CRL; import java.util.*; import java.lang.*; import java.util.stream.Collector; import java.util.stream.Collectors; @SuppressWarnings("unused") public class Main { static InputStream is; static PrintWriter out; static String INPUT = ""; static String OUTPUT = ""; //global //private final static long BASE = 998244353L; private final static int ALPHABET = (int)('z') - (int)('a') + 1; private final static int BASE = 1000000007; private final static int INF_I = (1<<31)-1; private final static long INF_L = (1l<<63)-1; private final static int MAXN = 200100; private final static int MAXK = 31; private final static int[] DX = {-1,0,1,0}; private final static int[] DY = {0,1,0,-1}; static void solve() { int ntest = 1; for (int test=0;test<ntest;test++) { int N = readInt(), P = readInt(); long[] fibo = new long[P+1]; fibo[0] = 1l; fibo[1] = 1l; for (int i=2;i<=P;i++) fibo[i] = (fibo[i-1] + fibo[i-2]) % BASE; for (int i=1;i<=P;i++) fibo[i] = (fibo[i] + fibo[i-1]) % BASE; int[] A = readIntArray(N); Arrays.sort(A); long ans = 0; Set<Integer> inSet = new HashSet<>(); for (int i=0;i<N;i++) { int a = A[i]; Deque<Integer> bit = new LinkedList<>(); TreeSet<Integer> pos1 = new TreeSet<>(); while (a>0) { int b = a%2; bit.addFirst(b); a/=2; if (b==1) pos1.add(bit.size()); } //int[] leng0 = pos1.toArray(); int[] leng0 = new int[pos1.size()]; int k=0; for (int x:pos1) leng0[k++] = x; for (int j=leng0.length-1;j>=1;j--) leng0[j] = leng0[j] - leng0[j-1] - 1; leng0[0] -= 1; int[] parity = new int[leng0.length]; for (int j=0;j<leng0.length;j++) parity[j] = leng0[j]%2; for (int j=1;j<leng0.length;j++) parity[j] |= parity[j-1]; int cnt0=0; int curr=0; k=0; //int[] bitArr = bit.toArray(); int[] bitArr = new int[bit.size()]; for (int x:bit) bitArr[k++] = x; boolean isMet = false; int all = leng0.length-1; if (bitArr[0]==1) all+=1; for (int j=0;j<bitArr.length;j++) { int x = bitArr[j]; curr = curr * 2 + x; if (x==1) { all-=1; cnt0=0; } else { cnt0+=1; } int next = 0; if (all > 0) next = parity[all-1]; if (inSet.contains(curr) && ((next | (leng0[all]-cnt0)%2)==0)) { isMet = true; break; } } if (!isMet && bit.size() <= P) ans = (ans + fibo[P - bit.size()]) % BASE; inSet.add(A[i]); } out.println(ans); } } public static void main(String[] args) throws Exception { long S = System.currentTimeMillis(); if (INPUT=="") { is = System.in; } else { File file = new File(INPUT); is = new FileInputStream(file); } if (OUTPUT == "") out = new PrintWriter(System.out); else out = new PrintWriter(OUTPUT); solve(); out.flush(); long G = System.currentTimeMillis(); } private static class Point<T extends Number & Comparable<T>> implements Comparable<Point<T>> { private T x; private T y; public Point(T x, T y) { this.x = x; this.y = y; } public T getX() {return x;} public T getY() {return y;} @Override public int compareTo(Point<T> o) { int cmp = x.compareTo(o.getX()); if (cmp==0) return y.compareTo(o.getY()); return cmp; } } private static class ClassComparator<T extends Comparable<T>> implements Comparator<T> { public ClassComparator() {} @Override public int compare(T a, T b) { return a.compareTo(b); } } private static class ListComparator<T extends Comparable<T>> implements Comparator<List<T>> { public ListComparator() {} @Override public int compare(List<T> o1, List<T> o2) { for (int i = 0; i < Math.min(o1.size(), o2.size()); i++) { int c = o1.get(i).compareTo(o2.get(i)); if (c != 0) { return c; } } return Integer.compare(o1.size(), o2.size()); } } private static boolean eof() { if(lenbuf == -1)return true; int lptr = ptrbuf; while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false; try { is.mark(1000); while(true){ int b = is.read(); if(b == -1){ is.reset(); return true; }else if(!isSpaceChar(b)){ is.reset(); return false; } } } catch (IOException e) { return true; } } private static byte[] inbuf = new byte[1024]; static int lenbuf = 0, ptrbuf = 0; private static int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } // private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); } private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private static double readDouble() { return Double.parseDouble(readString()); } private static char readChar() { return (char)skip(); } private static String readString() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private static char[] readChar(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private static char[][] readTable(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = readChar(m); return map; } private static int[] readIntArray(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = readInt(); return a; } private static long[] readLongArray(int n) { long[] a = new long[n]; for (int i=0;i<n;i++) a[i] = readLong(); return a; } private static int readInt() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static long readLong() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); } }
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
f6101b9290f46c9d12e21209338b22af
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 D { public void prayGod() throws IOException { int n = nextInt(), p = nextInt(); int[] a = nextIntArray(n); sort(a); HashSet<Integer> seen = new HashSet<>(); int[] count = new int[30]; for (int i = 0; i < n; i++) { String curr = Integer.toBinaryString(a[i]); boolean existed = false; int parent = 0; for (int j = 0; j < curr.length(); j++) { int bit = curr.charAt(j) - 48; parent = parent * 2 + bit; if (seen.contains(parent) && buildable(curr.substring(j + 1))) { existed = true; break; } } if (!existed) { count[curr.length() - 1]++; seen.add(a[i]); } } long[] dp = new long[p]; long ret = 0; for (int i = 0; i < p; i++) { if (i - 2 >= 0) dp[i] = (dp[i - 2] + dp[i]) % mod; if (i - 1 >= 0) dp[i] = (dp[i - 1] + dp[i]) % mod; if (i < 30) { dp[i] = (dp[i] + count[i]) % mod; } ret = (ret + dp[i]) % mod; } out.println(ret); } public boolean buildable(String s) { if (s.length() == 0) return true; int cnt = 0, ptr = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != s.charAt(ptr)) { if (s.charAt(ptr) == '0' && cnt % 2 == 1) return false; ptr = i; cnt = 1; } else cnt++; } return s.charAt(ptr) != '0' || cnt % 2 == 0; } public long binpow(long a, long b) { if (b < 0) return 0; long ret = 1, curr = a; while (b > 0) { if (b % 2 == 1) ret = (ret * curr) % mod; b /= 2; curr = (curr * curr) % mod; } return ret; } public void printVerdict(boolean verdict) { if (verdict) out.println("YES"); else out.println("NO"); } static final boolean RUN_TIMING = true; static final boolean AUTOFLUSH = false; static final boolean FILE_INPUT = false; static final boolean FILE_OUTPUT = false; static int iinf = 0x3f3f3f3f; static long inf = (long) 1e18 + 10; static long mod = 1000000007; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), AUTOFLUSH); // int data-type public int nextInt() throws IOException { return Integer.parseInt(next()); } 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 void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // long data-type public long nextLong() throws IOException { return Long.parseLong(next()); } 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 static void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public void sort(long[] a) { shuffle(a); Arrays.sort(a); } // double data-type public double nextDouble() throws IOException { return Double.parseDouble(next()); } 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 static void printArray(double[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // Generic type public <T> void sort(T[] a) { shuffle(a); Arrays.sort(a); } public static <T> void printArray(T[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public String next() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char) c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String nextLine() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char) c; len++; } return new String(inputBuffer, 0, len); } public boolean hasNext() throws IOException { String line = nextLine(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public void shuffle(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(Object[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public static void main(String[] args) throws IOException { if (FILE_INPUT) in = new PushbackReader(new BufferedReader(new FileReader(new File("output.txt"))), 1 << 20); if (FILE_OUTPUT) out = new PrintWriter(new FileWriter(new File("output.txt"))); long time = 0; time -= System.nanoTime(); new D().prayGod(); time += System.nanoTime(); if (RUN_TIMING) System.err.printf("%.3f ms%n", time / 1000000.0); out.flush(); in.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
b886809c655c647779982a48594e258d
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 D { public void prayGod() throws IOException { int n = nextInt(), p = nextInt(); int[] a = nextIntArray(n); sort(a); HashSet<Integer> seen = new HashSet<>(); int[][] count = new int[30][2]; for (int i = 0; i < n; i++) { String curr = Integer.toBinaryString(a[i]); boolean existed = false; int parent = 0; for (int j = 0; j < curr.length(); j++) { int bit = curr.charAt(j) - 48; parent = parent * 2 + bit; if (seen.contains(parent) && buildable(curr.substring(j + 1))) { existed = true; break; } } if (!existed) { count[curr.length() - 1][a[i] % 2]++; } seen.add(a[i]); } long[][] dp = new long[p][2]; long ret = 0; for (int i = 0; i < p; i++) { if (i - 2 >= 0) dp[i][0] = (dp[i - 2][0] + dp[i - 2][1]) % mod; if (i - 1 >= 0) dp[i][1] = (dp[i - 1][0] + dp[i - 1][1]) % mod; if (i < 30) { dp[i][0] = (dp[i][0] + count[i][0]) % mod; dp[i][1] = (dp[i][1] + count[i][1]) % mod; } ret = (ret + (dp[i][0] + dp[i][1]) % mod) % mod; } out.println(ret); } public boolean buildable(String s) { if (s.length() == 0) return true; int cnt = 0, ptr = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != s.charAt(ptr)) { if (s.charAt(ptr) == '0' && cnt % 2 == 1) return false; ptr = i; cnt = 1; } else cnt++; } return s.charAt(ptr) != '0' || cnt % 2 == 0; } public long binpow(long a, long b) { if (b < 0) return 0; long ret = 1, curr = a; while (b > 0) { if (b % 2 == 1) ret = (ret * curr) % mod; b /= 2; curr = (curr * curr) % mod; } return ret; } public void printVerdict(boolean verdict) { if (verdict) out.println("YES"); else out.println("NO"); } static final boolean RUN_TIMING = true; static final boolean AUTOFLUSH = false; static final boolean FILE_INPUT = false; static final boolean FILE_OUTPUT = false; static int iinf = 0x3f3f3f3f; static long inf = (long) 1e18 + 10; static long mod = 1000000007; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), AUTOFLUSH); // int data-type public int nextInt() throws IOException { return Integer.parseInt(next()); } 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 void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // long data-type public long nextLong() throws IOException { return Long.parseLong(next()); } 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 static void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public void sort(long[] a) { shuffle(a); Arrays.sort(a); } // double data-type public double nextDouble() throws IOException { return Double.parseDouble(next()); } 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 static void printArray(double[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // Generic type public <T> void sort(T[] a) { shuffle(a); Arrays.sort(a); } public static <T> void printArray(T[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public String next() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char) c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String nextLine() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char) c; len++; } return new String(inputBuffer, 0, len); } public boolean hasNext() throws IOException { String line = nextLine(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public void shuffle(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(Object[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public static void main(String[] args) throws IOException { if (FILE_INPUT) in = new PushbackReader(new BufferedReader(new FileReader(new File("output.txt"))), 1 << 20); if (FILE_OUTPUT) out = new PrintWriter(new FileWriter(new File("output.txt"))); long time = 0; time -= System.nanoTime(); new D().prayGod(); time += System.nanoTime(); if (RUN_TIMING) System.err.printf("%.3f ms%n", time / 1000000.0); out.flush(); in.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
b77b0354868c7a8861cf86300b7ae806
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 D { class TrieNode { TrieNode[] children; boolean endValue; TrieNode() { children = new TrieNode[2]; Arrays.fill(children, null); endValue = false; } } public void prayGod() throws IOException { int n = nextInt(), p = nextInt(); long[] a = nextLongArray(n); sort(a); TrieNode root = new TrieNode(); HashMap<Integer, Integer> countOdd = new HashMap<>(); HashMap<Integer, Integer> countEven = new HashMap<>(); for (int i = 0; i < n; i++) { String curr = Long.toBinaryString(a[i]); TrieNode ptr = root; boolean existed = false; for (int j = 0; j < curr.length(); j++) { int idx = curr.charAt(j) - 48; if (ptr.children[idx] == null) break; ptr = ptr.children[idx]; if (ptr.endValue && buildable(curr.substring(j + 1))) { existed = true; break; } } if (!existed) { if (a[i] % 2 == 1) countOdd.put(curr.length() - 1, countOdd.getOrDefault(curr.length() - 1, 0) + 1); else countEven.put(curr.length() - 1, countEven.getOrDefault(curr.length() - 1, 0) + 1); } ptr = root; for (int j = 0; j < curr.length(); j++) { int idx = curr.charAt(j) - 48; if (ptr.children[idx] == null) ptr.children[idx] = new TrieNode(); ptr = ptr.children[idx]; } ptr.endValue = true; } long[][] dp = new long[p][2]; long ret = 0; for (int i = 0; i < p; i++) { if (i - 2 >= 0) dp[i][0] = (dp[i - 2][0] + dp[i - 2][1]) % mod; if (i - 1 >= 0) dp[i][1] = (dp[i - 1][0] + dp[i - 1][1]) % mod; dp[i][0] = (dp[i][0] + countEven.getOrDefault(i, 0)) % mod; dp[i][1] = (dp[i][1] + countOdd.getOrDefault(i, 0)) % mod; ret = (ret + (dp[i][0] + dp[i][1]) % mod) % mod; } out.println(ret); } public boolean buildable(String s) { if (s.length() == 0) return true; int cnt = 0, ptr = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) != s.charAt(ptr)) { if (s.charAt(ptr) == '0' && cnt % 2 == 1) return false; ptr = i; cnt = 1; } else cnt++; } return s.charAt(ptr) != '0' || cnt % 2 == 0; } public long binpow(long a, long b) { if (b < 0) return 0; long ret = 1, curr = a; while (b > 0) { if (b % 2 == 1) ret = (ret * curr) % mod; b /= 2; curr = (curr * curr) % mod; } return ret; } public void printVerdict(boolean verdict) { if (verdict) out.println("YES"); else out.println("NO"); } static final boolean RUN_TIMING = true; static final boolean AUTOFLUSH = false; static final boolean FILE_INPUT = false; static final boolean FILE_OUTPUT = false; static int iinf = 0x3f3f3f3f; static long inf = (long) 1e18 + 10; static long mod = 1000000007; static char[] inputBuffer = new char[1 << 20]; static PushbackReader in = new PushbackReader(new BufferedReader(new InputStreamReader(System.in)), 1 << 20); static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), AUTOFLUSH); // int data-type public int nextInt() throws IOException { return Integer.parseInt(next()); } 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 void sort(int[] a) { shuffle(a); Arrays.sort(a); } public static void printArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // long data-type public long nextLong() throws IOException { return Long.parseLong(next()); } 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 static void printArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public void sort(long[] a) { shuffle(a); Arrays.sort(a); } // double data-type public double nextDouble() throws IOException { return Double.parseDouble(next()); } 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 static void printArray(double[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } // Generic type public <T> void sort(T[] a) { shuffle(a); Arrays.sort(a); } public static <T> void printArray(T[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(); } public String next() throws IOException { int len = 0; int c; do { c = in.read(); } while (Character.isWhitespace(c) && c != -1); if (c == -1) { throw new NoSuchElementException("Reached EOF"); } do { inputBuffer[len] = (char) c; len++; c = in.read(); } while (!Character.isWhitespace(c) && c != -1); while (c != '\n' && Character.isWhitespace(c) && c != -1) { c = in.read(); } if (c != -1 && c != '\n') { in.unread(c); } return new String(inputBuffer, 0, len); } public String nextLine() throws IOException { int len = 0; int c; while ((c = in.read()) != '\n' && c != -1) { if (c == '\r') { continue; } inputBuffer[len] = (char) c; len++; } return new String(inputBuffer, 0, len); } public boolean hasNext() throws IOException { String line = nextLine(); if (line.isEmpty()) { return false; } in.unread('\n'); in.unread(line.toCharArray()); return true; } public void shuffle(int[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(long[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); long temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public void shuffle(Object[] arr) { int n = arr.length; for (int i = 0; i < n; i++) { int j = (int) (Math.random() * (n - i)); Object temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } public static void main(String[] args) throws IOException { if (FILE_INPUT) in = new PushbackReader(new BufferedReader(new FileReader(new File("output.txt"))), 1 << 20); if (FILE_OUTPUT) out = new PrintWriter(new FileWriter(new File("output.txt"))); long time = 0; time -= System.nanoTime(); new D().prayGod(); time += System.nanoTime(); if (RUN_TIMING) System.err.printf("%.3f ms%n", time / 1000000.0); out.flush(); in.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
89106db06fb48ce196a15e906d0d85fe
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 javax.sound.midi.Soundbank; import java.awt.*; import java.io.*; import java.util.*; public class Main { static int N = 100001; // 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; } 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; } } public static long ncp(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; } public static void main(String[] args) throws IOException { Reader.init(System.in); BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out)); int t =1; while (t > 0){ int n = Reader.nextInt(); int p = Reader.nextInt(); long dp[] = new long[200001]; int i = 0; int[] array = new int[n]; PriorityQueue<Integer> pq = new PriorityQueue<>(); while (i < n){ array[i] = Reader.nextInt(); pq.add(array[i]); i++; } i = 0; while ( i < n){ array[i] = pq.poll(); i++; } ArrayList<Integer> usefull = new ArrayList<>(); i = 0; while ( i < n){ int v = array[i]; boolean notFound = true; while ( v != 0){ if ( (v&1) == 1){ v = v >> 1; if ( bs(0,i-1,array,v) != -1){ notFound = false; break; } } else{ v = v >> 1; if ( (v&1) == 1){ break; } else { v = v>>1; if ( bs(0,i-1,array,v) != -1) { notFound = false; break; } } } } if( notFound){ usefull.add(array[i]); } i++; } i = 0; while ( i < usefull.size()){ int u = usefull.get(i); int c = 0; while ( u != 0){ u = u>>1; c++; } dp[c-1]+=1; i++; } i = 1; while ( i < p){ if ( i == 1){ dp[i]+=dp[i-1]; } else{ dp[i]+=dp[i-1]; dp[i]+=dp[i-2]; } dp[i] = dp[i]%1000000007; i++; } i = 0; long ans = 0; while ( i < p){ ans+=dp[i]; ans%=1000000007; i++; } output.write(ans+"\n"); t--; } output.flush(); } public static long log2(long N) { long result = (long)(Math.log(N) / Math.log(2)); return result; } private static int bs(int low,int high,int [] array,int find){ if ( low <= high ){ int mid = low + (high-low)/2; if ( array[mid] > find){ high = mid -1; return bs(low,high,array,find); } else if ( array[mid] < find){ low = mid+1; return bs(low,high,array,find); } return mid; } return -1; } private static long max(long a, long b) { return Math.max(a,b); } private static long min(long a,long b){ return Math.min(a,b); } public static long modularExponentiation(long a,long b,long mod){ if ( b == 1){ return a; } else{ long ans = modularExponentiation(a,b/2,mod)%mod; if ( b%2 == 1){ return (a*((ans*ans)%mod))%mod; } return ((ans*ans)%mod); } } public static long sum(long n){ return (n*(n+1))/2; } public static long abs(long a){ return a < 0 ? (-1*a) : a; } public static long gcd(long a,long b){ if ( a == 0){ return b; } else{ return gcd(b%a,a); } } } class Reader { 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() ) { 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()); } } class NComparator implements Comparator<Node>{ @Override public int compare(Node o1, Node o2) { if ( o2.b > o1.b){ return 1; } else if ( o2.b < o1.b){ return -1; } else { return 0; } } } class DComparator implements Comparator<Character>{ @Override public int compare(Character o1, Character o2) { if ( o2 < o1){ return 1; } else if ( o2 > o1){ return -1; } else{ return 0; } } } class Node{ String a; int b; Node(String A,int B){ a = A; b = B; } } /* 4 4 bbaa abba abaa aaaa */
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
d5e8ce45a1aa8d8cde0928dfd876e022
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 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 FastReader obj = new FastReader(); public static PrintWriter out = new PrintWriter(System.out); public static int mod=1000000007; public static void sort(int[] a) { ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } public static void revsort(long[] a) { ArrayList<Long> arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr, Collections.reverseOrder()); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } //Cover the small test cases like for n=1 . public static class pair { long a; long b; pair(long x, long y) { a = x; b = y; } } public static long l() { return obj.nextLong(); } public static int i() { return obj.nextInt(); } public static String s() { return obj.next(); } public static long[] l(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = l(); return arr; } public static int[] i(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = i(); return arr; } public static long ceil(long a, long b) { return (a + b - 1) / b; } public static void p(long val) { out.println(val); } public static void p(String s) { out.println(s); } public static void pl(long[] arr) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + " "); } out.println(); } public static void p(int[] arr) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + " "); } out.println(); } public static void sortpair(Vector<pair> arr) { //ascending just change return 1 to return -1 and vice versa to get descending. //compare based on value of pair.a arr.sort(new Comparator<pair>() { public int compare(pair o1, pair o2) { long val = o1.a - o2.a; if (val == 0) return 0; else if (val > 0) return 1; else return -1; } }); } // Take of the small test cases such as when n=1,2 etc. // remember in case of fenwick tree ft is 1 based but our array should be 0 based. // in fenwick tree when we update some index it doesn't change the value to val but it // adds the val value in it so remember to add val-a[i] instead of just adding val. //in case of finding the inverse mod do it (biexpo(a,mod-2)%mod + mod )%mod public static HashSet<Integer> useful=new HashSet<>(); public static boolean check(int x) { if(x<=0)return true; if(useful.contains(x))return false; boolean ans=true; if(x%2!=0)ans=ans&check((x-1)/2); if(x%4==0)ans=ans&check(x/4); return ans; } public static int c(int a) { return 31-Integer.numberOfLeadingZeros(a); } public static void main(String[] args) { int len =1; while (len-- != 0) { int n = i(); int p=i(); int[] a=i(n); sort(a); for(int i=0;i<n;i++) { if(check(a[i])) { useful.add(a[i]); } } long[] dp=new long[p]; for(int i:useful) { int val=c(i); if(val<p)dp[val]++; } long ans=0; for(int i=0;i<p;i++) { if(i>=1) { dp[i]+=dp[i-1]; dp[i]%=mod; } if(i>=2) { dp[i]+=dp[i-2]; dp[i]%=mod; } ans+=dp[i]%mod; ans=ans%mod; } out.println(ans); } 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
22f937617ed64526f8d65276feddab22
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 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 FastReader obj = new FastReader(); public static PrintWriter out = new PrintWriter(System.out); public static void sort(int[] a) { ArrayList<Integer> arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } public static void revsort(long[] a) { ArrayList<Long> arr = new ArrayList<>(); for (int i = 0; i < a.length; i++) arr.add(a[i]); Collections.sort(arr, Collections.reverseOrder()); for (int i = 0; i < arr.size(); i++) a[i] = arr.get(i); } //Cover the small test cases like for n=1 . public static class pair { long a; long b; pair(long x, long y) { a = x; b = y; } } public static long l() { return obj.nextLong(); } public static int i() { return obj.nextInt(); } public static String s() { return obj.next(); } public static long[] l(int n) { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = l(); return arr; } public static int[] i(int n) { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = i(); return arr; } public static long ceil(long a, long b) { return (a + b - 1) / b; } public static void p(long val) { out.println(val); } public static void p(String s) { out.println(s); } public static void pl(long[] arr) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + " "); } out.println(); } public static void p(int[] arr) { for (int i = 0; i < arr.length; i++) { out.print(arr[i] + " "); } out.println(); } public static void sortpair(Vector<pair> arr) { //ascending just change return 1 to return -1 and vice versa to get descending. //compare based on value of pair.a arr.sort(new Comparator<pair>() { public int compare(pair o1, pair o2) { long val = o1.a - o2.a; if (val == 0) return 0; else if (val > 0) return 1; else return -1; } }); } // Take of the small test cases such as when n=1,2 etc. // remember in case of fenwick tree ft is 1 based but our array should be 0 based. // in fenwick tree when we update some index it doesn't change the value to val but it // adds the val value in it so remember to add val-a[i] instead of just adding val. //in case of finding the inverse mod do it (biexpo(a,mod-2)%mod + mod )%mod public static HashSet<Integer> useful=new HashSet<>(); public static boolean check(int n) { if(n<=0)return true; if(useful.contains(n)) return false; boolean ans=true; if(n%2!=0)ans=ans&check((n-1)/2); if(n%4==0)ans=ans&check(n/4); return ans; } public static int c(int a) { return 31-Integer.numberOfLeadingZeros(a); } public static int mod=1000000007; public static void main(String[] args) { int len = 1; while (len-- != 0) { int n = i(); int p=i(); int[] a=new int[n]; for(int i=0;i<n;i++) a[i]=obj.nextInt(); sort(a); for(int i=0;i<n;i++)if(check(a[i]))useful.add(a[i]); int[] cnt=new int[31]; for(int i:useful) cnt[c(i)]++; long ans=0; long[] dp=new long[p]; for(int i=0;i<p;i++) { if(i<30)dp[i]=cnt[i]; if(i>=1) { dp[i]+=dp[i-1]; if(dp[i]>=mod) dp[i]-=mod; } if(i>=2) { dp[i]+=dp[i-2]; if(dp[i]>=mod) dp[i]-=mod; } ans+=dp[i]; if(ans>=mod) ans-=mod; } out.println(ans); } 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
f03be037caa0b977d4680f0bc1d0be5a
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 InfiniteSet { //io static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static PrintWriter out = new PrintWriter(System.out); static boolean debug = false; //param static TreeSet<Integer> useful = new TreeSet<>(); static int[] all; static int N; static int P; //const static final long MOD=(long)1e9+7; public static void main(String[] args) throws IOException { //parse input StringTokenizer st = new StringTokenizer(br.readLine()); N = Integer.parseInt(st.nextToken()); P = Integer.parseInt(st.nextToken()); all = new int[N]; st = new StringTokenizer(br.readLine()); for (int i=0;i<N;i++){ all[i] = Integer.parseInt(st.nextToken()); } Arrays.sort(all); if (debug) System.out.println(Arrays.toString(all)); //logic //create useful treeSet for (int i=0;i<N;i++){ if (isUseful(all[i])) useful.add(all[i]); } if (debug) System.out.println(useful); //fill this int[] usefulWithLen = new int[Math.max(40,P+1)]; for (int u : useful){ usefulWithLen[size(u)]++; } if (debug) System.out.println(Arrays.toString(usefulWithLen)); //do dp: dp[i]=dp[i-1]+dp[i-2]+usefulWithLen[i] long[] dp = new long[P+1]; dp[1]=usefulWithLen[1]; if (P>1) dp[2]=usefulWithLen[2]+dp[1]; for (int i=3;i<=P;i++){ dp[i]=(dp[i-1]+dp[i-2]+usefulWithLen[i])%MOD; } //turn in answer if (debug) System.out.println(Arrays.toString(dp)); long ans=0; for (int i=1;i<=P;i++)ans=(ans+dp[i])%MOD; out.println(ans); out.close(); } static boolean isUseful(int num){ while (num > 0){ if (num%2==1){ num>>=1; } else if (num%4==0){ num>>=2; } else { break; } if (useful.contains(num)) return false; } return true; } static int size(int num){ return (int)(Math.log(num)/Math.log(2)+1); } }
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
7a3e1e53e3d6b18c49c2c00ce4ba3879
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.Arrays; import java.util.HashSet; import java.util.StringTokenizer; public class D { static int mod= (int) (1e9+7); public static void main(String[] args) { FastScanner sc=new FastScanner(); int t=1; PrintWriter pw=new PrintWriter(System.out); while(t-->0) { int n=sc.nextInt(); int p=sc.nextInt(); int[] a=sc.readArray(n); Arrays.sort(a); HashSet<Integer> useful=new HashSet<Integer>(); for(int i=0;i<n;i++){ int x=a[i]; boolean flag=true; while(x>0){ if(useful.contains(x)){ flag=false; break; } if(x%2==1){ x=x>>1; } else if (x%4==0){ x>>=2; } else { break; } } if(flag){ useful.add(a[i]); } } int[] cnt=new int[31]; for(int x:useful){ cnt[(int) (Math.log10(x)/Math.log10(2))]++; } long[] dp=new long[p+1]; long ans=0; for(int i=0;i<p;i++){ if(i<=30){ dp[i]=cnt[i]; } if(i>=1){ dp[i]=(dp[i]+dp[i-1])%mod; } if(i>=2){ dp[i]=(dp[i]+dp[i-2])%mod; } ans=(ans+dp[i])%mod; } pw.println(ans); } pw.flush(); } 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
40884e8f47432ee8a3e894bb58763299
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.Arrays; import java.util.HashSet; import java.util.StringTokenizer; public class D { static int mod= (int) (1e9+7); public static void main(String[] args) { FastScanner sc=new FastScanner(); int t=1; PrintWriter pw=new PrintWriter(System.out); while(t-->0) { int n=sc.nextInt(); int p=sc.nextInt(); int[] a=sc.readArray(n); Arrays.sort(a); HashSet<Integer> useful=new HashSet<Integer>(); for(int i=0;i<n;i++){ int x=a[i]; boolean flag=true; while(x>0){ if(useful.contains(x)){ flag=false; break; } if(x%2==1){ x=x>>1; } else if ((x&3)>0){ break; } else { x>>=2; } } if(flag){ useful.add(a[i]); } } int[] cnt=new int[31]; for(int x:useful){ cnt[(int) (Math.log10(x)/Math.log10(2))]++; } long[] dp=new long[p+1]; long ans=0; for(int i=0;i<p;i++){ if(i<=30){ dp[i]=cnt[i]; } if(i>=1){ dp[i]=(dp[i]+dp[i-1])%mod; } if(i>=2){ dp[i]=(dp[i]+dp[i-2])%mod; } ans=(ans+dp[i])%mod; } pw.println(ans); } pw.flush(); } 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
72df30dbae663aba746de2d23a723584
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.Arrays; import java.util.HashSet; import java.util.StringTokenizer; public class D { static int mod= (int) (1e9+7); public static void main(String[] args) { FastScanner sc=new FastScanner(); int t=1; PrintWriter pw=new PrintWriter(System.out); while(t-->0) { int n=sc.nextInt(); int p=sc.nextInt(); int[] a=sc.readArray(n); Arrays.sort(a); HashSet<Integer> useful=new HashSet<Integer>(); for(int i=0;i<n;i++){ int x=a[i]; boolean flag=true; while(x>0){ if(useful.contains(x)){ flag=false; } if(x%2==1){ x=x>>1; } else if ((x&3)>0){ break; } else { x>>=2; } } if(flag){ useful.add(a[i]); } } int[] cnt=new int[31]; for(int x:useful){ cnt[(int) (Math.log10(x)/Math.log10(2))]++; } long[] dp=new long[p+1]; long ans=0; for(int i=0;i<p;i++){ if(i<=30){ dp[i]=cnt[i]; } if(i>=1){ dp[i]=(dp[i]+dp[i-1])%mod; } if(i>=2){ dp[i]=(dp[i]+dp[i-2])%mod; } ans=(ans+dp[i])%mod; } pw.println(ans); } pw.flush(); } 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
640a45b2f6aef89b569b93c9ec2d23be
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 CodeForces { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void solve(int tCase) throws IOException { 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(); _sort(arr,true); for(int i=0;i<n;i++) if(!isPresent(set,arr[i]))set.add(arr[i]); dp = new long[p+40]; for(int i=0;i<n;i++) if(set.contains(arr[i]))dp[_digitCount(arr[i],2)]++; long sum = dp[1]; for(int i=2;i<=p;i++){ dp[i]+=dp[i-1]; dp[i]+=dp[i-2]; dp[i] %= mod; sum += dp[i]; sum %=mod; } out.println(sum); } static long[] dp; private static boolean isPresent(Set<Integer> set, int x){ if(x==0)return false; if(set.contains(x))return true; if((x & 1)==1) return isPresent(set,(x-1)/2); if(x%4==0)return isPresent(set,x/4); return false; } public static void main(String[] args) throws IOException { long s = System.currentTimeMillis(); openIO(); int testCase = 1; // testCase = sc. nextInt(); for (int i = 1; i <= testCase; i++) solve(i); // out.println((System.currentTimeMillis() - s)/1000d); closeIO(); } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ /*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/ public static int mod = (int) 1e9 + 7; // public static int mod = 998244353; public static int inf_int = (int) 2e9; public static long inf_long = (long) 2e18; public static void _sort(int[] arr, boolean isAscending) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } public static void _sort(long[] arr, boolean isAscending) { int n = arr.length; List<Long> list = new ArrayList<>(); for (long ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // time : O(1), space : O(1) public static int _digitCount(long num,int base){ // this will give the # of digits needed for a number num in format : base return (int)(1 + Math.log(num)/Math.log(base)); } // time : O(n), space: O(n) public static long _fact(int n){ // simple factorial calculator long ans = 1; for(int i=2;i<=n;i++) ans = ans * i % mod; return ans; } // time for pre-computation of factorial and inverse-factorial table : O(nlog(mod)) public static long[] factorial , inverseFact; public static void _ncr_precompute(int n){ factorial = new long[n+1]; inverseFact = new long[n+1]; factorial[0] = inverseFact[0] = 1; for (int i = 1; i <=n; i++) { factorial[i] = (factorial[i - 1] * i) % mod; inverseFact[i] = _modExpo(factorial[i], mod - 2); } } // time of factorial calculation after pre-computation is O(1) public static int _ncr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod); } public static int _npr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[n - r] % mod); } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a>0){ long x = a; a = b % a; b = x; } return b; // if (a == 0) // return b; // return _gcd(b % a, a); } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // binary exponentiation time O(logn) public static long _modExpo(long x, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans *= x; ans %= mod; n--; } else { x *= x; x %= mod; n >>= 1; } } return ans; } // function to find a/b under modulo mod. time : O(logn) public static long _modInv(long a,long b){ return (a * _modExpo(b,mod-2)) % mod; } //sieve or first divisor time : O(mx * log ( log (mx) ) ) public static int[] _seive(int mx){ int[] firstDivisor = new int[mx+1]; for(int i=0;i<=mx;i++)firstDivisor[i] = i; for(int i=2;i*i<=mx;i++) if(firstDivisor[i] == i) for(int j = i*i;j<=mx;j+=i) firstDivisor[j] = i; return firstDivisor; } // check if x is a prime # of not. time : O( n ^ 1/2 ) private static boolean _isPrime(long x){ for(long i=2;i*i<=x;i++) if(x%i==0)return false; return true; } static class Pair<K, V>{ K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString(){ return ff.toString()+" "+ss.toString(); } } /*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/ /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException { sc = new FastestReader(); out = new PrintWriter(System.out); } public static void closeIO() throws IOException { out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) { } return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ }
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
44664466f61b2cb0f558177cc1468ccb
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 CodeForces { /*-------------------------------------------EDITING CODE STARTS HERE-------------------------------------------*/ public static void solve(int tCase) throws IOException { 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(); _sort(arr,true); for(int i=0;i<n;i++) if(!isPresent(set,arr[i]))set.add(arr[i]); dp = new long[p+40]; for(int i=0;i<n;i++) if(set.contains(arr[i]))dp[_digitCount(arr[i],2)]++; long sum = 0; for(int i=1;i<=p;i++){ dp[i]+=dp[i-1]; if(i-2>=0)dp[i]+=dp[i-2]; dp[i] %= mod; sum += dp[i]; sum %=mod; } out.println(sum); } static long[] dp; private static boolean isPresent(Set<Integer> set, int x){ if(x==0)return false; if(set.contains(x))return true; if((x & 1)==1) return isPresent(set,(x-1)/2); if(x%4==0)return isPresent(set,x/4); return false; } public static void main(String[] args) throws IOException { long s = System.currentTimeMillis(); openIO(); int testCase = 1; // testCase = sc. nextInt(); for (int i = 1; i <= testCase; i++) solve(i); // out.println((System.currentTimeMillis() - s)/1000d); closeIO(); } /*-------------------------------------------EDITING CODE ENDS HERE-------------------------------------------*/ /*--------------------------------------HELPER FUNCTIONS STARTS HERE-----------------------------------------*/ public static int mod = (int) 1e9 + 7; // public static int mod = 998244353; public static int inf_int = (int) 2e9; public static long inf_long = (long) 2e18; public static void _sort(int[] arr, boolean isAscending) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } public static void _sort(long[] arr, boolean isAscending) { int n = arr.length; List<Long> list = new ArrayList<>(); for (long ele : arr) list.add(ele); Collections.sort(list); if (!isAscending) Collections.reverse(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // time : O(1), space : O(1) public static int _digitCount(long num,int base){ // this will give the # of digits needed for a number num in format : base return (int)(1 + Math.log(num)/Math.log(base)); } // time : O(n), space: O(n) public static long _fact(int n){ // simple factorial calculator long ans = 1; for(int i=2;i<=n;i++) ans = ans * i % mod; return ans; } // time for pre-computation of factorial and inverse-factorial table : O(nlog(mod)) public static long[] factorial , inverseFact; public static void _ncr_precompute(int n){ factorial = new long[n+1]; inverseFact = new long[n+1]; factorial[0] = inverseFact[0] = 1; for (int i = 1; i <=n; i++) { factorial[i] = (factorial[i - 1] * i) % mod; inverseFact[i] = _modExpo(factorial[i], mod - 2); } } // time of factorial calculation after pre-computation is O(1) public static int _ncr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[r] % mod * inverseFact[n - r] % mod); } public static int _npr(int n,int r){ if(r > n)return 0; return (int)(factorial[n] * inverseFact[n - r] % mod); } // euclidean algorithm time O(max (loga ,logb)) public static long _gcd(long a, long b) { while (a>0){ long x = a; a = b % a; b = x; } return b; // if (a == 0) // return b; // return _gcd(b % a, a); } // lcm(a,b) * gcd(a,b) = a * b public static long _lcm(long a, long b) { return (a / _gcd(a, b)) * b; } // binary exponentiation time O(logn) public static long _modExpo(long x, long n) { long ans = 1; while (n > 0) { if ((n & 1) == 1) { ans *= x; ans %= mod; n--; } else { x *= x; x %= mod; n >>= 1; } } return ans; } // function to find a/b under modulo mod. time : O(logn) public static long _modInv(long a,long b){ return (a * _modExpo(b,mod-2)) % mod; } //sieve or first divisor time : O(mx * log ( log (mx) ) ) public static int[] _seive(int mx){ int[] firstDivisor = new int[mx+1]; for(int i=0;i<=mx;i++)firstDivisor[i] = i; for(int i=2;i*i<=mx;i++) if(firstDivisor[i] == i) for(int j = i*i;j<=mx;j+=i) firstDivisor[j] = i; return firstDivisor; } // check if x is a prime # of not. time : O( n ^ 1/2 ) private static boolean _isPrime(long x){ for(long i=2;i*i<=x;i++) if(x%i==0)return false; return true; } static class Pair<K, V>{ K ff; V ss; public Pair(K ff, V ss) { this.ff = ff; this.ss = ss; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || this.getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; return ff.equals(pair.ff) && ss.equals(pair.ss); } @Override public int hashCode() { return Objects.hash(ff, ss); } @Override public String toString(){ return ff.toString()+" "+ss.toString(); } } /*--------------------------------------HELPER FUNCTIONS ENDS HERE-----------------------------------------*/ /*-------------------------------------------FAST INPUT STARTS HERE---------------------------------------------*/ static FastestReader sc; static PrintWriter out; private static void openIO() throws IOException { sc = new FastestReader(); out = new PrintWriter(System.out); } public static void closeIO() throws IOException { out.flush(); out.close(); sc.close(); } private static final class FastestReader { private static final int BUFFER_SIZE = 1 << 16; private final DataInputStream din; private final byte[] buffer; private int bufferPointer, bytesRead; public FastestReader() { din = new DataInputStream(System.in); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } public FastestReader(String file_name) throws IOException { din = new DataInputStream(new FileInputStream(file_name)); buffer = new byte[BUFFER_SIZE]; bufferPointer = bytesRead = 0; } private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() throws IOException { int b; //noinspection StatementWithEmptyBody while ((b = read()) != -1 && isSpaceChar(b)) { } return b; } public String next() throws IOException { int b = skip(); final StringBuilder sb = new StringBuilder(); while (!isSpaceChar(b)) { // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = read(); } return sb.toString(); } public int nextInt() throws IOException { int ret = 0; byte c = read(); while (c <= ' ') { c = read(); } final 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(); } final 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(); } final 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 { din.close(); } } /*---------------------------------------------FAST INPUT ENDS HERE ---------------------------------------------*/ }
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
1198acd4e142434723d086484c2b8cff
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 { static int M = 1_000_000_007; static Random rng = new Random(); private static int testCase(int n, int p, int[] a) { Set<Integer> base = new HashSet<>(); int[] memo = new int[Math.max(34, p + 3)]; int ans = 0, curr; for (int ai : a) { base.add(ai); } sort(a); for (int i = n - 1; i >= 0; i--) { curr = a[i]; while (curr > 2 && (curr % 4 == 0 || curr % 2 == 1)) { if (curr % 4 == 0) { curr >>= 2; } else if (curr % 2 == 1) { curr >>= 1; } if (base.contains(curr)) { base.remove(a[i]); break; } } } for (int i = p; i >= 0; i--) { memo[i] = (memo[i + 1] + memo[i + 2] + 1) % M; } for (int num : base) { ans = (ans + memo[Integer.toBinaryString(num).length()]) % M; } return ans; } public static void main(String[] args) { FastScanner in = new FastScanner(); PrintWriter out = new PrintWriter(System.out); int t = 1; // Scanner has functions to read ints, longs, strings, chars, etc. //in.nextLine(); for (int tt = 1; tt <= t; ++tt) { int n = in.nextInt(), p = in.nextInt(); int[] a = in.readArray(n); out.println(testCase(n, p, a)); } out.close(); } private 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(); } boolean hasNext() { return st.hasMoreTokens(); } char[] readCharArray(int n) { char[] arr = new char[n]; try { br.read(arr); br.readLine(); } catch (IOException e) { e.printStackTrace(); } return arr; } 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[] readLongArray(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()); } } private static void sort(int[] arr) { int temp, idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static void sort(long[] arr) { long temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static <T> void sort(T[] arr) { T temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr); } private static <T> void sort(T[] arr, Comparator<? super T> cmp) { T temp; int idx; for (int i = arr.length - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = arr[i]; arr[i] = arr[idx]; arr[idx] = temp; } Arrays.sort(arr, cmp); } private static <T extends Comparable<? super T>> void sort(List<T> list) { T temp; int idx; for (int i = list.size() - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = list.get(i); list.set(i, list.get(idx)); list.set(idx, temp); } Collections.sort(list); } private static <T> void sort(List<T> list, Comparator<? super T> cmp) { T temp; int idx; for (int i = list.size() - 1; i > 0; i--) { idx = rng.nextInt(i + 1); temp = list.get(i); list.set(i, list.get(idx)); list.set(idx, temp); } Collections.sort(list, cmp); } static class DSU { int[] parent, rank; public DSU(int n) { parent = new int[n]; rank = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; } } public int find(int i) { if (parent[i] == i) { return i; } else { int res = find(parent[i]); parent[i] = res; return res; } } public boolean isSameSet(int i, int j) { return find(i) == find(j); } public void union(int i, int j) { int iParent = find(i), jParent = find(j); if (iParent != jParent) { if (rank[iParent] > rank[jParent]) { parent[jParent] = iParent; } else { parent[iParent] = jParent; if (rank[iParent] == rank[jParent]) { rank[jParent]++; } } } } } }
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
90e1541ad581f2fba12c996b2248f3e5
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 static java.lang.Integer.parseInt; import static java.lang.Long.parseLong; import static java.lang.Double.parseDouble; public class Main { public static void main(String[] args) throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); long MOD = 1000000007; int n = nextInt(); int p = nextInt(); long[] a = new long[n]; long[] menshe = new long[2000000]; long[] rovno = new long[2000000]; TreeSet<Long> treeSet = new TreeSet<>(); ArrayList<Long> arrayList = new ArrayList<>(); for (int i = 0; i < n; i++) { a[i] = nextLong(); arrayList.add(a[i]); treeSet.add(a[i]); } Arrays.sort(a); check(arrayList, treeSet); for (int i = 0; i < arrayList.size(); i++) { double log = Math.log((double)arrayList.get(i)) / Math.log(2); int intLog = (int) Math.ceil(log); if (log < Math.ceil(log)) { menshe[intLog]++; } else { rovno[intLog]++; } } int uk = 0; for (int i = 0; i <= p; i++) { menshe[i] %= MOD; menshe[i + 1] += menshe[i] % MOD; menshe[i + 2] += rovno[i] % MOD; menshe[i + 2] += menshe[i] % MOD; if (rovno[i] == 1) { rovno[i + 2] = 1; } } long ans = 0; for (int i = 0; i <= p; i++) { ans += menshe[i] % MOD; ans %= MOD; if (i != p) { ans += rovno[i]; } } ans %= MOD; out.println(ans); out.println(); in.close(); out.close(); } public static void check(ArrayList<Long> arrayList, TreeSet<Long> treeSet) { for (int j = arrayList.size() - 1; j >= 0; j--) { long numb = arrayList.get(j); while (numb > 0) { if (numb % 4 == 0) { numb /= 4; } else if (numb % 2 == 1) { numb /= 2; } else { break; } if (treeSet.contains(numb)) { arrayList.remove(j); break; } } } } static BufferedReader in; static PrintWriter out; static StringTokenizer tok; static int nextInt() throws IOException { return parseInt(next()); } static long nextLong() throws IOException { return parseLong(next()); } static double nextDouble() throws IOException { return parseDouble(next()); } static String next() throws IOException { while (tok == null || !tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); } return tok.nextToken(); } } class Triple { int x; int y; int z; public Triple(int x, int y, int z) { this.x = x; this.y = y; this.z = z; } }
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
0edd4b37532551a896091d3caa22ca55
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 q4 { public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static long mod = 1000000007; public static long[] dp; public static void fill(){ dp = new long[200001]; dp[0] = 1; dp[1] = 2; for(int i = 2;i < 200001;i++) dp[i] = (dp[i - 1] + dp[i - 2] + 1) % mod; } public static long func(long val,int p){ long count = 0,pow = 1; while(pow * 2 <= val){ pow *= 2; count++; } return p >= count + 1 ? dp[(int)(p - count - 1)] : 0; } public static void solve() throws Exception { fill(); String[] parts = br.readLine().split(" "); int n = Integer.parseInt(parts[0]); int p = Integer.parseInt(parts[1]); int[] arr = new int[n]; parts = br.readLine().split(" "); for(int i = 0;i < n;i++){ arr[i] = Integer.parseInt(parts[i]); } long ans = 0; sort(arr); HashSet<Integer> set = new HashSet<>(); for(int val : arr){ boolean flag = true; int cval = val; while(cval > 0){ if(cval % 2 == 1) cval = (cval - 1) / 2; else if(cval % 4 == 0) cval /= 4; else break; if(set.contains(cval)) { flag = false; break; } } set.add(val); if(flag) ans = (ans + func(val,p)) % mod; } System.out.println(ans); } public static void main(String[] args) throws Exception { // int tests = Integer.parseInt(br.readLine()); // for (int test = 1; test <= tests; test++) { solve(); // } } // public static ArrayList<Integer> primes; // public static void seive(int n){ // primes = new ArrayList<>(); // boolean[] arr = new boolean[n + 1]; // Arrays.fill(arr,true); // // for(int i = 2;i * i <= n;i++){ // if(arr[i]) { // for (int j = i * i; j <= n; j += i) { // arr[j] = false; // } // } // } // for(int i = 2;i <= n;i++) if(arr[i]) primes.add(i); // } public static void sort(int[] arr){ ArrayList<Integer> temp = new ArrayList<>(); for(int val : arr) temp.add(val); Collections.sort(temp); for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i); } // public static void sort(long[] arr){ // ArrayList<Long> temp = new ArrayList<>(); // for(long val : arr) temp.add(val); // // Collections.sort(temp); // // for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i); // } // // public static long power(long a,long b,long mod){ // if(b == 0) return 1; // // long p = power(a,b / 2,mod); // p = (p * p) % mod; // // if(b % 2 == 1) return (p * a) % mod; // return p; // } // public static long modDivide(long a,long b,long mod){ // return ((a % mod) * (power(b,mod - 2,mod) % mod)) % mod; // } // // public static int GCD(int a,int b){ // return b == 0 ? a : GCD(b,a % b); // } // public static long GCD(long a,long b){ // return b == 0 ? a : GCD(b,a % b); // } // // public static int LCM(int a,int b){ // return a * b / GCD(a,b); // } // public static long LCM(long a,long b){ // return a * b / GCD(a,b); // } }
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
2fdde40a7a8f515a75a97174a863d8a7
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 q4 { public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static long mod = 1000000007; public static long[] twoPower,normal; public static void fill(){ twoPower = new long[200001]; normal = new long[200001]; twoPower[0] = 1; twoPower[1] = 2; for(int i = 2;i < 200001;i++){ twoPower[i] = twoPower[i - 1] + twoPower[i - 2] + 1; twoPower[i] %= mod; } normal[0] = 1; normal[1] = 2; for(int i = 2;i < 200001;i++){ normal[i] = normal[i - 1] + normal[i - 2] + 1; normal[i] %= mod; } } public static long func(long val,int p){ long count = 0,pow = 1; while(pow * 2 <= val){ pow *= 2; count++; } if(pow == val){ return p >= count + 1 ? twoPower[(int)(p - count - 1)] : 0; }else{ return p >= count + 1 ? normal[(int)(p - count - 1)] : 0; } } public static void solve() throws Exception { fill(); String[] parts = br.readLine().split(" "); int n = Integer.parseInt(parts[0]); int p = Integer.parseInt(parts[1]); int[] arr = new int[n]; parts = br.readLine().split(" "); for(int i = 0;i < n;i++){ arr[i] = Integer.parseInt(parts[i]); } long ans = 0; sort(arr); HashSet<Integer> set = new HashSet<>(); for(int val : arr){ boolean flag = true; int cval = val; while(cval > 0){ if(cval % 2 == 1) cval = (cval - 1) / 2; else if(cval % 4 == 0) cval /= 4; else break; if(set.contains(cval)) { flag = false; break; } } set.add(val); if(flag){ ans = (ans + func(val,p)) % mod; } // System.out.println(ans); } System.out.println(ans); } public static void main(String[] args) throws Exception { // int tests = Integer.parseInt(br.readLine()); // for (int test = 1; test <= tests; test++) { solve(); // } } // public static ArrayList<Integer> primes; // public static void seive(int n){ // primes = new ArrayList<>(); // boolean[] arr = new boolean[n + 1]; // Arrays.fill(arr,true); // // for(int i = 2;i * i <= n;i++){ // if(arr[i]) { // for (int j = i * i; j <= n; j += i) { // arr[j] = false; // } // } // } // for(int i = 2;i <= n;i++) if(arr[i]) primes.add(i); // } public static void sort(int[] arr){ ArrayList<Integer> temp = new ArrayList<>(); for(int val : arr) temp.add(val); Collections.sort(temp); for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i); } // public static void sort(long[] arr){ // ArrayList<Long> temp = new ArrayList<>(); // for(long val : arr) temp.add(val); // // Collections.sort(temp); // // for(int i = 0;i < arr.length;i++) arr[i] = temp.get(i); // } // // public static long power(long a,long b,long mod){ // if(b == 0) return 1; // // long p = power(a,b / 2,mod); // p = (p * p) % mod; // // if(b % 2 == 1) return (p * a) % mod; // return p; // } // public static long modDivide(long a,long b,long mod){ // return ((a % mod) * (power(b,mod - 2,mod) % mod)) % mod; // } // // public static int GCD(int a,int b){ // return b == 0 ? a : GCD(b,a % b); // } // public static long GCD(long a,long b){ // return b == 0 ? a : GCD(b,a % b); // } // // public static int LCM(int a,int b){ // return a * b / GCD(a,b); // } // public static long LCM(long a,long b){ // return a * b / GCD(a,b); // } }
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
42795cc68d902fdd2c0010895eec74c0
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 D1 { static IOHandler sc = new IOHandler(); public static void main(String[] args) { // TODO Auto-generated method stub solve(0); } private static void solve(int t) { int n = sc.nextInt(); int p = sc.nextInt(); int [] arr = sc.readArray(n); int [] score = new int [n]; Arrays.fill(score, -1); Set<Integer> set = new HashSet<>(); for (int num : arr) set.add(num); int val, sc; for (int i = 0; i < arr.length; ++i) { val = arr[i]; sc = -1; while (val > 0) { val /= 2; ++sc; } val = arr[i]; score[i] = sc; do { if (val % 4 == 0) val /= 4; else if (val % 2 == 1){ val -= 1; val /= 2; }else { break; } if (set.contains(val)) { score[i] = -1; break; } }while (val > 0); } Map<Integer, Integer> map = new HashMap<>(); Set<Integer> st = new HashSet<>(); for (int i = 0; i < arr.length; ++i) { if (!st.add(arr[i])) continue; else if (score[i] == -1) continue; map.put(score[i], map.getOrDefault(score[i], 0) + 1); } //System.out.println(map); int result = 0; int mod = 1_000_000_007; int [] dp = new int [p]; for (int i = 0; i < p; ++i) { dp[i] += map.getOrDefault(i, 0); if (i - 1 >= 0) { dp[i] += dp[i - 1]; } dp[i] %= mod; if (i - 2 >= 0) { dp[i] += dp[i - 2]; } dp[i] %= mod; } for (int num : dp) { result += num; result %= mod; } //System.out.println(Arrays.toString(score)); //System.out.println(Arrays.toString(dp)); System.out.println(result); } private static class IOHandler { BufferedReader br; StringTokenizer st; public IOHandler() { 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()); } int [] readArray(int n) { int [] res = new int [n]; for (int i = 0; i < n; ++i) res[i] = nextInt(); return res; } 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
13faca4b65158acb96a22fd5dd371955
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.*; public class contestE { static final int mod = 1000000007; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = 1;//scanner.nextInt(); while (t-->0){ int n = scanner.nextInt(); int p = scanner.nextInt(); int[] f = new int[p]; for(int i=p-1;i>=0;--i){ if(i==p-1) f[i] = 1; else if(i==p-2) f[i] = 2; else f[i] = (f[i+1]+f[i+2]+1)%mod; } int[] a = new int[n]; for(int i=0;i<n;++i) a[i] = scanner.nextInt(); Arrays.sort(a); Set<Integer> set = new HashSet<>(); int ans = 0; for(int i=0;i<n;++i){ if(level(a[i])>=p) continue; boolean flag = true; int m = a[i]; while (m>0){ if(set.contains(m)){ flag = false; break; } if(m%2==1) m = m/2; else if( m%4==2) break; else m = m/4; } if(flag){ ans = (ans + f[level(a[i])])%mod; } set.add(a[i]); } System.out.println(ans); } } static int level(int x){ int ret = 0; int y = 2; while (y<=x){ ret++; y*=2; } return ret; } }
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
d1e4e182e97a99828876ae8315ab3b8f
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.util.Scanner; import java.util.Set; public class TaskD { public static void main(String[] args) { Scanner in = new Scanner(System.in); new TaskD().solve(in); } private void solve(Scanner in) { int n = in.nextInt(); int p = in.nextInt(); int[] list = new int[n]; for (int i = 0; i < n; i++) { list[i] = in.nextInt(); } Set<Integer> set = getRelevant(list); int[] relevantList = new int[set.size()]; int pos = 0; for (int i = 0; i < n; i++) { if (set.contains(list[i])) { relevantList[pos++] = list[i]; set.remove(list[i]); } } System.out.println(finalAns(getCountI(relevantList, p), p)); } private Set<Integer> getRelevant(int[] list) { Arrays.sort(list); Set<Integer> set = new HashSet<>(); for (int i = 0; i < list.length; i++) { int x = list[i]; boolean flag = true; while (x > 0) { if (set.contains(x)) { flag = false; break; } if ((x & 1) > 0) { x >>= 1; } else if ((x & 3) > 0) { break; } else { x >>= 2; } } if (flag == true) set.add(list[i]); } return set; } private int[] getCountI(int[] list, final int p) { int[] countI = new int[p]; for (int i = 0; i < list.length; i++) { int log = log(list[i]); if (log >= p) continue; countI[log] += 1; } return countI; } private int log(int i) { int res = 0; while (i > 1) { res += 1; i /= 2; } return res; } private int finalAns(int[] countI, final int p) { final int MOD = 1000000007; int[] dp = new int[p]; int ans = 0; for (int i = 0; i < p; i++) { dp[i] = countI[i] % MOD; if (i - 1 >= 0) { dp[i] = ((dp[i] % MOD) + (dp[i - 1] % MOD)) % MOD; } if (i - 2 >= 0) { dp[i] = ((dp[i] % MOD) + (dp[i - 2] % MOD)) % MOD; } ans = ((ans % MOD) + (dp[i] % MOD)) % MOD; } return 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
efdfebf2edbbce79fa787de64c4cee89
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 { Solution() { super(System.out); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { Solution o = new Solution(); 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
5a64b148d9830d0198489e41cad01830
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 codeforces { static class in { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer tokenizer = new StringTokenizer(""); static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer.nextToken(); } static int nextInt() throws IOException { //System.out.println(" I WAS CALLED"); return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static class AVLTreePBDS { private Node root; private boolean multi; AVLTreePBDS(boolean multi) { this.root = null; this.multi = multi; } int size() { return size(root); } boolean isEmpty() { return size(root) == 0; } boolean contains(int key) { return contains(root, key); } void add(int key) { root = add(root, key); } void remove(int key) { root = remove(root, key); } Integer first() { Node min = findMin(root); return min != null ? min.key : null; } Integer last() { Node max = findMax(root); return max != null ? max.key : null; } Integer poolFirst() { Node min = findMin(root); if (min != null) { remove(min.key); return min.key; } return null; } Integer poolLast() { Node max = findMax(root); if (max != null) { remove(max.key); return max.key; } return null; } // min >= key Integer ceiling(int key) { return contains(key) ? key : higher(key); } // max <= key Integer floor(int key) { return contains(key) ? key : lower(key); } // min > key Integer higher(int key) { Node min = higher(root, key); return min == null ? null : min.key; } private Node higher(Node cur, int key) { if (cur == null) return null; if (cur.key <= key) return higher(cur.right, key); // cur.key > key Node left = higher(cur.left, key); return left == null ? cur : left; } // max < key Integer lower(int key) { Node max = lower(root, key); return max == null ? null : max.key; } private Node lower(Node cur, int key) { if (cur == null) return null; if (cur.key >= key) return lower(cur.left, key); // cur.key < key Node right = lower(cur.right, key); return right == null ? cur : right; } private class Node { int key, height, size; Node left, right; Node(int key) { this.key = key; height = size = 1; left = right = null; } private void preOrder(Node root , StringBuilder ans) { if(root == null) return; if(root.left != null) preOrder(root.left,ans ); ans.append(root.key+","); if(root.right!=null) preOrder(root.right, ans); } public String toString() { StringBuilder res = new StringBuilder(); preOrder(root, res); return "[" + String.valueOf(res.substring(0 , res.length()-1)) +"]" ; } } private int height(Node cur) { return cur == null ? 0 : cur.height; } private int balanceFactor(Node cur) { return height(cur.right) - height(cur.left); } private int size(Node cur) { return cur == null ? 0 : cur.size; } // fixVertex private void fixHeightAndSize(Node cur) { cur.height = Math.max(height(cur.left), height(cur.right)) + 1; cur.size = size(cur.left) + size(cur.right) + 1; } private Node rotateRight(Node cur) { Node prevLeft = cur.left; cur.left = prevLeft.right; prevLeft.right = cur; fixHeightAndSize(cur); fixHeightAndSize(prevLeft); return prevLeft; } private Node rotateLeft(Node cur) { Node prevRight = cur.right; cur.right = prevRight.left; prevRight.left = cur; fixHeightAndSize(cur); fixHeightAndSize(prevRight); return prevRight; } private Node balance(Node cur) { fixHeightAndSize(cur); if (balanceFactor(cur) == 2) { if (balanceFactor(cur.right) < 0) cur.right = rotateRight(cur.right); return rotateLeft(cur); } if (balanceFactor(cur) == -2) { if (balanceFactor(cur.left) > 0) cur.left = rotateLeft(cur.left); return rotateRight(cur); } return cur; } private boolean contains(Node cur, int key) { if (cur == null) return false; else if (key < cur.key) return contains(cur.left, key); else if (key > cur.key) return contains(cur.right, key); else return true; } private Node add(Node cur, int key) { if (cur == null) return new Node(key); if (key < cur.key) cur.left = add(cur.left, key); else if (key > cur.key || multi) cur.right = add(cur.right, key); return balance(cur); } private Node findMin(Node cur) { return cur.left != null ? findMin(cur.left) : cur; } private Node findMax(Node cur) { return cur.right != null ? findMax(cur.right) : cur; } private Node removeMin(Node cur) { if (cur.left == null) return cur.right; cur.left = removeMin(cur.left); return balance(cur); } private Node removeMax(Node cur) { if (cur.right == null) return cur.left; cur.right = removeMax(cur.right); return balance(cur); } private Node remove(Node cur, int key) { if (cur == null) return null; if (key < cur.key) cur.left = remove(cur.left, key); else if (key > cur.key) cur.right = remove(cur.right, key); else { // k == cur.key Node prevLeft = cur.left; Node prevRight = cur.right; if (prevRight == null) return prevLeft; Node min = findMin(prevRight); min.right = removeMin(prevRight); min.left = prevLeft; return balance(min); } return balance(cur); } int orderOfKey(int key) { return orderOfKey(root, key); } // count < key private int orderOfKey(Node cur, int key) { if (cur == null) return 0; if (cur.key < key) return size(cur.left) + 1 + orderOfKey(cur.right, key); if(cur.key > key || (multi && cur.left!=null && cur.left.key == key)) return orderOfKey(cur.left, key); // cur.key == key return size(cur.left); } Integer findByOrder(int pos) { return size(root) > pos ? findByOrder(root, pos) : null; } // get i-th private int findByOrder(Node cur, int pos) { if (size(cur.left) > pos) return findByOrder(cur.left, pos); if (size(cur.left) == pos) return cur.key; // size(cur.left) < pos return findByOrder(cur.right, pos - 1 - size(cur.left)); } public String toString() { return String.valueOf(this.root) ; } } static boolean check(long num,Set<Long> st){ long temp = num; if(st.contains(temp))return false; while (temp>1){ if(temp%2==1){ temp = (temp-1)/2; } else{ if(temp%4==0)temp=temp/4; else break; } if(st.contains(temp))return false; } return true; } public static void main(String[] args) throws IOException { int n = in.nextInt(); int p = in.nextInt(); long[] arr = new long[n]; for(int i=0;i<n;i++)arr[i]=in.nextLong(); int u= 200000+5; long[] fib=new long[u]; long mod = 1000000000L+7L; fib[0]=fib[1]=1L; for(int i=2;i<u;i++)fib[i]=(fib[i-1]+fib[i-2])%mod; long[] prefix = new long[u]; prefix[0]=fib[0]; for(int i=1;i<u;i++)prefix[i]=(prefix[i-1]+fib[i])%mod; long ans=0; Set<Long> st = new HashSet<>(); Arrays.sort(arr); for(long i:arr){ if(check(i,st)){ st.add(i); String temp = Long.toBinaryString(i); if(temp.length()<=p)ans= (ans+ prefix[p-(temp.length())])%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
4e95f0b0a26e1370ffcdd752263e7edc
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.lang.*; import java.math.BigInteger; import java.io.*; public class Main implements Runnable { public static void main(String[] args) { new Thread(null, new Main(), "whatever", 1 << 26).start(); } private FastScanner sc; private PrintWriter pw; public void run() { try { boolean isSumitting = true; // isSumitting = false; if (isSumitting) { pw = new PrintWriter(System.out); sc = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); } else { pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt"))); sc = new FastScanner(new BufferedReader(new FileReader("input.txt"))); } } catch (Exception e) { throw new RuntimeException(); } // int t = sc.nextInt(); int t = 1; while (t-- > 0) { // sc.nextLine(); // System.out.println("for t=" + t); solve(); } pw.close(); } public long mod = 1_000_000_007; private class Pair { int first, second; Pair(int first, int second) { this.first = first; this.second = second; } } private HashSet<Integer> set; private int[] dp; public void solve() { int n = sc.nextInt(); int p = sc.nextInt(); set = new HashSet<Integer>(); dp = new int[p + 1]; for (int i = 0; i < n; i++) { set.add(sc.nextInt()); } ArrayList<Integer> arr = new ArrayList<Integer>(); for (int i : set) arr.add(i); for (int i : arr) { int temp = i; while (temp != 0) { if (temp % 2 == 0) { temp = temp >> 1; if (temp % 2 == 0) { temp = temp >> 1; } else break; } else { temp = temp >> 1; } if (set.contains(temp)) { set.remove(i); } } } for (int i : set) { // pw.print(i + " "); int temp = (int)(Math.floor((Math.log(i) / Math.log(2)))); if (temp < p) dp[temp]++; } // pw.println(); dp[1] += dp[0]; for (int i = 2; i < p; i++) { dp[i] += (dp[i - 1] + dp[i - 2]); dp[i] %= mod; } for (int i = 1; i < p; i++) { dp[i] += dp[i - 1]; dp[i] %= mod; } pw.println(dp[p - 1]); } // private void dfs(int i) { // if (i > 1_000_000_000) { // return; // } // if (set.contains(i)) { // set.remove(i); // } // dfs(2 * i + 1); // dfs(4 * i); // } class FastScanner { private BufferedReader reader = null; private StringTokenizer tokenizer = null; public FastScanner(BufferedReader bf) { reader = bf; 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 float nextFloat() { return Float.parseFloat(next()); } public int[] nextIntArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String[] nextStringArray(int n) { String[] a = new String[n]; for (int i = 0; i < n; i++) { a[i] = next(); } return a; } public long[] nextLongArray(int n) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = nextLong(); } return a; } } private static class Sorter { public static <T extends Comparable<? super T>> void sort(T[] arr) { Arrays.sort(arr); } public static <T> void sort(T[] arr, Comparator<T> c) { Arrays.sort(arr, c); } public static <T> void sort(T[][] arr, Comparator<T[]> c) { Arrays.sort(arr, c); } public static <T extends Comparable<? super T>> void sort(ArrayList<T> arr) { Collections.sort(arr); } public static <T> void sort(ArrayList<T> arr, Comparator<T> c) { Collections.sort(arr, c); } public static void normalSort(int[] arr) { Arrays.sort(arr); } public static void normalSort(long[] arr) { Arrays.sort(arr); } public static void sort(int[] arr) { timSort(arr); } public static void sort(int[] arr, Comparator<Integer> c) { timSort(arr, c); } public static void sort(int[][] arr, Comparator<Integer[]> c) { timSort(arr, c); } public static void sort(long[] arr) { timSort(arr); } public static void sort(long[] arr, Comparator<Long> c) { timSort(arr, c); } public static void sort(long[][] arr, Comparator<Long[]> c) { timSort(arr, c); } private static void timSort(int[] arr) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[] arr, Comparator<Integer> c) { Integer[] temp = new Integer[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(int[][] arr, Comparator<Integer[]> c) { Integer[][] temp = new Integer[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } private static void timSort(long[] arr) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[] arr, Comparator<Long> c) { Long[] temp = new Long[arr.length]; for (int i = 0; i < arr.length; i++) temp[i] = arr[i]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) arr[i] = temp[i]; } private static void timSort(long[][] arr, Comparator<Long[]> c) { Long[][] temp = new Long[arr.length][arr[0].length]; for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; Arrays.sort(temp, c); for (int i = 0; i < arr.length; i++) for (int j = 0; j < arr[0].length; j++) temp[i][j] = arr[i][j]; } } public long fastPow(long x, long y, long mod) { if (y == 0) return 1; if (y == 1) return x % mod; long temp = fastPow(x, y / 2, mod); long ans = (temp * temp) % mod; return (y % 2 == 1) ? (ans * (x % mod)) % mod : ans; } public long fastPow(long x, long y) { if (y == 0) return 1; if (y == 1) return x; long temp = fastPow(x, y / 2); long ans = (temp * temp); return (y % 2 == 1) ? (ans * x) : 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
e38bcb8a318ff1a27d926fe64b6534d4
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.util.function.Function; import java.util.stream.IntStream; /* polyakoff */ public class Main { static FastReader in; static PrintWriter out; static Random rand = new Random(); static final int oo = (int) 2e9 + 10; static final long OO = (long) 2e18 + 10; static final int MOD = (int) 1e9 + 7; static void shuffle(int[] arr) { for (int i = 0; i < arr.length; i++) { int j = i + rand.nextInt(arr.length - i); int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } static void solve() { int n = in.nextInt(); int p = in.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } shuffle(a); Arrays.sort(a); HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { int x = a[i]; boolean hasLess = false; while (x != 0) { if ((x & 1) == 1) { x >>= 1; if (set.contains(x)) { hasLess = true; break; } } else if ((x & 2) == 0) { x >>= 2; if (set.contains(x)) { hasLess = true; break; } } else break; } if (!hasLess) set.add(a[i]); } long[] dp = new long[p]; dp[0] = 1; if (p > 1) dp[1] = 2; for (int i = 2; i < p; i++) { dp[i] = (1 + dp[i - 2] + dp[i - 1]) % MOD; } long ans = 0; for (int x : set) { int len = Integer.bitCount(Integer.highestOneBit(x) - 1) + 1; if (len <= p) ans = (ans + dp[p - len]) % MOD; } out.println(ans); } public static void main(String[] args) { in = new FastReader(); out = new PrintWriter(System.out); int t = 1; // t = in.nextInt(); while (t-- > 0) { solve(); } out.flush(); out.close(); } static class FastReader { BufferedReader br; StringTokenizer st; FastReader() { this(System.in); } FastReader(String file) throws FileNotFoundException { this(new FileInputStream(file)); } FastReader(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String next() { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(nextLine()); } return st.nextToken(); } String nextLine() { String line; try { line = br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } }
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
dd6eea80d4e8380065043ee9bcdcea39
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.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.util.TreeSet; import java.util.StringTokenizer; import java.io.Writer; import java.io.OutputStreamWriter; import java.io.BufferedReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DInfiniteSet solver = new DInfiniteSet(); solver.solve(1, in, out); out.close(); } static class DInfiniteSet { int mod = 1000000007; TreeSet<Integer> set; public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(), p = in.nextInt(); int[] arr = in.nextIntArray(n); long[] dp = new long[p + 1]; set = new TreeSet<>(); for (int a : arr) { set.add(a); } for (int i = 1; i <= p; i++) { dp[i] = add(dp[i - 1], dp[i]); if (i - 2 >= 0) dp[i] = add(dp[i - 2], dp[i]); if (i <= 31) { for (int a : arr) { int x = a; if (x >= (1 << (i - 1)) && x < (1 << i)) { boolean flag = false; if (x % 4 == 0) { flag |= valid(x / 4); } if (x % 2 == 1) { flag |= valid((x - 1) / 2); } if (!flag) dp[i] = add(dp[i], 1); } } } } long ans = 0; // out.println(dp); for (int i = 0; i <= p; i++) { ans = add(ans, dp[i]); } out.println(ans); } boolean valid(int x) { if (set.contains(x)) return true; if (x == 0) return false; if (x % 4 != 0 && x % 2 != 1) return false; if (x % 4 == 0) return valid(x / 4); return valid(x / 2); } long add(long a, long b) { return (a + b) % mod; } } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int[] nextIntArray(int n) { int[] array = new int[n]; for (int i = 0; i < n; ++i) array[i] = nextInt(); return array; } public int nextInt() { return Integer.parseInt(next()); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(long i) { writer.println(i); } } }
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
1f18569c38c4cf50ad1041b68dbc115d
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 static java.lang.Math.max; import static java.lang.Math.min; import static java.lang.Math.abs; import java.io.*; import java.util.*; public class Solution { static int mod=(int)1e9+7; static HashMap<Integer,Boolean> mp=new HashMap<>(),present=new HashMap<>(); public static void main(String[] args) { var io = new Copied(System.in, System.out); // int k=1; int t = 1; // t = io.nextInt(); for (int i = 0; i < t; i++) { // io.print("Case #" + k + ": "); solve(io); // k++; } io.close(); } static boolean poss(int n){ if(mp.containsKey(n)) return mp.get(n); if(present.containsKey(n)) return present.get(n); if(n==0) return false; if(n%2==0 && n%4!=0) return false; boolean ret=false; if(n%2==1){ ret|=poss((n-1)/2); } else if(n%4==0){ ret|=poss(n/4); } mp.putIfAbsent(n, ret); return ret; } public static void solve(Copied io) { int n=io.nextInt(),p=io.nextInt(); int a[]=io.readArray(n); for(int i:a) present.putIfAbsent(i,true); sort(a); int dp[]=new int[p+1]; dp[0]=0; for(int i=1;i<=p;i++){ if(i<=30){ int mx=1<<i,mn=1<<(i-1); for(int j=0;j<n;j++){ if(a[j]<mx && a[j]>=mn){ boolean add=true; if(a[j]%2==1) add=!poss((a[j]-1)/2); else if(a[j]%4==0) add=!poss(a[j]/4); if(add) dp[i]++; } } } if(i>=1){ dp[i]=mod_add(dp[i-1], dp[i]); } if(i>=2){ dp[i]=mod_add(dp[i-2], dp[i]); } } int ans=0; for(int i=1;i<=p;i++){ ans=mod_add(ans, dp[i]); } io.println(ans); } static int power(int x, int y, int p) { int res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if ((y & 1) != 0) res = (res * x) % p; y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } static void binaryOutput(boolean ans, Copied io){ String a=new String("YES"), b=new String("NO"); if(ans){ io.println(a); } else{ io.println(b); } } 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 printArr(int[] arr) { for (int x : arr) System.out.print(x + " "); System.out.println(); } static int[] listToInt(ArrayList<Integer> a){ int n=a.size(); int arr[]=new int[n]; for(int i=0;i<n;i++){ arr[i]=a.get(i); } return arr; } static int mod_mul(int a, int b){ a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;} static int mod_add(int a, int b){ a = a % mod; b = b % mod; return (((a + b) % mod) + mod) % mod;} static int mod_sub(int a, int b){ a = a % mod; b = b % mod; return (((a - b) % mod) + mod) % mod;} } class Pair implements Cloneable, Comparable<Pair> { int x,y; Pair(int a,int b) { this.x=a; this.y=b; } @Override public boolean equals(Object obj) { if(obj instanceof Pair) { Pair p=(Pair)obj; return p.x==this.x && p.y==this.y; } return false; } @Override public int hashCode() { return Math.abs(x)+101*Math.abs(y); } @Override public String toString() { return "("+x+" "+y+")"; } @Override protected Pair clone() throws CloneNotSupportedException { return new Pair(this.x,this.y); } @Override public int compareTo(Pair a) { int t= this.x-a.x; if(t!=0) return t; else return this.y-a.y; } } // class DescendingOrder<T> implements Comparator<T>{ // @Override // public int compare(Object o1,Object o2){ // // -1 if want to swap and (1,0) otherwise. // int addingNumber=(Integer) o1,existingNumber=(Integer) o2; // if(addingNumber>existingNumber) return -1; // else if(addingNumber<existingNumber) return 1; // else return 0; // } // } class Range{ int l,r,i; Range(int l,int r,int i){ this.l=l; this.r=r; this.i=i; } String show(Copied io){ return new String((l+1)+" "+(r+1)+" "+(i+1)+"\n"); } } class Copied extends PrintWriter { public Copied(InputStream i) { super(new BufferedOutputStream(System.out)); r = new BufferedReader(new InputStreamReader(i)); } public Copied(InputStream i, OutputStream o) { super(new BufferedOutputStream(o)); r = new BufferedReader(new InputStreamReader(i)); } public boolean hasMoreTokens() { return peekToken() != null; } public int nextInt() { return Integer.parseInt(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public String next() { return nextToken(); } public double[] nextDoubles(int N) { double[] res = new double[N]; for (int i = 0; i < N; i++) { res[i] = nextDouble(); } return res; } int[] readArray(int n) { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public long[] nextLongs(int N) { long[] res = new long[N]; for (int i = 0; i < N; i++) { res[i] = nextLong(); } return res; } private BufferedReader r; private String line; private StringTokenizer st; private String token; private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = r.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return 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
dc4552f0827ab61f5a7c6e74e0a476f6
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.lang.*; import java.io.*; public class Main { void solve() { int n = in.nextInt(); int p = in.nextInt(); int[] a = array(n, 1); Arrays.sort(a); HashSet<Integer> set = new HashSet<>(); long[] dp = new long[p]; for (int i = 0; i < n; i++) { boolean inSet = false; int val = a[i]; while (a[i] > 0) { if (a[i] % 4 == 0) { a[i] = a[i] / 4; if (set.contains(a[i])) { inSet = true; break; } } else if (a[i] % 2 == 1) { a[i] = (a[i] - 1) / 2; if (set.contains(a[i])) { inSet = true; break; } } else { break; } } if (!inSet) { set.add(val); int powerGreater = (int)(Math.log(val) / Math.log(2)); if (powerGreater >= p)continue; println(powerGreater + " " + val); dp[powerGreater] += 1; } } if (p == 1) { out.append(dp[0] + endl); return; } dp[1] += dp[0]; for (int i = 2; i < p ; i++) { dp[i] = (dp[i] + dp[i - 1] + dp[i - 2]) % mod; } long sum = 0; for (int i = 0; i < p; i++) { sum = (sum + dp[i]) % mod; } out.append(sum + endl); } public static void main (String[] args) { // It happens - Syed Mizbahuddin Main sol = new Main(); int t = 1; // t = in.nextInt(); while (t-- != 0) { sol.solve(); } System.out.print(out); } <T> void println(T[] s) { if (err == null)return; err.println(Arrays.toString(s)); } <T> void println(T s) { if (err == null)return; err.println(s); } void println(int s) { if (err == null)return; err.println(s); } void println(int[] a) { if (err == null)return; println(Arrays.toString(a)); } void println(long[] a) { if (err == null)return; println(Arrays.toString(a)); } int[] array(int n, int x) { int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); } return a; } long[] array(int n, long x) { long[] a = new long[n]; for (int i = 0; i < n; i++) { a[i] = in.nextLong(); } return a; } int[] array1(int n) { int[] a = new int[n + 1]; for (int i = 1; i <= n; i++) { a[i] = in.nextInt(); } return a; } int max(int[] a) { int max = a[0]; for (int i = 0; i < a.length; i++)max = Math.max(max, a[i]); return max; } int min(int[] a) { int min = a[0]; for (int i = 0; i < a.length; i++)min = Math.min(min, a[i]); return min; } int count(int[] a, int x) { int count = 0; for (int i = 0; i < a.length; i++)if (x == a[i])count++; return count; } void printArray(int[] a) { for (int ele : a)out.append(ele + " "); out.append("\n"); } void printArray(long[] a) { for (long ele : a)out.append(ele + " "); out.append("\n"); } static { try { System.setIn(new FileInputStream("input.txt")); System.setOut(new PrintStream(new FileOutputStream("output.txt"))); err = new PrintStream(new FileOutputStream("error.txt")); } catch (Exception e) {} } static FastReader in; static StringBuilder out; static PrintStream err; static String yes , no , endl; final int MAX; final int MIN; int mod ; Main() { in = new FastReader(); out = new StringBuilder(); MAX = Integer.MAX_VALUE; MIN = Integer.MIN_VALUE; mod = (int)1e9 + 7; yes = "YES\n"; no = "NO\n"; endl = "\n"; } 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; } } class Pair implements Comparable<Pair> { int first , second; Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair b) { return this.first - b.first; } public String toString() { String s = "{ " + Integer.toString(first) + " , " + Integer.toString(second) + " }"; return s; } } }
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
ee01e5db1991efb214d7fc5f22fd10e7
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 com.company; import java.io.*; import java.util.*; public class Main{ static boolean[] primecheck = new boolean[1000002]; static ArrayList<Integer>[] adj; static int[] vis; static int mod = (int)1e9 + 7; public static void main(String[] args) { OutputStream outputStream = System.out; FastReader in = new FastReader(); PrintWriter out = new PrintWriter(outputStream); PROBLEM solver = new PROBLEM(); int t = 1; //t = in.ni(); for (int i = 0; i < t; i++) { //out.print("Case #" + (i+1) + ": "); solver.solve(in, out); } out.close(); } static class PROBLEM { public void solve(FastReader in, PrintWriter out) { //Where there is will there is way!! int n = in.ni(), p = in.ni(); int[] a = in.ra(n), dp = new int[p+1]; TreeSet<Integer> x = new TreeSet<>(), y = new TreeSet<>(); for (int i = 0; i < n; i++) { x.add(a[i]); } // pa(a, out); for(int i: a){ int b = i; boolean isThere = false; while(b > 0){ if(b%4 == 0) b>>=2; else if((b&1) == 1) b>>=1; else break; if(x.contains(b)){ isThere = true; break; } } if(!isThere) y.add(i); } int ans = 0; //out.println(y); for(int i : y){ int cnt = 0; while(i > 0){ i>>=1; cnt++; } if(cnt <= p) dp[cnt]++; } for (int i = 1; i <= p; i++) { if(i > 0) dp[i] += (dp[i-1]%mod); if(i > 1) dp[i] += (dp[i-2]%mod); dp[i]%=mod; ans = (ans + dp[i])%mod; } //pa(dp, out); out.println(ans); } } static void pa(int[] a, PrintWriter out){ for (int j : a) { out.print(j + " "); } out.println(); } public static void sortbyColumn(int arr[][], int col) { // Using built-in sort function Arrays.sort Arrays.sort(arr, new Comparator<int[]>() { @Override // Compare values according to columns public int compare(final int[] entry1, final int[] entry2) { // To sort in descending order revert // the '>' Operator if (entry1[col] > entry2[col]) return 1; else return -1; } }); // End of function call sort(). } static boolean isPoT(int n){ double p = Math.log((double)n)/Math.log(2D); return (p == (int)p); } static void dfs(int i){ vis[i] = 1; for(int j: adj[i]){ if(vis[j] == 0) { dfs(j); } } } static long sigmaK(long k){ return (k*(k+1))/2; } static void swap(int[] a, int l, int r) { int temp = a[l]; a[l] = a[r]; a[r] = temp; } static int binarySearch(int[] a, int l, int r, int x){ if(r>=l){ int mid = l + (r-l)/2; if(a[mid] == x) return mid; if(a[mid] > x) return binarySearch(a, l, mid-1, x); else return binarySearch(a,mid+1, r, x); } return -1; } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } static long lcm(long a, long b){ return (a / gcd(a, b)) * b; } static boolean isSquare(double a) { boolean isSq = false; double b = Math.sqrt(a); double c = Math.sqrt(a) - Math.floor(b); if (c == 0) isSq = true; return isSq; } static long fast_pow(long a, long b) { //Jeel bhai OP if(b == 0) return 1L; long val = fast_pow(a, b / 2); if(b % 2 == 0) return val * val % mod; else return val * val % mod * a % mod; } static int exponentMod(int A, int B, int C) { // Base cases if (A == 0) return 0; if (B == 0) return 1; // If B is even long y; if (B % 2 == 0) { y = exponentMod(A, B / 2, C); y = (y * y) % C; } // If B is odd else { y = A % C; y = (y * exponentMod(A, B - 1, C) % C) % C; } return (int) ((y + C) % C); } // static class Pair implements Comparable<Pair>{ // // int x; // int y; // // Pair(int x, int y){ // this.x = x; // this.y = y; // } // // public int compareTo(Pair o){ // // int ans = Integer.compare(x, o.x); // if(o.x == x) ans = Integer.compare(y, o.y); // // return ans; // //// int ans = Integer.compare(y, o.y); //// if(o.y == y) ans = Integer.compare(x, o.x); //// //// return ans; // } // } static class Tuple implements Comparable<Tuple>{ int x, y, id; Tuple(int x, int y, int id){ this.x = x; this.y = y; this.id = id; } public int compareTo(Tuple o){ int ans = Integer.compare(x, o.x); if(o.x == x) ans = Integer.compare(y, o.y); return ans; } } public static class Pair<U extends Comparable<U>, V extends Comparable<V>> implements Comparable<Pair<U, V>> { public U x; public V y; public Pair(U x, V y) { this.x = x; this.y = y; } public int hashCode() { return (x == null ? 0 : x.hashCode() * 31) + (y == null ? 0 : y.hashCode()); } public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<U, V> p = (Pair<U, V>) o; return (x == null ? p.x == null : x.equals(p.x)) && (y == null ? p.y == null : y.equals(p.y)); } public int compareTo(Pair<U, V> b) { int cmpU = x.compareTo(b.x); return cmpU != 0 ? cmpU : y.compareTo(b.y); } public int compareToY(Pair<U, V> b) { int cmpU = y.compareTo(b.y); return cmpU != 0 ? cmpU : x.compareTo(b.x); } public String toString() { return String.format("(%s, %s)", x.toString(), y.toString()); } } 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 ni() { return Integer.parseInt(next()); } long nl() { return Long.parseLong(next()); } double nd() { return Double.parseDouble(next()); } char nc() { return next().charAt(0); } boolean nb() { return !(ni() == 0); } // boolean nextBoolean(){return Boolean.parseBoolean(next());} String nline() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int[] ra(int size) { int[] array = new int[size]; for (int i = 0; i < size; i++) array[i] = ni(); return array; } } private static int[] mergeSort(int[] array) { //array.length replaced with ctr int ctr = array.length; if (ctr <= 1) { return array; } int midpoint = ctr / 2; int[] left = new int[midpoint]; int[] right; if (ctr % 2 == 0) { right = new int[midpoint]; } else { right = new int[midpoint + 1]; } for (int i = 0; i < left.length; i++) { left[i] = array[i]; } for (int i = 0; i < right.length; i++) { right[i] = array[i + midpoint]; } left = mergeSort(left); right = mergeSort(right); int[] result = merge(left, right); return result; } private static int[] merge(int[] left, int[] right) { int[] result = new int[left.length + right.length]; int leftPointer = 0, rightPointer = 0, resultPointer = 0; while (leftPointer < left.length || rightPointer < right.length) { if (leftPointer < left.length && rightPointer < right.length) { if (left[leftPointer] < right[rightPointer]) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } else if (leftPointer < left.length) { result[resultPointer++] = left[leftPointer++]; } else { result[resultPointer++] = right[rightPointer++]; } } return result; } public static void Sieve(int n) { Arrays.fill(primecheck, true); primecheck[0] = false; primecheck[1] = false; for (int i = 2; i * i < n + 1; i++) { if (primecheck[i]) { for (int j = i * 2; j < n + 1; j += i) { primecheck[j] = false; } } } } }
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
023a41c53a40f0be092216781e18d011
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.util.HashSet; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.util.Set; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); DInfiniteSet solver = new DInfiniteSet(); solver.solve(1, in, out); out.close(); } static class DInfiniteSet { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int p = in.nextInt(); int mod = (int) (1e9 + 7); int[] a = new int[n]; Set<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { a[i] = in.nextInt(); set.add(a[i]); } int[] dp = new int[p + 100]; for (int i = 0; i < n; i++) { int len = bitLength(a[i]); boolean f = false; while (a[i] > 0) { if (a[i] % 2 == 1) { a[i] = (a[i] - 1) / 2; } else if (a[i] % 4 == 0) { a[i] = a[i] / 4; } else { break; } if (set.contains(a[i])) { f = true; break; } } if (!f) { dp[len]++; } } long sum = dp[1]; for (int i = 2; i <= p; i++) { dp[i] = (dp[i] + (dp[i - 1] + dp[i - 2]) % mod) % mod; sum += dp[i]; sum %= mod; } out.println(sum); } int bitLength(long num) { int c = 0; for (; num > 0; num >>= 1) { ++c; } return c; } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(long i) { writer.println(i); } } }
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
b90d8fca31e707a6d90e292a8047a5a1
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
/* "Everything in the universe is balanced. Every disappointment you face in life will be balanced by something good for you! Keep going, never give up." Just have Patience + 1... */ import java.util.*; import java.lang.*; import java.io.*; public class Solution { static int MOD = 1000000007; public static void main(String[] args) throws java.lang.Exception { out = new PrintWriter(new BufferedOutputStream(System.out)); sc = new FastReader(); int test = 1; for (int t = 1; t <= test; t++) { solve(); } out.close(); } private static void solve() { int n = sc.nextInt(); int p = sc.nextInt(); HashSet<Integer> infiniteSet = new HashSet<>(); for (int i = 0; i < n; i++) { int val = sc.nextInt(); infiniteSet.add(val); } // remove elements from the set which can be formed from some other element present in the given infiniteSet TreeSet<Integer> required = modifySet(infiniteSet); long[] dp = new long[p + 1]; // dp[i] is the count of number of elements in the set whose msb is i. long res = 0; // count of all elements in the set whose msb < p for (int i = 0; i < p; i++) { if (i < 33) { // check if present in the set int upperBound = 1 << (i + 1); while (!required.isEmpty() && required.first() < upperBound) { dp[i]++; required.remove(required.first()); } } if (i > 0) { // in second operation new number will have one extra bit dp[i] += dp[i - 1]; } if (i > 1) { // in third operation new number will have two extra bit dp[i] += dp[i - 2]; } dp[i] %= MOD; res += dp[i]; res %= MOD; } out.println(res); } private static TreeSet<Integer> modifySet(HashSet<Integer> infiniteSet) { TreeSet<Integer> modified = new TreeSet<>(); for (int val : infiniteSet) { int curr = val; boolean needed = true; while ((curr > 1 && curr % 2 == 1) || (curr >= 4 && curr % 4 == 0)) { if (curr % 4 == 0) { curr /= 4; }else { curr = (curr - 1) / 2; } if (infiniteSet.contains(curr)) { needed = false; break; } } if (needed) { modified.add(val); } } return modified; } public static FastReader sc; public static PrintWriter out; static class FastReader { BufferedReader br; StringTokenizer str; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (str == null || !str.hasMoreElements()) { try { str = new StringTokenizer(br.readLine()); } catch (IOException lastMonthOfVacation) { lastMonthOfVacation.printStackTrace(); } } return str.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 lastMonthOfVacation) { lastMonthOfVacation.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
2af4336905a735934483b19d67e582f8
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.Set; import java.lang.Math; import java.util.Queue; import java.util.Arrays; import java.util.Scanner; import java.util.HashMap; import java.util.HashSet; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.PriorityQueue; public final class code { static class sortCond implements Comparator<Pair<Integer, Integer>> { public int compare(Pair<Integer, Integer> obj, Pair<Integer, Integer> obj1) { if (obj.getValue() < obj1.getValue()) { return -1; } else { return 1; } } } static class Pair<f, s> { f a; s b; Pair(f a, s b) { this.a = a; this.b = b; } f getKey() { return this.a; } s getValue() { return this.b; } } interface modOperations { long mod(long a, long b, long mod); } static long findBinaryExponentian(long a, long pow, long mod) { if (pow == 1) { return a; } else if (pow == 0) { return 1; } else { long retVal = findBinaryExponentian(a, (int) pow / 2, mod); return modMul.mod(modMul.mod(retVal, retVal, mod), (pow % 2 == 0) ? 1 : a, mod); } } static int bleft(int ele, int[] sortedArr) { int l = 0; int h = sortedArr.length; while (l <= h) { int mid = l + (int) (h - l) / 2; if (sortedArr[mid] < ele) { l = mid + 1; } else if (sortedArr[mid] > ele) { h = mid - 1; } else { return mid; } } return -1; } static long gcd(long a, long b) { long div = b; long rem = a % b; while (rem != 0) { long temp = rem; rem = div % rem; div = temp; } return div; } static int log(long no) { int i = 0; while ((1 << i) <= no) { i += 1; } return i - 1; } static modOperations modAdd = (long a, long b, long mod) -> { return (a % mod + b % mod) % mod; }; static modOperations modSub = (long a, long b, long mod) -> { return (a % mod - b % mod + mod) % mod; }; static modOperations modMul = (long a, long b, long mod) -> { return (a % mod * b % mod) % mod; }; static modOperations modDiv = (long a, long b, long mod) -> { return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod); }; static ArrayList<Integer> primeList(int MAXI) { int[] prime = new int[MAXI + 1]; ArrayList<Integer> obj = new ArrayList<Integer>(); for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) { if (prime[i] == 0) { obj.add(i); for (int j = i * i; j <= MAXI; j += i) { prime[j] = 1; } } } for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) { if (prime[i] == 0) { obj.add(i); } } return obj; } public static void main(String args[]) { Scanner obj = new Scanner(System.in); long mod = 1000000007; int n = obj.nextInt(); int p = obj.nextInt(); int log = p, j = 0; long arr[] = new long[n]; Set<Long> isT = new HashSet<Long>(); for (j = 0; j < n; j++) { arr[j] = obj.nextLong(); } Arrays.sort(arr); for (j = 0; j < n; j++) { long ele = arr[j]; while (!isT.contains(ele) && ele > 2) { if (ele % 2 != 0) { ele = (long) (ele - 1) / 2; } else { if (ele % 4 == 0) { ele = (long) ele / 4; } else { break; } } } if (!isT.contains(ele)) { isT.add(arr[j]); } } long dp[] = new long[log]; for (long v : isT) { int logi = log(v); if (logi < log) { dp[logi] += 1; } } for (j = 1; j < log; j++) { dp[j] += modAdd.mod(dp[j - 1], (j - 2) >= 0 ? dp[j - 2] : 0, mod); dp[j] = dp[j] % mod; // System.out.println(dp[j]); } long ans = 0; for (j = 0; j < log; j++) { ans += dp[j]; ans = 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
460a06f49e80dcdbaaacb601053068b8
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
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class Eshan { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.0000000"); final static int mod = (int) (1e9 + 7); final static int MAX = Integer.MAX_VALUE; final static int MIN = Integer.MIN_VALUE; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== // int t = readInt(); int t = 1; preprocess(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { int n = readInt(), p = readInt(); int[] arr = readIntArray(n); sort(arr); Set<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { int num = arr[i]; boolean present = false; while (num > 0) { if ((num & 1) == 1) num >>= 1; else if ((num & 3) == 0) num >>= 2; else break; if (set.contains(num)) { present = true; break; } } if (!present) set.add(arr[i]); } long ans = 0L; for (int i : set) { int x = (int) Math.ceil(Math.log(i + 1) / Math.log(2)); if (x <= p) ans = (ans + dp[p + 1 - x]) % mod; } out.println(ans); } static int[] dp; private static void preprocess() { dp = new int[(int) 2e5 + 1]; dp[1] = 1; for (int i = 2; i <= 2e5; i++) dp[i] = (dp[i - 1] + dp[i - 2] + 1) % mod; } // ==================== CUSTOM CLASSES ================================ static class Pair implements Comparable<Pair> { int first, second; Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair o) { if (this.first != o.first) return this.second - o.second; return this.first - o.first; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray(int n) throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(m); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static long power(long a, long b) { if (b == 0) return 1L; long ans = power(a, b >> 1); ans *= ans; if ((b & 1) == 1) ans *= a; return ans; } private static int mod_power(int a, int b) { if (b == 0) return 1; int temp = mod_power(a, b >> 1); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) prime[j] = false; } } // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) { if (primes[j] == j) primes[j] = i; } } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ==================== SEGMENT TREE (RANGE SUM) ===================== public static class SegmentTree { int n; int[] arr, tree, lazy; SegmentTree(int arr[]) { this.arr = arr; this.n = arr.length; this.tree = new int[(n << 2)]; this.lazy = new int[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, int val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, int val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } int query(int l, int r) { return query(1, 0, n - 1, l, r); } int query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } public void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } public boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ==================== FENWICK TREE ================================ static class FT { long[] tree; int n; FT(int[] arr, int n) { this.n = n; this.tree = new long[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return 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
d9dbdb8bfa43be8013b3aa03ceaa88a0
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
// JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA import java.util.*; import java.util.Map.Entry; import java.util.stream.*; import java.lang.*; import java.math.BigInteger; import java.text.DecimalFormat; import java.io.*; public class Eshan { static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer st; static PrintWriter out = new PrintWriter(System.out); static DecimalFormat df = new DecimalFormat("0.0000000"); final static int mod = (int) (1e9 + 7); final static int MAX = Integer.MAX_VALUE; final static int MIN = Integer.MIN_VALUE; static Random rand = new Random(); // ======================= MAIN ================================== public static void main(String[] args) throws IOException { long time = System.currentTimeMillis(); boolean oj = System.getProperty("ONLINE_JUDGE") != null; // ==== start ==== // int t = readInt(); int t = 1; preprocess(); while (t-- > 0) { solve(); } out.flush(); // ==== end ==== if (!oj) System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" })); } private static void solve() throws IOException { int n = readInt(), p = readInt(); int[] dp = new int[(int) 2e5 + 1]; dp[1] = 1; for (int i = 2; i <= 2e5; i++) dp[i] = (dp[i - 1] + dp[i - 2] + 1) % mod; int[] arr = readIntArray(n); sort(arr); Set<Integer> set = new HashSet<>(); for (int i = 0; i < n; i++) { int num = arr[i]; boolean present = false; while (num > 0) { if ((num & 1) == 1) num >>= 1; else if ((num & 3) == 0) num >>= 2; else break; if (set.contains(num)) { present = true; break; } } if (!present) set.add(arr[i]); } long ans = 0L; for (int i : set) { int x = (int) Math.ceil(Math.log(i + 1) / Math.log(2)); if (x <= p) ans = (ans + dp[p + 1 - x]) % mod; } out.println(ans); } private static void preprocess() { } // ==================== CUSTOM CLASSES ================================ static class Pair implements Comparable<Pair> { int first, second; Pair(int first, int second) { this.first = first; this.second = second; } public int compareTo(Pair o) { if (this.first != o.first) return this.second - o.second; return this.first - o.first; } } static class DequeNode { DequeNode prev, next; int val; DequeNode(int val) { this.val = val; } DequeNode(int val, DequeNode prev, DequeNode next) { this.val = val; this.prev = prev; this.next = next; } } // ======================= FOR INPUT ================================== static String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(readLine()); return st.nextToken(); } static long readLong() throws IOException { return Long.parseLong(next()); } static int readInt() throws IOException { return Integer.parseInt(next()); } static double readDouble() throws IOException { return Double.parseDouble(next()); } static char readCharacter() throws IOException { return next().charAt(0); } static String readString() throws IOException { return next(); } static String readLine() throws IOException { return br.readLine().trim(); } static int[] readIntArray(int n) throws IOException { int[] arr = new int[n]; for (int i = 0; i < n; i++) arr[i] = readInt(); return arr; } static int[][] read2DIntArray(int n, int m) throws IOException { int[][] arr = new int[n][m]; for (int i = 0; i < n; i++) arr[i] = readIntArray(m); return arr; } static List<Integer> readIntList(int n) throws IOException { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(readInt()); return list; } static long[] readLongArray(int n) throws IOException { long[] arr = new long[n]; for (int i = 0; i < n; i++) arr[i] = readLong(); return arr; } static long[][] read2DLongArray(int n, int m) throws IOException { long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) arr[i] = readLongArray(m); return arr; } static List<Long> readLongList(int n) throws IOException { List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(readLong()); return list; } static char[] readCharArray(int n) throws IOException { return readString().toCharArray(); } static char[][] readMatrix(int n, int m) throws IOException { char[][] mat = new char[n][m]; for (int i = 0; i < n; i++) mat[i] = readCharArray(m); return mat; } // ========================= FOR OUTPUT ================================== private static void printIList(List<Integer> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printLList(List<Long> list) { for (int i = 0; i < list.size(); i++) out.print(list.get(i) + " "); out.println(" "); } private static void printIArray(int[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DIArray(int[][] arr) { for (int i = 0; i < arr.length; i++) printIArray(arr[i]); } private static void printLArray(long[] arr) { for (int i = 0; i < arr.length; i++) out.print(arr[i] + " "); out.println(" "); } private static void print2DLArray(long[][] arr) { for (int i = 0; i < arr.length; i++) printLArray(arr[i]); } // ====================== TO CHECK IF STRING IS NUMBER ======================== private static boolean isInteger(String s) { try { Integer.parseInt(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } private static boolean isLong(String s) { try { Long.parseLong(s); } catch (NumberFormatException e) { return false; } catch (NullPointerException e) { return false; } return true; } // ==================== FASTER SORT ================================ private static void sort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(int[] arr) { int n = arr.length; List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void sort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list); for (int i = 0; i < n; i++) arr[i] = list.get(i); } private static void reverseSort(long[] arr) { int n = arr.length; List<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) list.add(arr[i]); Collections.sort(list, Collections.reverseOrder()); for (int i = 0; i < n; i++) arr[i] = list.get(i); } // ==================== MATHEMATICAL FUNCTIONS =========================== private static int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } private static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } private static int lcm(int a, int b) { return (a / gcd(a, b)) * b; } private static long lcm(long a, long b) { return (a / gcd(a, b)) * b; } private static long power(long a, long b) { if (b == 0) return 1L; long ans = power(a, b >> 1); ans *= ans; if ((b & 1) == 1) ans *= a; return ans; } private static int mod_power(int a, int b) { if (b == 0) return 1; int temp = mod_power(a, b >> 1); temp %= mod; temp = (int) ((1L * temp * temp) % mod); if ((b & 1) == 1) temp = (int) ((1L * temp * a) % mod); return temp; } private static int multiply(int a, int b) { return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod); } private static boolean isPrime(long n) { for (long i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true; } private static long nCr(long n, long r) { if (n - r > r) r = n - r; long ans = 1L; for (long i = r + 1; i <= n; i++) ans *= i; for (long i = 2; i <= n - r; i++) ans /= i; return ans; } // ==================== Primes using Seive ===================== private static List<Integer> getPrimes(int n) { boolean[] prime = new boolean[n + 1]; Arrays.fill(prime, true); for (int i = 2; i * i <= n; i++) { if (prime[i]) { for (int j = i * i; j <= n; j += i) prime[j] = false; } } // return prime; List<Integer> list = new ArrayList<>(); for (int i = 2; i <= n; i++) if (prime[i]) list.add(i); return list; } private static int[] SeivePrime(int n) { int[] primes = new int[n]; for (int i = 0; i < n; i++) primes[i] = i; for (int i = 2; i * i < n; i++) { if (primes[i] != i) continue; for (int j = i * i; j < n; j += i) { if (primes[j] == j) primes[j] = i; } } return primes; } // ==================== STRING FUNCTIONS ================================ private static boolean isPalindrome(String str) { int i = 0, j = str.length() - 1; while (i < j) if (str.charAt(i++) != str.charAt(j--)) return false; return true; } private static String reverseString(String str) { StringBuilder sb = new StringBuilder(str); return sb.reverse().toString(); } private static String sortString(String str) { int[] arr = new int[256]; for (char ch : str.toCharArray()) arr[ch]++; StringBuilder sb = new StringBuilder(); for (int i = 0; i < 256; i++) while (arr[i]-- > 0) sb.append((char) i); return sb.toString(); } // ==================== LIS & LNDS ================================ private static int LIS(int arr[], int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find1(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find1(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) >= val) { ret = mid; j = mid - 1; } else { i = mid + 1; } } return ret; } private static int LNDS(int[] arr, int n) { List<Integer> list = new ArrayList<>(); for (int i = 0; i < n; i++) { int idx = find2(list, arr[i]); if (idx < list.size()) list.set(idx, arr[i]); else list.add(arr[i]); } return list.size(); } private static int find2(List<Integer> list, int val) { int ret = list.size(), i = 0, j = list.size() - 1; while (i <= j) { int mid = (i + j) / 2; if (list.get(mid) <= val) { i = mid + 1; } else { ret = mid; j = mid - 1; } } return ret; } // =============== Lower Bound & Upper Bound =========== // less than or equal private static int lower_bound(List<Integer> list, int val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(List<Long> list, long val) { int ans = -1, lo = 0, hi = list.size() - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(int[] arr, int val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } private static int lower_bound(long[] arr, long val) { int ans = -1, lo = 0, hi = arr.length - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] <= val) { ans = mid; lo = mid + 1; } else { hi = mid - 1; } } return ans; } // greater than or equal private static int upper_bound(List<Integer> list, int val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(List<Long> list, long val) { int ans = list.size(), lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (list.get(mid) >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(int[] arr, int val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } private static int upper_bound(long[] arr, long val) { int ans = arr.length, lo = 0, hi = ans - 1; while (lo <= hi) { int mid = (lo + hi) / 2; if (arr[mid] >= val) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } return ans; } // ==================== UNION FIND ===================== private static int find(int x, int[] parent) { if (parent[x] == x) return x; return parent[x] = find(parent[x], parent); } private static boolean union(int x, int y, int[] parent, int[] rank) { int lx = find(x, parent), ly = find(y, parent); if (lx == ly) return false; if (rank[lx] > rank[ly]) parent[ly] = lx; else if (rank[lx] < rank[ly]) parent[lx] = ly; else { parent[lx] = ly; rank[ly]++; } return true; } // ==================== SEGMENT TREE (RANGE SUM) ===================== public static class SegmentTree { int n; int[] arr, tree, lazy; SegmentTree(int arr[]) { this.arr = arr; this.n = arr.length; this.tree = new int[(n << 2)]; this.lazy = new int[(n << 2)]; build(1, 0, n - 1); } void build(int id, int start, int end) { if (start == end) tree[id] = arr[start]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; build(left, start, mid); build(right, mid + 1, end); tree[id] = tree[left] + tree[right]; } } void update(int l, int r, int val) { update(1, 0, n - 1, l, r, val); } void update(int id, int start, int end, int l, int r, int val) { distribute(id, start, end); if (end < l || r < start) return; if (start == end) tree[id] += val; else if (l <= start && end <= r) { lazy[id] += val; distribute(id, start, end); } else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; update(left, start, mid, l, r, val); update(right, mid + 1, end, l, r, val); tree[id] = tree[left] + tree[right]; } } int query(int l, int r) { return query(1, 0, n - 1, l, r); } int query(int id, int start, int end, int l, int r) { if (end < l || r < start) return 0; distribute(id, start, end); if (start == end) return tree[id]; else if (l <= start && end <= r) return tree[id]; else { int mid = (start + end) / 2, left = (id << 1), right = left + 1; return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r); } } void distribute(int id, int start, int end) { if (start == end) tree[id] += lazy[id]; else { tree[id] += lazy[id] * (end - start + 1); lazy[(id << 1)] += lazy[id]; lazy[(id << 1) + 1] += lazy[id]; } lazy[id] = 0; } } // ==================== TRIE ================================ static class Trie { class Node { Node[] children; boolean isEnd; Node() { children = new Node[26]; } } Node root; Trie() { root = new Node(); } public void insert(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) curr.children[ch - 'a'] = new Node(); curr = curr.children[ch - 'a']; } curr.isEnd = true; } public boolean find(String word) { Node curr = root; for (char ch : word.toCharArray()) { if (curr.children[ch - 'a'] == null) return false; curr = curr.children[ch - 'a']; } return curr.isEnd; } } // ==================== FENWICK TREE ================================ static class FT { long[] tree; int n; FT(int[] arr, int n) { this.n = n; this.tree = new long[n + 1]; for (int i = 1; i <= n; i++) { update(i, arr[i - 1]); } } void update(int idx, int val) { while (idx <= n) { tree[idx] += val; idx += idx & -idx; } } long query(int l, int r) { return getSum(r) - getSum(l - 1); } long getSum(int idx) { long ans = 0L; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } } // ==================== BINARY INDEX TREE ================================ static class BIT { long[][] tree; int n, m; BIT(int[][] mat, int n, int m) { this.n = n; this.m = m; tree = new long[n + 1][m + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { update(i, j, mat[i - 1][j - 1]); } } } void update(int x, int y, int val) { while (x <= n) { int t = y; while (t <= m) { tree[x][t] += val; t += t & -t; } x += x & -x; } } long query(int x1, int y1, int x2, int y2) { return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1); } long getSum(int x, int y) { long ans = 0L; while (x > 0) { int t = y; while (t > 0) { ans += tree[x][t]; t -= t & -t; } x -= x & -x; } return 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