content
stringlengths
219
31.2k
complexity
stringclasses
5 values
file_name
stringlengths
6
9
complexity_ranked
float64
0.1
0.9
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long s = sc.nextLong(); long ans = (s - 1) / n + 1; System.out.print(ans); } }
1
600.java
0.1
import java.util.*; public class OrangeJuice{ public static void main(String[] args) { Scanner in=new Scanner(System.in); int n=in.nextInt(); int s=in.nextInt(); if(n>=1&&n<=100000&&s>=1&&s<=1000000000){ if(s%n==0){ System.out.println(s/n); }else{ int o=s%n; System.out.println((s-o)/n+1); } } } }
1
601.java
0.1
import java.util.Scanner; /** * Solution */ public class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), s = sc.nextInt(); int coins; if(s%n != 0) coins = s/n + 1; else coins = s/n; System.out.println(coins); } }
1
602.java
0.1
import java .util.*; import java .io.*; public class Main{ public static void main(String[]YAHIA_MOSTAFA){ Scanner sc =new Scanner(System.in); long n=sc.nextLong(),x=sc.nextLong(),y=sc.nextLong(); long xb,xw,yb,yw; xw=x-1;yw=y-1;xb=n-x;yb=n-y; if (x==n&&y==n){ System.out.println("Black");return; } long c1=0,c2=0; long f =Math.max(xb,yb); long h =Math.max(xw,yw); //System.out.println(h+" "+f+" "+(h-f)); if (h<=f) System.out.println("White"); else System.out.println("Black"); } }
1
615.java
0.1
import java.util.*; import java.io.*; public class Param { public static void main( String[]args) { MyScanner param = new MyScanner(); out = new PrintWriter(new BufferedOutputStream(System.out)); long l2 =param.nextLong(); long dice1=param.nextLong(); long dice2=param.nextLong(); long l1=1; long r1=1; long r2=l2; long count=0; long check=0; count=l2-dice1; count=count+(r2-dice2); check=dice1-l1; check=check+(dice2-r1); if(check<=count){ System.out.println("White"); } else{ System.out.println("Black"); } out.close(); } public static PrintWriter out; //-----------MyScanner class for faster input---------- public static class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } }
1
616.java
0.1
import java.io.BufferedReader; import java.io.InputStreamReader; public class Dont { static BufferedReader jin = new BufferedReader( new InputStreamReader( System.in ) ); static long n; static long x, y; public static void main(String[] args) throws Exception { n = nextLong(); x = nextLong(); y = nextLong(); System.out.println( (max(n-x, n-y) < max(x-1, y-1))? "Black" : "White" ); } static long max(long a, long b) { return (a > b)? a : b ; } static long nextLong() throws Exception { long res = 0; int ch = jin.read(); while(ch < '0' || '9' < ch) ch = jin.read(); while('0'<= ch && ch <= '9') { res = res*10 + ch - '0'; ch = jin.read(); } return res; } }
1
619.java
0.1
// Java program to count // the number of indexes // in range L R such that // Ai=Ai+1 class GFG { public static int N = 1000 ; // Array to store count // of index from 0 to // i that obey condition static int prefixans[] = new int [ 1000 ]; // precomputing prefixans[] array public static void countIndex( int a[], int n) { // traverse to compute // the prefixans[] array for ( int i = 0 ; i < n; i++) { if (i + 1 < n && a[i] == a[i + 1 ]) prefixans[i] = 1 ; if (i != 0 ) prefixans[i] += prefixans[i - 1 ]; } } // function that answers // every query in O(1) public static int answer_query( int l, int r) { if (l == 0 ) return prefixans[r - 1 ]; else return prefixans[r - 1 ] - prefixans[l - 1 ]; } // Driver Code public static void main(String args[]) { int a[] = { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 }; int n = 9 ; // pre-computation countIndex(a, n); int L, R; // 1-st query L = 1 ; R = 8 ; System.out.println(answer_query(L, R)); // 2nd query L = 0 ; R = 4 ; System.out.println(answer_query(L, R)); } } // This code is contributed by Jaideep Pyne
1
63.java
0.1
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int hp = scanner.nextInt(); if (hp%4 == 1){ System.out.println("0 A"); } else if (hp%4 == 2){ System.out.println("1 B"); } else if (hp%4 == 3){ System.out.println("2 A"); } else { System.out.println("1 A"); } } }
1
643.java
0.1
//package Contest573; import java.io.*; import java.util.StringTokenizer; public class mainA { public static PrintWriter out = new PrintWriter(System.out); public static FastScanner enter = new FastScanner(System.in); public static void main(String[] args) throws IOException { int x=enter.nextInt(); if(x%4==0){ System.out.println(1 +" A"); } else if(x%4==1){ System.out.println(0+" A"); } else if(x%4==2){ System.out.println(1+" B"); } else if(x%4==3){ System.out.println(2+" A"); } } static class FastScanner { BufferedReader br; StringTokenizer stok; FastScanner(InputStream is) { br = new BufferedReader(new InputStreamReader(is)); } String next() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } char nextChar() throws IOException { return (char) (br.read()); } String nextLine() throws IOException { return br.readLine(); } } }
1
644.java
0.1
import java.util.*; import java.io.*; public class TokitsukazeAndEnhancement { static char check(int n) { if(n%4 == 0) return 'D'; if(n%4 == 1) return 'A'; if(n%4 == 2) return 'C'; return 'B'; } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(System.out); StringTokenizer st; int n = Integer.parseInt(br.readLine()); int a = n + 1; int b = n + 2; char n1 = check(n); char a1 = check(a); char b1 = check(b); char sol = (char) Math.min(Math.min(a1, b1),n1); if(sol == n1) pw.println(0 + " " + sol); else if(sol == a1) pw.println(1 + " " + sol); else if(sol == b1) pw.println(2 + " " + sol); pw.close(); } }
1
645.java
0.1
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; public class problem2 { static class tile implements Comparable<tile> { int number; int suit; public int compareTo(tile b){ return number - b.number; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String line = sc.nextLine(); String[] parts = line.split(" "); tile[] tiles = new tile[3]; for(int i = 0; i < 3; i++){ tiles[i] = new tile(); tiles[i].number = parts[i].charAt(0) - '0'; if(parts[i].substring(1,2).equals("s"))tiles[i].suit = 1; if(parts[i].substring(1,2).equals("m"))tiles[i].suit = 2; if(parts[i].substring(1,2).equals("p"))tiles[i].suit = 3; //System.out.println(tiles[i].number + " "+ tiles[i].suit); } Arrays.sort(tiles); int[][] tilesObtained = new int[10][4]; int[][] stairCases = new int[10][4]; int[][] stairCases2 = new int[10][4]; for(int i = 0; i < 3; i++){ int currNumber = tiles[i].number; int currSuit = tiles[i].suit; tilesObtained[currNumber][currSuit]++; stairCases[currNumber][currSuit] = 1 + stairCases[currNumber-1][currSuit]; if(currNumber != 1){ if(stairCases2[currNumber-2][currSuit] != 0){ stairCases2[currNumber][currSuit] = 2; } else{ stairCases2[currNumber][currSuit] = 1; } } else{ stairCases2[currNumber][currSuit]++; } } int best = 3; for(int i = 1; i <= 9; i++){ for(int j = 1; j <= 3; j++){ best = Math.min(best, 3 - tilesObtained[i][j]); best = Math.min(best, 3 - stairCases[i][j]); best = Math.min(best, 3- stairCases2[i][j]); if(best <0)best = 0; } } System.out.println(best); } }
1
646.java
0.1
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.StringTokenizer; public class Main { static Scanner sc = new Scanner(System.in); public static char returnLetter(int n) { if(n%4==1) { return 'A'; } else if(n%4==3) { return 'B'; } else if(n%4==2) { return 'C'; } else { return 'D'; } } /*public static void main(String[] args) throws IOException { int n=sc.nextInt(); int zero=n; int one=n+1; int two=n+2; char z=returnLetter(zero); char o=returnLetter(one); char t=returnLetter(two); if(z<o && z<t) { System.out.println("0 " + z); } else if(o<z && o<t) { System.out.println("1 " + o); } else if(t<z && o>t) { System.out.println("2 " + t); } }*/ public static void main(String[] args) throws IOException { String[] a= new String[3]; for (int i = 0; i < a.length; i++) { a[i]=sc.next(); } boolean equal=true; for (int i = 1; i < a.length; i++) { if(a[i].charAt(1)!=a[i-1].charAt(1) || a[i].charAt(0)!=a[i-1].charAt(0)) { equal=false;break; } } if(equal) { System.out.println(0); } else { Arrays.sort(a); boolean ord=true; for (int i = 1; i < a.length; i++) { if(a[i].charAt(1)!=a[i-1].charAt(1) || a[i].charAt(0)-a[i-1].charAt(0)!=1) { ord=false;break; } } if(ord) { System.out.println(0); } else { if(a[1].charAt(1)==a[0].charAt(1) && a[1].charAt(0)-a[0].charAt(0)==2) { System.out.println(1); } else if(a[2].charAt(1)==a[1].charAt(1) && a[2].charAt(0)-a[1].charAt(0)==2) { System.out.println(1); } else if(a[2].charAt(1)==a[0].charAt(1) && a[2].charAt(0)-a[0].charAt(0)==2) { System.out.println(1); } else if(a[0].charAt(1)==a[1].charAt(1) && a[0].charAt(0)-a[1].charAt(0)==2) { System.out.println(1); } else if(a[1].charAt(1)==a[2].charAt(1) && a[1].charAt(0)-a[2].charAt(0)==2) { System.out.println(1); } else if(a[0].charAt(1)==a[2].charAt(1) && a[0].charAt(0)-a[2].charAt(0)==2) { System.out.println(1); } else if(a[1].charAt(1)==a[0].charAt(1) && a[1].charAt(0)-a[0].charAt(0)==1) { System.out.println(1); } else if(a[2].charAt(1)==a[1].charAt(1) && a[2].charAt(0)-a[1].charAt(0)==1) { System.out.println(1); } else if(a[2].charAt(1)==a[0].charAt(1) && a[2].charAt(0)-a[0].charAt(0)==1) { System.out.println(1); } else if(a[0].charAt(1)==a[1].charAt(1) && a[0].charAt(0)-a[1].charAt(0)==1) { System.out.println(1); } else if(a[1].charAt(1)==a[2].charAt(1) && a[1].charAt(0)-a[2].charAt(0)==1) { System.out.println(1); } else if(a[0].charAt(1)==a[2].charAt(1) && a[0].charAt(0)-a[2].charAt(0)==1) { System.out.println(1); } else if(a[1].charAt(1)==a[0].charAt(1) && a[1].charAt(0)==a[0].charAt(0)) { System.out.println(1); } else if(a[2].charAt(1)==a[1].charAt(1) && a[1].charAt(0)==a[2].charAt(0)) { System.out.println(1); } else { System.out.println(2); } } } } static class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s) { br = new BufferedReader(new InputStreamReader(s)); } public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(next()); } public long nextLong() throws IOException { return Long.parseLong(next()); } public String nextLine() throws IOException { return br.readLine(); } public double nextDouble() throws IOException { String x = next(); StringBuilder sb = new StringBuilder("0"); double res = 0, f = 1; int start = 0; boolean dec = false, neg = false; 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 boolean ready() throws IOException { return br.ready(); } } }
1
647.java
0.1
//package codeforces; import java.util.Scanner; public class ex5 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String S [] = new String[3]; int m=0,s=0,p=0; int temp=0; for (int i = 0; i < S.length; i++) { S[i]=scan.next(); if(S[i].indexOf('m')!=-1) m++; if(S[i].indexOf('s')!=-1) s++; if(S[i].indexOf('p')!=-1) p++; } int n1 = Integer.parseInt(S[0].substring(0,1)); int n2 = Integer.parseInt(S[1].substring(0,1)); int n3 = Integer.parseInt(S[2].substring(0,1)); int d3 = Math.abs(n1-n2); int d4 = Math.abs(n1-n3); int d5 = Math.abs(n2-n3); if(m==3||s==3||p==3) { if(d3==1&d5==1&d4==2||d3==1&d4==1&d5==2||d5==1&d4==1&d3==2) System.out.println(0); else if(d3==0&d4==0) System.out.println(0); else if(d3<d5&d3<d4) { if(d3==1||d3==2||d3==0) System.out.println(1); else System.out.println(2); } else if (d5<d4&d5<d3){ if(d5==1||d5==2||d5==0) System.out.println(1); else System.out.println(2); } else if(d4<d5&d4<d3) { if(d4==1||d4==2||d4==0) System.out.println(1); else System.out.println(2); } else if(d3==2&d5==2||d4==2&d5==2||d3==2&d4==2||d3==1&d5==1||d4==1&d5==1||d3==2&d4==1) System.out.println(1); else System.out.println(2); } if(m==2||s==2||p==2) { char c1 = S[0].charAt(1); char c2 = S[1].charAt(1); char c3 = S[2].charAt(1); if(c1==c2) { if(n1==n2) System.out.println(1); else if(d3==1||d3==2) System.out.println(1); else System.out.println(2); } if(c1==c3) { if(n1==n3) System.out.println(1); else if(d4==1||d4==2) System.out.println(1); else System.out.println(2); } if(c2==c3) { if(n2==n3) System.out.println(1); else if(d5==1||d5==2) System.out.println(1); else System.out.println(2); } } if(m==1&s==1&p==1) System.out.println(2); } }
1
648.java
0.1
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Ideone { public static void main (String[] args) throws java.lang.Exception { // your code goes here long n,s,p; Scanner in=new Scanner(System.in); n=in.nextLong(); s=in.nextLong(); if(n==1 && s<=1) { System.out.print(n-1); } else if(s<n) { if(s%2!=0) {System.out.print(s/2);} else {System.out.print(s/2-1);} } else if(s==n) { if(s%2==0) {System.out.println((n/2)-1);} else {System.out.println(n/2);} } else if(s<=(2*n-1)) { System.out.print((2*n+1-s)/2); } else { System.out.print(0); } } }
1
718.java
0.1
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class AAA { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n=Integer.parseInt(st.nextToken()); int m=Integer.parseInt(st.nextToken()); String a=""; String b=""; for(int i=0;i<1129;i++) { a+="1"; b+="8"; } a+="9"; b+="1"; System.out.println(a); System.out.println(b); } }
1
733.java
0.1
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author fintech */ 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); KingEscape solver = new KingEscape(); solver.solve(1, in, out); out.close(); } static class KingEscape { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); int qx = in.nextInt(); int qy = in.nextInt(); int kx = in.nextInt(); int ky = in.nextInt(); int safex = in.nextInt(); int safey = in.nextInt(); boolean possible = false; if (qx > Math.max(kx, safex) || qx < Math.min(kx, safex)) { if (qy > Math.max(ky, safey) || qy < Math.min(ky, safey)) { possible = true; } } if (possible) out.println("YES"); else out.println("NO"); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object... objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) { writer.print(' '); } writer.print(objects[i]); } } public void println(Object... objects) { print(objects); writer.println(); } public void close() { writer.close(); } } 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); } } }
1
743.java
0.1
import java.util.*; public class helloWorld { public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = in.nextLong(); long m = in.nextLong(); long ans = m / n; if(m % n > 0) ans++; System.out.println(ans); in.close(); } }
1
745.java
0.1
import com.sun.org.apache.xalan.internal.xslt.Process; import java.io.*; import java.util.*; public class main { public static void main(String[] args) throws IOException { init();//"prizes.in", "prizes.out"); int x = nextInt(); int y = nextInt(); int z = nextInt(); int t1 = nextInt(); int t2 = nextInt(); int t3 = nextInt(); int lift = Math.abs(z - x) * t2 + 2 * t3 + Math.abs(x - y) * t2 + t3; int stair = Math.abs(x - y) * t1; if (lift <= stair) { pw.print("YES"); } else { pw.print("NO"); } pw.close(); } static StringTokenizer st; static BufferedReader sc; static PrintWriter pw; static String next() throws IOException { while (st == null || !st.hasMoreElements()) { st = new StringTokenizer(sc.readLine()); } return st.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static void init(String in, String out) throws IOException { sc = new BufferedReader(new FileReader(in)); pw = new PrintWriter(out); } static void init() { sc = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(System.out); } } class DSU { int parent[]; public DSU(int n){ parent = new int[n]; for (int i = 0; i < n; i++) { parent[i] = i; } } int get(int i){ if (i == parent[i]){ return i; } int p = get(parent[i]); parent[i] = p; return p; } boolean union(int a, int b){ a = get(a); b = get(b); if (a == b) return false; parent[a] = b; return true; } }
1
757.java
0.1
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long s = sc.nextLong(); long ans = (s - 1) / n + 1; System.out.print(ans); } }
1
766.java
0.1
import java.io.*; import java.util.*; public class Codechef{ public static void main(String []args){ Scanner in = new Scanner(System.in); long n=in.nextLong(); long m=in.nextLong(); long k=in.nextLong(); long l=in.nextLong(); long j=((k+l)/m); if((k+l)%m!=0)j++; if((k+l>n) || j*m>n) { System.out.println(-1); }else { System.out.println(j); } } }
1
775.java
0.1
import java.util.Scanner; public class origami { public static void main(String args[]){ Scanner input = new Scanner(System.in); double n = input.nextInt(); double k = input.nextInt(); double red = 0; double green = 0; double blue = 0; double ans = 0; red = (2 * n) / k; green = (5 * n) / k; blue = (8 * n) / k; double red1 = Math.ceil(red) ; double green1 = Math.ceil(green); double blue1 = Math.ceil(blue); ans+=red1; ans+=green1; ans+=blue1; Double answer = new Double(ans); int finished = answer.intValue(); System.out.println(finished); } }
1
780.java
0.1
import java.util.Scanner; public class MargariteBestPresent_1080B { private static int f(int x) { return (x%2==0)?x/2:(x-1)/2-x; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n,r,l; n = sc.nextInt(); while(n-->0) { l = sc.nextInt(); r = sc.nextInt(); System.out.println(f(r)-f(l-1)); } sc.close(); } }
1
781.java
0.1
import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Map; import java.util.StringTokenizer; import java.util.TreeMap; public class Main { static class Task { int NN = 500005; int MOD = 1000000007; int INF = 2000000000; long INFINITY = 2000000000000000000L; public void solve(InputReader in, PrintWriter out) { int t = in.nextInt(); while(t-->0) { long n =in.nextLong(); long m = in.nextLong(); long x1 = in.nextLong(); long y1 = in.nextLong(); long x2 = in.nextLong(); long y2 = in.nextLong(); long x3 = in.nextLong(); long y3 = in.nextLong(); long x4 = in.nextLong(); long y4 = in.nextLong(); long w = white(1, 1, m, n); long b = black(1, 1, m, n); long whited = 0; if(x3 > x2 || x4 < x1 || y3 > y2 || y4 < y1) { whited = black(x1, y1, x2, y2); } else { whited = black(x1, y1, x2, y2); long xm1 = Math.max(x1, x3); long ym1 = Math.max(y1, y3); long xm2 = Math.min(x2, x4); long ym2 = Math.min(y2, y4); whited -= black(xm1, ym1, xm2, ym2); } b -= whited;w += whited; long blacked = white(x3, y3, x4, y4); w-= blacked;b += blacked; out.println(w + " " + b); } } long black(long x1, long y1, long x2, long y2) { long dx = (x2 - x1) + 1; long dy = (y2 - y1) + 1; if((x1+y1)%2!=0) { return ((dy+1)/2)*((dx+1)/2)+(dy/2)*(dx/2); } return ((dy+1)/2)*((dx)/2)+(dy/2)*((dx+1)/2); } long white(long x1, long y1, long x2, long y2) { long dx = (x2 - x1) + 1; long dy = (y2 - y1) + 1; if((x1+y1)%2==0) { return ((dy+1)/2)*((dx+1)/2)+(dy/2)*(dx/2); } return ((dy+1)/2)*(dx/2)+(dy/2)*((dx+1)/2); } } static void prepareIO(boolean isFileIO) { //long t1 = System.currentTimeMillis(); Task solver = new Task(); // Standard IO if(!isFileIO) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); solver.solve(in, out); //out.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0); out.close(); } // File IO else { String IPfilePath = System.getProperty("user.home") + "/Downloads/ip.in"; String OPfilePath = System.getProperty("user.home") + "/Downloads/op.out"; InputReader fin = new InputReader(IPfilePath); PrintWriter fout = null; try { fout = new PrintWriter(new File(OPfilePath)); } catch (FileNotFoundException e) { e.printStackTrace(); } solver.solve(fin, fout); //fout.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0); fout.close(); } } public static void main(String[] args) { prepareIO(false); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public InputReader(String filePath) { File file = new File(filePath); try { reader = new BufferedReader(new FileReader(file)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } tokenizer = null; } public String nextLine() { String str = ""; try { str = reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return str; } 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()); } } }
1
782.java
0.1
import java.io.*; public class VJudgeProblem2{ public static void main(String[] args) throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int x = Integer.parseInt(reader.readLine()); if (x == 1) System.out.println(-1); else System.out.println(x + " " + x); } }
1
791.java
0.1
import java.util.*; public class Main{ public static void main(String [] args) { Scanner scan=new Scanner(System.in); int q=scan.nextInt(); int i; for(i=1;i<=q;i++) { int a=scan.nextInt(); int ans=a/7+1; System.out.println(ans); } } }
1
802.java
0.1
import java.util.Scanner; public class Main { static Scanner scan = new Scanner(System.in); public static void main(String [] args){ int num = scan.nextInt(); for(int i=0;i<num;i++){ int a = scan.nextInt(); int b = scan.nextInt(); System.out.println(a+" "+2*a); } } }
1
804.java
0.1
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); if(a>=1 && a<=100){ if(a>2){ if(a%2==0){ System.out.println("YES"); }else{ System.out.println("NO"); } }else{ System.out.println("NO"); } } } }
1
813.java
0.1
import java.util.*; public class D5 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int a = input.nextInt(), v = input.nextInt(); int l = input.nextInt(), d = input.nextInt(), w = input.nextInt(); double lo = 0, hi = v; for(int iter = 0; iter < 1000; iter++) { double mid = (lo+hi)/2; if(can(mid, a, d, w)) lo = mid; else hi = mid; } //System.out.println(lo); double t1 = lo / a; double gone = .5 * t1 * t1 * a; if(lo > w) { gone += -a * .5 * (lo - w) / a * (lo - w) / a + lo * (lo - w) / a; t1 += (lo - w) / a; } t1 += (d - gone) / lo; //System.out.println(t1); double v0 = Math.min(lo, w); double togo = l - d; double toAdd = (-v0 + Math.sqrt(v0 * v0 + 4 * togo * .5 * a)) / a; if(toAdd * a + v0 > v) { double tt = (v - v0) / a; t1 += tt; togo -= .5 * a * tt * tt + v0 * tt; t1 += togo / v; } else t1 += toAdd; System.out.println(t1); } static boolean can(double v, double a, double d, double max) { double t1 = v / a; double distGone = .5 * a * t1 * t1; if(v > max) { t1 = (v - max) / a; distGone += -.5 * a * t1 * t1 + v * t1; } return distGone <= d; } }
1
816.java
0.1
import java.io.*; import java.util.*; public class b { public static void main(String[] args) throws IOException, InterruptedException { Scanner zizo = new Scanner(System.in); PrintWriter wr = new PrintWriter(System.out); int n1 = 0,n2 = 0,n3 = 0,n4 = 0; int n = 3; while(n --> 0) { int x = zizo.nextInt(); switch(x) { case 1:n1++;break; case 4:n4++;break; case 2:n2++;break; case 3:n3++;break; } } n2 += n4/2; if(n1>0 || n2>1 || n3>2) System.out.println("YES"); else System.out.println("NO"); wr.close(); } } class pair{ int l,r; pair(int a,int b){l = a;r = b;} } class Scanner { StringTokenizer st; BufferedReader br; public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));} public String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } public int nextInt() throws IOException {return Integer.parseInt(next());} public long nextLong() throws IOException {return Long.parseLong(next());} public String nextLine() throws IOException {return br.readLine();} public double nextDouble() throws IOException { 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 boolean ready() throws IOException {return br.ready();} }
1
830.java
0.1
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class TrickyAlchemy { public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static StringTokenizer st; public static void main(String[] args) throws IOException { long a = nextLong(); long b = nextLong(); long x = nextLong(); long y = nextLong(); long z = nextLong(); System.out.println(Math.max(2*x+y-a, 0) + Math.max(y+3*z-b, 0)); } public static String nextLine() throws IOException { return in.readLine(); } public static String nextString() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextString()); } public static long nextLong() throws IOException { return Long.parseLong(nextString()); } public static int[] intArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public static int[][] intArray(int n, int m) throws IOException { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = nextInt(); return a; } public static long[] longArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }
1
832.java
0.1
import java.util.*; import java.math.*; public class Main{ public static void main(String [] args) { Scanner scan=new Scanner(System.in); long n=scan.nextLong(); long m=scan.nextLong(); n=(long)Math.pow(2,n); long ans=m%n; System.out.println(ans); } }
1
835.java
0.1
import java.util.*; import java.lang.Math; public class tab { public static void main(String[] args) { int n,pos,l,r; Scanner sc=new Scanner(System.in); n=sc.nextInt(); pos=sc.nextInt(); l=sc.nextInt(); r=sc.nextInt(); int sum; int a=(n-r)+(l-1); if((Math.abs(pos-l)<Math.abs(r-pos))&&a!=0) { if(l!=1) {sum=Math.abs(pos-l); sum++; if(r!=n) sum+=(r-l)+1; } else sum=Math.abs(r-pos)+1; } else if(a==0) sum=0; else { if(r!=n) { sum=Math.abs(r-pos); sum++; if(l!=1) sum+=(r-l)+1; } else sum=Math.abs(pos-l)+1; } System.out.println(sum); }}
1
842.java
0.1
import java.util.Scanner; public class Main { //kai public static void main(String[] args) { Scanner kai = new Scanner(System.in); while (kai.hasNext()) { int a = kai.nextInt(); if ((a & 1) == 0) { System.out.println("Mahmoud"); }else { System.out.println("Ehab"); } } } }
1
858.java
0.1
import java.io.*; import java.util.*; public class cf { static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { try { br = new BufferedReader(new InputStreamReader(System.in)); st = new StringTokenizer(br.readLine()); } catch (Exception e){e.printStackTrace();} } public String next() { if (st.hasMoreTokens()) return st.nextToken(); try {st = new StringTokenizer(br.readLine());} catch (Exception e) {e.printStackTrace();} return st.nextToken(); } public int nextInt() {return Integer.parseInt(next());} public long nextLong() {return Long.parseLong(next());} public double nextDouble() {return Double.parseDouble(next());} public String nextLine() { String line = ""; if(st.hasMoreTokens()) line = st.nextToken(); else try {return br.readLine();}catch(IOException e){e.printStackTrace();} while(st.hasMoreTokens()) line += " "+st.nextToken(); return line; } } public static void main(String[] args) { FastScanner sc = new FastScanner(); PrintWriter pw = new PrintWriter(System.out); int n = sc.nextInt(); pw.println(n/2+1); pw.close(); } }
1
868.java
0.1
import java.io.*; import java.util.*; public class Codechef{ static int max=Integer.MIN_VALUE; static int res=0; static int[] checkMax(int arr[],int j){ int sum=0; int x=arr[j]; while(x!=0){ if(j+1==15){ j=0; }else{ arr[j+1]=arr[j+1]+1; } // if(arr[j+1]%2==0){ // sum=sum+arr[j+1]; // if(sum>=max){ // max=sum; // } // } x--; j++; } return arr; } public static void main(String []args){ Scanner sc = new Scanner (System.in); long a [] = new long [14]; long b [] = new long [14]; long p,q,r,s,max = 0; for(int i = 0; i < 14; i++) a[i] = sc.nextInt(); for(int i = 0; i < 14; i++){ p = a[i]%14; q = a[i]/14; r = 0; s = 0; for(int j = 0; j < 14; j++) b[j] = a[j]; b[i] = 0; int j = (i+1)%14; for(; r < p; r++) { b[j]++; j=(j+1)%14; } for( j = 0; j < 14; j++) { b[j] += q; if(b[j] % 2 == 0) s+= b[j]; } max = Math.max(max,s); } System.out.println(max); } }
1
878.java
0.1
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class PizzaPizzaPizza { public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static StringTokenizer st; public static void main(String[] args) throws IOException { long n = nextLong(); if (n == 0) System.out.println(0); else if (n % 2 == 0) System.out.println(n+1); else System.out.println((n+1)/2); } public static String nextLine() throws IOException { return in.readLine(); } public static String nextString() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextString()); } public static long nextLong() throws IOException { return Long.parseLong(nextString()); } public static int[] intArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public static int[][] intArray(int n, int m) throws IOException { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = nextInt(); return a; } public static long[] longArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }
1
889.java
0.1
import java.util.*; import java.lang.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.*; public class Main { public static void main(String[] args) throws Exception{ FastReader sc=new FastReader(); OutputStream outputStream = System.out; PrintWriter out = new PrintWriter(outputStream); int n=sc.nextInt(); HashMap<String,Integer> map=new HashMap<String,Integer>(); for(int i=0;i<n;i++) { map.put(sc.next(), 1); } ArrayList<String> list=new ArrayList<String>(); int count=0; if(!map.containsKey("purple")) { count++; list.add("Power"); } if(!map.containsKey("green")) { count++; list.add("Time"); } if(!map.containsKey("blue")) { count++; list.add("Space"); } if(!map.containsKey("orange")) { count++; list.add("Soul"); } if(!map.containsKey("red")) { count++; list.add("Reality"); } if(!map.containsKey("yellow")) { count++; list.add("Mind"); }System.out.println(count); for(String s:list) { System.out.println(s); } } } 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; } }
1
910.java
0.1
import java.util.*; public class helloWorld { public static void main(String[] args) { Scanner in = new Scanner(System.in); int a = in.nextInt(); int b = in.nextInt(); int c = in.nextInt(); int n = in.nextInt(); int ans = n - (a + b - c); if(ans < 1 || a >= n || b >= n || c > a || c > b) ans = -1; System.out.println(ans); in.close(); } }
1
923.java
0.1
import java.util.*; public class Main { static int mod = 1000000007; static int size = 200000; static long[] fac = new long[size]; static long[] finv = new long[size]; static long[] inv = new long[size]; static int INF = Integer.MAX_VALUE; public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String[] s = new String[2]; for(int i = 0; i < 2; i++){ s[i] = scanner.next(); } int n = s[0].length(); char[][] c = new char[2][n]; for(int i = 0; i < 2; i++){ for(int j = 0; j < n; j++){ c[i][j] = s[i].charAt(j); } } int count = 0; for(int i = 0; i < n-1; i++){ if(c[0][i] == '0' && c[1][i] == '0' && c[0][i+1] == '0'){ c[0][i] = 'X'; c[1][i] = 'X'; c[0][i+1] = 'X'; count++; } if(c[0][i] == '0' && c[1][i] == '0' && c[1][i+1] == '0'){ c[0][i] = 'X'; c[1][i] = 'X'; c[1][i+1] = 'X'; count++; } if(c[0][i] == '0' && c[0][i+1] == '0' && c[1][i+1] == '0'){ c[0][i] = 'X'; c[0][i+1] = 'X'; c[1][i+1] = 'X'; count++; } if(c[0][i+1] == '0' && c[1][i+1] == '0' && c[1][i] == '0'){ c[1][i] = 'X'; c[0][i+1] = 'X'; c[1][i+1] = 'X'; count++; } } System.out.println(count); } public static boolean isPrime(int n){ if(n == 1) return false; if(n == 2 || n == 3) return true; for(int i = 2; i <= Math.sqrt(n); i++){ if(n % i == 0) return false; } return true; } // tar の方が数字が大きいかどうか static boolean compare(String tar, String src) { if (src == null) return true; if (src.length() == tar.length()) { int len = tar.length(); for (int i = 0; i < len; i++) { if (src.charAt(i) > tar.charAt(i)) { return false; } else if (src.charAt(i) < tar.charAt(i)) { return true; } } return tar.compareTo(src) > 0 ? true : false; } else if (src.length() < tar.length()) { return true; } else if (src.length() > tar.length()) { return false; } return false; } public static class Edge{ int to; Edge(int to){ this.to = to; } } public static void swap(long a, long b){ long tmp = 0; if(a > b){ tmp = a; a = b; b = tmp; } } static class Pair implements Comparable<Pair>{ int first, second; Pair(int a, int b){ first = a; second = b; } @Override public boolean equals(Object o){ if (this == o) return true; if (!(o instanceof Pair)) return false; Pair p = (Pair) o; return first == p.first && second == p.second; } @Override public int compareTo(Pair p){ return first == p.first ? second - p.second : first - p.first; //firstで昇順にソート //return (first == p.first ? second - p.second : first - p.first) * -1; //firstで降順にソート //return second == p.second ? first - p.first : second - p.second;//secondで昇順にソート //return (second == p.second ? first - p.first : second - p.second)*-1;//secondで降順にソート } } //繰り返し二乗法 public static long pow(long x, long n){ long ans = 1; while(n > 0){ if((n & 1) == 1){ ans = ans * x; ans %= mod; } x = x * x % mod; n >>= 1; } return ans; } public static long div(long x, long y){ return (x*pow(y, mod-2))%mod; } //fac, inv, finvテーブルの初期化、これ使う場合はinitComb()で初期化必要 public static void initComb(){ fac[0] = finv[0] = inv[0] = fac[1] = finv[1] = inv[1] = 1; for (int i = 2; i < size; ++i) { fac[i] = fac[i - 1] * i % mod; inv[i] = mod - (mod / i) * inv[(int) (mod % i)] % mod; finv[i] = finv[i - 1] * inv[i] % mod; } } //nCk % mod public static long comb(int n, int k){ return fac[n] * finv[k] % mod * finv[n - k] % mod; } //n! % mod public static long fact(int n){ return fac[n]; } //(n!)^-1 with % mod public static long finv(int n){ return finv[n]; } static class UnionFind { int[] parent; public UnionFind(int size) { parent = new int[size]; Arrays.fill(parent, -1); } public boolean unite(int x, int y) { x = root(x); y = root(y); if (x != y) { if (parent[y] < parent[x]) { int tmp = y; y = x; x = tmp; } parent[x] += parent[y]; parent[y] = x; return true; } return false; } public boolean same(int x, int y) { return root(x) == root(y); } public int root(int x) { return parent[x] < 0 ? x : (parent[x] = root(parent[x])); } public int size(int x) { return -parent[root(x)]; } } public static int upperBound(int[] array, int value) { int low = 0; int high = array.length; int mid; while( low < high ) { mid = ((high - low) >>> 1) + low; // (high + low) / 2 if( array[mid] <= value ) { low = mid + 1; } else { high = mid; } } return low; } public static final int lowerBound(final int[] arr, final int value) { int low = 0; int high = arr.length; int mid; while (low < high){ mid = ((high - low) >>> 1) + low; //(low + high) / 2 (オーバーフロー対策) if (arr[mid] < value) { low = mid + 1; } else { high = mid; } } return low; } //n,mの最大公約数 public static long gcd(long n, long m){ if(m > n) return gcd(m,n); if(m == 0) return n; return gcd(m, n%m); } //3要素のソート private class Pair2 implements Comparable<Pair2> { String s; int p; int index; public Pair2(String s, int p, int index) { this.s = s; this.p = p; this.index = index; } public int compareTo(Pair2 other) { if (s.equals(other.s)) { return other.p - this.p; } return this.s.compareTo(other.s); } } //c -> intに変換 public static int c2i(char c){ if('A' <= c && c <= 'Z'){ return c - 'A'; }else{ return c - 'a' + 26; } } // int -> charに変換 public static char i2c(int i){ if(0 <= i && i < 26){ return (char)(i + 'A'); }else{ return (char)(i + 'a' - 26); } } }
1
925.java
0.1
import java.util.*; import java.io.*; import java.text.*; import java.math.*; import static java.lang.Integer.*; import static java.lang.Double.*; import java.lang.Math.*; public class two_squares { public static void main(String[] args) throws Exception { new two_squares().run(); } public void run() throws Exception { FastIO file = new FastIO(); double x1 = file.nextInt(); double y1 = file.nextInt(); double x2 = file.nextInt(); double y2 = file.nextInt(); double x3 = file.nextInt(); double y3 = file.nextInt(); double x4 = file.nextInt(); double y4 = file.nextInt(); double minx1, maxx1, miny1, maxy1; minx1 = Math.min(x1, Math.min(x2, Math.min(x3, x4))); maxx1 = Math.max(x1, Math.max(x2, Math.max(x3, x4))); miny1 = Math.min(y1, Math.min(y2, Math.min(y3, y4))); maxy1 = Math.max(y1, Math.max(y2, Math.max(y3, y4))); double x5 = file.nextInt(); double y5 = file.nextInt(); double x6 = file.nextInt(); double y6 = file.nextInt(); double x7 = file.nextInt(); double y7 = file.nextInt(); double x8 = file.nextInt(); double y8 = file.nextInt(); double minx2, maxx2, miny2, maxy2; minx2 = Math.min(x5, Math.min(x6, Math.min(x7, x8))); maxx2 = Math.max(x5, Math.max(x6, Math.max(x7, x8))); miny2 = Math.min(y5, Math.min(y6, Math.min(y7, y8))); maxy2 = Math.max(y5, Math.max(y6, Math.max(y7, y8))); Point _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, _15, _16; _1 = new Point(x1, y1); _2 = new Point(x2, y2); _3 = new Point(x3, y3); _4 = new Point(x4, y4); _5 = new Point(x5, y5); _6 = new Point(x6, y6); _7 = new Point(x7, y7); _8 = new Point(x8, y8); _9 = new Point(minx1, maxy1); _10 = new Point(minx1, miny1); _11 = new Point(maxx1, maxy1); _12 = new Point(maxx1, miny1); double m1 = (minx2 + maxx2) / 2; double m2 = (miny2 + maxy2) / 2; _13 = new Point(minx2, m2); _14 = new Point(m1, miny2); _15 = new Point(maxx2, m2); _16 = new Point(m1, maxy2); Point[] a = {_1, _2, _3, _4}; Point[] b = {_5, _6, _7, _8}; boolean works = false; Line[] aa = {new Line(_9,_10), new Line(_10, _12), new Line(_12, _11), new Line(_11, _9)}; Line[] bb = {new Line(_13, _14), new Line(_14, _15), new Line(_15, _16), new Line(_16, _13)}; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { if (aa[i].intersection(bb[i]) != null) { works = true; } } } for (Point p : b) { if (p.x >= minx1 && p.x <= maxx1 && p.y >= miny1 && p.y <= maxy1) { works = true; } } for (Point p : a) { boolean result = false; for (int i = 0, j = b.length - 1; i < b.length; j = i++) { if ((b[i].y > p.y) != (b[j].y > p.y) && (p.x < (b[j].x - b[i].x) * (p.y - b[i].y) / (b[j].y-b[i].y) + b[i].x)) { result = !result; } } if (result) works = true; } System.out.println(works ? "YES" : "NO"); } public static class Point { double x, y; public Point(double a, double b) { x = a; y = b; } } public static class Line { Point a, b; public Line(Point x, Point y) { a = x; b = y; } public Point intersection(Line o) { double x1 = a.x; double y1 = a.y; double x2 = b.x; double y2 = b.y; double x3 = o.a.x; double y3 = o.a.y; double x4 = o.b.x; double y4 = o.b.y; double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1); double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3))/denom; double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3))/denom; if (ua >= 0.0f && ua <= 1.0f && ub >= 0.0f && ub <= 1.0f) { return new Point((int) (x1 + ua*(x2 - x1)), (int) (y1 + ua*(y2 - y1))); } return null; } } public static class FastIO { BufferedReader br; StringTokenizer st; public FastIO() { 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 long pow(long n, long p, long mod) { if (p == 0) return 1; if (p == 1) return n % mod; if (p % 2 == 0) { long temp = pow(n, p / 2, mod); return (temp * temp) % mod; } else { long temp = pow(n, p / 2, mod); temp = (temp * temp) % mod; return (temp * n) % mod; } } public static long pow(long n, long p) { if (p == 0) return 1; if (p == 1) return n; if (p % 2 == 0) { long temp = pow(n, p / 2); return (temp * temp); } else { long temp = pow(n, p / 2); temp = (temp * temp); return (temp * n); } } public static long gcd(long x, long y) { if (x == 0) return y; else return gcd(y % x, x); } public static boolean isPrime(int n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } }
1
931.java
0.1
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class AlexAndARhombus { public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); public static StringTokenizer st; public static void main(String[] args) throws IOException { int n = nextInt(); System.out.println(n*n+(n-1)*(n-1)); } public static String nextLine() throws IOException { return in.readLine(); } public static String nextString() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } public static int nextInt() throws IOException { return Integer.parseInt(nextString()); } public static long nextLong() throws IOException { return Long.parseLong(nextString()); } public static int[] intArray(int n) throws IOException { int[] a = new int[n]; for (int i = 0; i < n; i++) a[i] = nextInt(); return a; } public static int[][] intArray(int n, int m) throws IOException { int[][] a = new int[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) a[i][j] = nextInt(); return a; } public static long[] longArray(int n) throws IOException { long[] a = new long[n]; for (int i = 0; i < n; i++) a[i] = nextLong(); return a; } }
1
995.java
0.1
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; import java.util.LinkedList; import java.util.List; import java.util.Queue; public class simple implements Runnable { public void run() { InputReader input = new InputReader(System.in); PrintWriter w = new PrintWriter(System.out); int n = input.nextInt(); System.out.println((2*n*n)-(2*n)+1); } class Graph{ private final int v; private List<List<Integer>> adj; Graph(int v){ this.v = v; adj = new ArrayList<>(v); for(int i=0;i<v;i++){ adj.add(new LinkedList<>()); } } private void addEdge(int a,int b){ adj.get(a).add(b); } private boolean isCyclic() { boolean[] visited = new boolean[v]; boolean[] recStack = new boolean[v]; for (int i = 0; i < v; i++) if (isCyclicUtil(i, visited, recStack)) return true; return false; } private boolean isCyclicUtil(int i, boolean[] visited, boolean[] recStack) { if (recStack[i]) return true; if (visited[i]) return false; visited[i] = true; recStack[i] = true; List<Integer> children = adj.get(i); for (Integer c: children) if (isCyclicUtil(c, visited, recStack)) return true; recStack[i] = false; return false; } } public static void sortbyColumn(int arr[][], int col) { Arrays.sort(arr, new Comparator<int[]>() { public int compare(int[] o1, int[] o2){ return(Integer.valueOf(o1[col]).compareTo(o2[col])); } }); } static long gcd(long a, long b) { if (b == 0) return a; return gcd(b, a % b); } public static class DJSet { public int[] upper; public DJSet(int n) { upper = new int[n]; Arrays.fill(upper, -1); } public int root(int x) { return upper[x] < 0 ? x : (upper[x] = root(upper[x])); } public boolean equiv(int x, int y) { return root(x) == root(y); } public boolean union(int x, int y) { x = root(x); y = root(y); if (x != y) { if (upper[y] < upper[x]) { int d = x; x = y; y = d; } upper[x] += upper[y]; upper[y] = x; } return x == y; } } public static int[] radixSort(int[] f) { int[] to = new int[f.length]; { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]&0xffff)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]&0xffff]++] = f[i]; int[] d = f; f = to;to = d; } { int[] b = new int[65537]; for(int i = 0;i < f.length;i++)b[1+(f[i]>>>16)]++; for(int i = 1;i <= 65536;i++)b[i]+=b[i-1]; for(int i = 0;i < f.length;i++)to[b[f[i]>>>16]++] = f[i]; int[] d = f; f = to;to = d; } return f; } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } public static void main(String args[]) throws Exception { new Thread(null, new simple(),"TaskA",1<<26).start(); } }
1
996.java
0.1
// A O(1) Java program to // find number of strings // that can be made under // given constraints. import java.io.*; class GFG { static int countStr( int n) { return 1 + (n * 2 ) + (n * ((n * n) - 1 ) / 2 ); } // Driver code public static void main (String[] args) { int n = 3 ; System.out.println( countStr(n)); } } // This code is contributed by ajit
1
262.java
0.1
// Java program to demonstrate working of // an algorithm that finds an element in an // array of infinite size class Test { // Simple binary search algorithm static int binarySearch( int arr[], int l, int r, int x) { if (r>=l) { int mid = l + (r - l)/ 2 ; if (arr[mid] == x) return mid; if (arr[mid] > x) return binarySearch(arr, l, mid- 1 , x); return binarySearch(arr, mid+ 1 , r, x); } return - 1 ; } // Method takes an infinite size array and a key to be // searched and returns its position if found else -1. // We don't know size of arr[] and we can assume size to be // infinite in this function. // NOTE THAT THIS FUNCTION ASSUMES arr[] TO BE OF INFINITE SIZE // THEREFORE, THERE IS NO INDEX OUT OF BOUND CHECKING static int findPos( int arr[], int key) { int l = 0 , h = 1 ; int val = arr[ 0 ]; // Find h to do binary search while (val < key) { l = h; // store previous high //check that 2*h doesn't exceeds array //length to prevent ArrayOutOfBoundException if ( 2 *h < arr.length- 1 ) h = 2 *h; else h = arr.length- 1 ; val = arr[h]; // update new val } // at this point we have updated low // and high indices, thus use binary // search between them return binarySearch(arr, l, h, key); } // Driver method to test the above function public static void main(String[] args) { int arr[] = new int []{ 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 }; int ans = findPos(arr, 10 ); if (ans==- 1 ) System.out.println( "Element not found" ); else System.out.println( "Element found at index " + ans); } }
logn
111.java
0.3
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { FastReader in; PrintWriter out; int n; public void solve(int testNumber, FastReader in, PrintWriter out) { this.in = in; this.out = out; n = in.nextInt(); if (n % 4 != 0) { out.println("! -1"); return; } int low = 0; int high = n >> 1; int fSign = Integer.signum(BValue(low)); if (fSign == 0) { out.println("! " + (low + 1)); return; } while (high - low > 1) { int mid = (high + low) >> 1; int mSign = Integer.signum(BValue(mid)); if (mSign == 0) { out.println("! " + (mid + 1)); return; } if (mSign == -fSign) { high = mid; } else { low = mid; } } out.println("! -1"); } public int BValue(int index) { out.println("? " + (index + 1)); out.flush(); int f = in.nextInt(); out.println("? " + (index + 1 + (n >> 1))); out.flush(); int s = in.nextInt(); return f - s; } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
logn
1157.java
0.3
import java.io.*; import java.util.StringTokenizer; import static java.lang.Math.max; import static java.lang.Math.min; public class Main { FastScanner in; PrintWriter out; void run() { in = new FastScanner(); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } public static void main(String[] args) { new Main().run(); } void solve() { int t = in.nextInt(); for (int sdfsdf = 0; sdfsdf < t; sdfsdf++) { long n = in.nextLong(); long k = in.nextLong(); if (n == 1) { if (k == 1) { out.println("YES 0"); } else { out.println("NO"); } continue; } if (k == 3) { if (n == 2) { out.println("NO"); } else { out.println("YES " + (n - 1)); } continue; } long cuts = 1; long squares = 4; int zoom = 1; while (k > cuts + squares) { cuts += squares; squares *= 4; zoom++; } if (zoom > n) { out.println("NO"); continue; } if (zoom == n && k > cuts) { out.println("NO"); continue; } long current_cuts = k - cuts; if (current_cuts > squares - (2L * Math.sqrt(squares) - 1L)) { out.println("YES " + (n - zoom - 1L)); } else { out.println("YES " + (n - zoom)); } } } }
logn
1158.java
0.3
import java.util.*; public class ehab4 { public static void main( String[] args ) { Scanner in = new Scanner( System.in ); int a = 0, b = 0; System.out.println( "? 0 0 " ); System.out.flush(); int c = in.nextInt(); for ( int i = 29; i >= 0; i-- ) { System.out.println( "? " + ( a + ( 1 << i ) ) + " " + b ); System.out.flush(); int q1 = in.nextInt(); System.out.println( "? " + a + " " + ( b + ( 1 << i ) ) ); System.out.flush(); int q2 = in.nextInt(); if ( q1 == q2 ) { if ( c == 1 ) a += ( 1 << i ); else if ( c == -1 ) b += ( 1 << i ); c = q1; } else if ( q1 == -1 ) { a += ( 1 << i ); b += ( 1 << i ); } else if ( q1 == -2 ) return; } System.out.println( "! " + a + " " + b ); System.out.flush(); } }
logn
1159.java
0.3
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.*; import java.util.*; import java.math.*; import java.lang.*; import java.util.PriorityQueue; import static java.lang.Math.*; public class solution implements Runnable { static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; private BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static int mod = (int)1e9+7; public static long fastexpo(long pow) { long expo = 2; long ans = 1; while(pow!=0) { if((pow&1)==1) { ans = (ans*expo)%mod; } expo = (expo*expo)%mod; pow = pow>>1; } return ans; } public static void main(String args[]) throws Exception { new Thread(null, new solution(),"Main",1<<26).start(); } public void run() { InputReader sc = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); long x = sc.nextLong(); if(x==0) { out.println(0); out.close(); return; } long k = sc.nextLong(); long a = ((fastexpo(k+1)%mod)*(x%mod))%mod; long b = (-1*fastexpo(k)%mod+mod)%mod; long ans = (a+b+1)%mod; out.println(ans); out.close(); } }
logn
1160.java
0.3
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { FastReader in; PrintWriter out; int n; public void solve(int testNumber, FastReader in, PrintWriter out) { this.in = in; this.out = out; n = in.nextInt(); if (n % 4 != 0) { out.println("! -1"); return; } int low = 0; int high = n >> 1; if (BValue(low) == 0) { out.println("! " + (low + 1)); return; } int fSign = Integer.signum(BValue(low)); while (high - low > 1) { int mid = (high + low) >> 1; int mSign = Integer.signum(BValue(mid)); if (mSign == 0) { out.println("! " + (mid + 1)); return; } if (mSign == -fSign) { high = mid; } else { low = mid; } } out.println("! -1"); } public int BValue(int index) { out.println("? " + (index + 1)); out.flush(); int f = in.nextInt(); out.println("? " + (index + 1 + (n >> 1))); out.flush(); int s = in.nextInt(); return f - s; } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
logn
1161.java
0.3
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Pradyumn */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastReader in = new FastReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { FastReader in; PrintWriter out; int n; public void solve(int testNumber, FastReader in, PrintWriter out) { this.in = in; this.out = out; n = in.nextInt(); if (n % 4 != 0) { out.println("! -1"); return; } int low = 0; int high = n >> 1; if (BValue(low) == 0) { out.println("! " + (low + 1)); return; } boolean value = BValue(low) > 0; while (high - low > 1) { int mid = (high + low) >> 1; int BVal = BValue(mid); if (BVal == 0) { out.println("! " + (mid + 1)); return; } if (value) { if (BVal < 0) { high = mid; } else { low = mid; } } else { if (BVal > 0) { high = mid; } else { low = mid; } } } out.println("! -1"); } public int BValue(int index) { out.println("? " + (index + 1)); out.flush(); int f = in.nextInt(); out.println("? " + (index + 1 + (n >> 1))); out.flush(); int s = in.nextInt(); return f - s; } } static class FastReader { private InputStream stream; private byte[] buf = new byte[8192]; private int curChar; private int pnumChars; public FastReader(InputStream stream) { this.stream = stream; } private int pread() { if (pnumChars == -1) { throw new InputMismatchException(); } if (curChar >= pnumChars) { curChar = 0; try { pnumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (pnumChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = pread(); while (isSpaceChar(c)) c = pread(); int sgn = 1; if (c == '-') { sgn = -1; c = pread(); } int res = 0; do { if (c == ',') { c = pread(); } if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = pread(); } while (!isSpaceChar(c)); return res * sgn; } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } }
logn
1162.java
0.3
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author kessido */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); BTheHat solver = new BTheHat(); solver.solve(1, in, out); out.close(); } static class BTheHat { PrintWriter out; InputReader in; int n; public void solve(int testNumber, InputReader in, PrintWriter out) { this.out = out; this.in = in; n = in.NextInt(); int desiredPair = -1; int result = query(1); if (result != 0) { int l = 2, r = 1 + n / 2; while (l < r) { int m = (l + r) / 2; int mRes = query(m); if (mRes == 0) { desiredPair = m; break; } else if (mRes == result) { l = m + 1; } else { r = m; } } } else { desiredPair = 1; } out.println("! " + desiredPair); } private int query(int i) { int iV = queryValue(i); int iN2V = queryValue(i + n / 2); if (iV < iN2V) { return -1; } else if (iV > iN2V) { return 1; } return 0; } private int queryValue(int i) { out.println("? " + i); out.flush(); return in.NextInt(); } } static class InputReader { BufferedReader reader; StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine(), " \t\n\r\f,"); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int NextInt() { return Integer.parseInt(next()); } } }
logn
1164.java
0.3
import java.io.BufferedReader; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class EhabAndAnotherAnotherXorProblem implements Closeable { private InputReader in = new InputReader(System.in); private PrintWriter out = new PrintWriter(System.out); public void solve() { int initial = ask(0, 0); int a = 0, b = 0; if (initial == 0) { for (int i = 0; i < 30; i++) { int response = ask(1 << i, 0); if (response == -1) { a |= (1 << i); } } b = a; } else { for (int i = 29; i >= 0; i--) { int response = ask(a | (1 << i), b | (1 << i)); if (response != initial) { if (response == 1) { b |= (1 << i); } else { a |= (1 << i); } initial = ask(a, b); } else { response = ask(a | (1 << i), b); if (response == -1) { a |= (1 << i); b |= (1 << i); } } } } answer(a, b); } private int ask(int c, int d) { out.printf("? %d %d\n", c, d); out.flush(); return in.ni(); } private void answer(int a, int b) { out.printf("! %d %d\n", a, b); out.flush(); } @Override public void close() throws IOException { in.close(); out.close(); } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int ni() { return Integer.parseInt(next()); } public long nl() { return Long.parseLong(next()); } public void close() throws IOException { reader.close(); } } public static void main(String[] args) throws IOException { try (EhabAndAnotherAnotherXorProblem instance = new EhabAndAnotherAnotherXorProblem()) { instance.solve(); } } }
logn
1165.java
0.3
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Liavontsi Brechka */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); DEhabIEsheOdnaOcherednayaZadachaNaXor solver = new DEhabIEsheOdnaOcherednayaZadachaNaXor(); solver.solve(1, in, out); out.close(); } static class DEhabIEsheOdnaOcherednayaZadachaNaXor { public void solve(int testNumber, InputReader in, PrintWriter out) { int c = 0; int d = 0; int prevSign = 0; int nextSign; boolean zeroOut = true; for (int i = 29; i >= 0; i--) { if (zeroOut) { print(c, d, out); prevSign = read(in); } print((1 << i) | c, (1 << i) | d, out); nextSign = read(in); if (prevSign == nextSign) { zeroOut = false; print((1 << i) | c, d, out); nextSign = read(in); if (nextSign < 0) { c = (1 << i) | c; d = (1 << i) | d; } } else { zeroOut = true; if (nextSign < 0) c = (1 << i) | c; else d = (1 << i) | d; } } out.printf("! %d %d", c, d); out.flush(); } private void print(int c, int d, PrintWriter out) { out.printf("? %d %d\n", c, d); out.flush(); } private int read(InputReader in) { return in.nextInt(); } } static class InputReader { private final BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream in) { reader = new BufferedReader(new InputStreamReader(in)); } public int nextInt() { return Integer.parseInt(next()); } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(readLine()); } return tokenizer.nextToken(); } public String readLine() { String line; try { line = reader.readLine(); } catch (IOException e) { throw new RuntimeException(e); } return line; } } }
logn
1166.java
0.3
import java.util.*; public class mad{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int cura = 0,curb = 0; int ver; System.out.println("? 0 0"); System.out.flush(); ver = sc.nextInt(); for(int i=29;i>=0;i--){ System.out.println("? "+(cura+(1<<i))+" "+curb); System.out.flush(); int temp1 = sc.nextInt(); System.out.println("? "+cura+" "+(curb+(1<<i))); System.out.flush(); int temp2 = sc.nextInt(); if(temp1!=temp2){ if(temp2==1){ cura += (1<<i); curb += (1<<i); } } else{ if(ver==1) cura += (1<<i); if(ver==-1) curb += (1<<i); ver = temp1; } } System.out.println("! "+cura+" "+curb); } }
logn
1168.java
0.3
import java.util.*; import java.lang.*; public class Main { static long m = 1000000007; static long powmod(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if((y & 1)==1) res = (res * x) % p; // y must be even now // y = y / 2 y = y >> 1; x = (x * x) % p; } return res; } static long mulmod(long a, long b, long mod){ long res=0; a = a % mod; while (b > 0) { // If b is odd, add 'a' to result if (b % 2 == 1) res = (res + a) % mod; // Multiply 'a' with 2 a = (a * 2) % mod; // Divide b by 2 b /= 2; } // Return result return res % mod; } public static void main(String args[] ) throws Exception { Scanner sc = new Scanner(System.in); long x = sc.nextLong(); long k = sc.nextLong(); if(x>0) { long d= powmod(2,k,m); long ans= mulmod(d,2,m)%m; ans= mulmod(ans,x,m)%m; ans++; ans%=m; ans= (ans-d+m)%m; System.out.println(ans); } else System.out.println(0); } }
logn
1169.java
0.3
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ public class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner in=new Scanner(System.in); long x=in.nextLong(); long k=in.nextLong(); long mod=1000000007; long get=power(2,k,mod); long ans=((get%mod)*((2*x)%mod))%mod-get+1; if(ans<0) ans+=mod; if(x==0) ans=0; System.out.println(ans); } static long power(long x, long y, long p) { // Initialize result long res = 1; // Update x if it is more // than or equal to p x = x % p; while (y > 0) { // If y is odd, multiply x // with result if((y & 1)==1) res = (res * x) % p; // y must be even now // y = y / 2 y = y >> 1; x = (x * x) % p; } return res; } }
logn
1170.java
0.3
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.math.BigInteger; import java.io.IOException; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author programajor */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskC solver = new TaskC(); solver.solve(1, in, out); out.close(); } static class TaskC { BigInteger mod = new BigInteger("1000000007"); public void solve(int testNumber, InputReader in, PrintWriter out) { BigInteger x = new BigInteger(in.next()); BigInteger k = new BigInteger(in.next()); if (x.longValue() == 0) { out.print(x); return; } BigInteger pow = powerWithMod(new BigInteger("2"), k); BigInteger current = x.mod(mod).multiply(pow).mod(mod); BigInteger result = current.multiply(new BigInteger("2")).mod(mod) .subtract(pow.subtract(new BigInteger("1")).mod(mod)) .mod(mod); out.print(result); } BigInteger powerWithMod(BigInteger base, BigInteger exponent) { if (exponent.longValue() == 0) { return new BigInteger("1"); } BigInteger temp = powerWithMod(base, exponent.divide(new BigInteger("2"))); BigInteger term = temp.mod(mod); if (exponent.mod(new BigInteger("2")).intValue() == 0) { return term.multiply(term.mod(mod)).mod(mod); } else { return term.multiply(term.mod(mod)).multiply(base.mod(mod)).mod(mod); } } } static class InputReader { private BufferedReader reader; private 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(); } } }
logn
1171.java
0.3
class Main { /* Function to get index of ceiling of x in arr[low..high]*/ static int ceilSearch( int arr[], int low, int high, int x) { int mid; /* If x is smaller than or equal to the first element, then return the first element */ if (x <= arr[low]) return low; /* If x is greater than the last element, then return -1 */ if (x > arr[high]) return - 1 ; /* get the index of middle element of arr[low..high]*/ mid = (low + high)/ 2 ; /* low + (high - low)/2 */ /* If x is same as middle element, then return mid */ if (arr[mid] == x) return mid; /* If x is greater than arr[mid], then either arr[mid + 1] is ceiling of x or ceiling lies in arr[mid+1...high] */ else if (arr[mid] < x) { if (mid + 1 <= high && x <= arr[mid+ 1 ]) return mid + 1 ; else return ceilSearch(arr, mid+ 1 , high, x); } /* If x is smaller than arr[mid], then either arr[mid] is ceiling of x or ceiling lies in arr[mid-1...high] */ else { if (mid - 1 >= low && x > arr[mid- 1 ]) return mid; else return ceilSearch(arr, low, mid - 1 , x); } } /* Driver program to check above functions */ public static void main (String[] args) { int arr[] = { 1 , 2 , 8 , 10 , 10 , 12 , 19 }; int n = arr.length; int x = 8 ; int index = ceilSearch(arr, 0 , n- 1 , x); if (index == - 1 ) System.out.println( "Ceiling of " +x+ " doesn't exist in array" ); else System.out.println( "ceiling of " +x+ " is " +arr[index]); } }
logn
125.java
0.3
/* Program to check for majority element in a sorted array */ import java.io.*; class Majority { /* If x is present in arr[low...high] then returns the index of first occurrence of x, otherwise returns -1 */ static int _binarySearch( int arr[], int low, int high, int x) { if (high >= low) { int mid = (low + high)/ 2 ; /*low + (high - low)/2;*/ /* Check if arr[mid] is the first occurrence of x. arr[mid] is first occurrence if x is one of the following is true: (i) mid == 0 and arr[mid] == x (ii) arr[mid-1] < x and arr[mid] == x */ if ( (mid == 0 || x > arr[mid- 1 ]) && (arr[mid] == x) ) return mid; else if (x > arr[mid]) return _binarySearch(arr, (mid + 1 ), high, x); else return _binarySearch(arr, low, (mid - 1 ), x); } return - 1 ; } /* This function returns true if the x is present more than n/2 times in arr[] of size n */ static boolean isMajority( int arr[], int n, int x) { /* Find the index of first occurrence of x in arr[] */ int i = _binarySearch(arr, 0 , n- 1 , x); /* If element is not present at all, return false*/ if (i == - 1 ) return false ; /* check if the element is present more than n/2 times */ if (((i + n/ 2 ) <= (n - 1 )) && arr[i + n/ 2 ] == x) return true ; else return false ; } /*Driver function to check for above functions*/ public static void main (String[] args) { int arr[] = { 1 , 2 , 3 , 3 , 3 , 3 , 10 }; int n = arr.length; int x = 3 ; if (isMajority(arr, n, x)== true ) System.out.println(x + " appears more than " + n/ 2 + " times in arr[]" ); else System.out.println(x + " does not appear more than " + n/ 2 + " times in arr[]" ); } } /*This code is contributed by Devesh Agrawal*/
logn
127.java
0.3
package com.rampatra.searching; /** * Created by IntelliJ IDEA. * * @author rampatra * @version 9/1/15 */ public class BinarySearch { /** * Searches an element {@param n} in a sorted array {@param a} * and returns its index in O(log n) time. The Index may not * correspond to the first occurrence of the element. * * @param a sorted array to be searched * @param n number to be searched in the array * @return index of {@param n} or {@code -1} if not present */ private static int binarySearch(int[] a, int n) { return binarySearch(a, n, 0, a.length - 1); } public static int binarySearch(int[] a, int n, int low, int high) { if (low <= high) { int mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2 if (n == a[mid]) { return mid; } else if (n < a[mid]) { return binarySearch(a, n, 0, mid - 1); } else { return binarySearch(a, n, mid + 1, high); } } else { return -1; } } /** * Non-recursive version of binary search. * * @param a sorted array to be searched * @param n number to be searched in the array * @return index of {@param n} or {@code -1} if not present */ private static int binarySearchNonRecursive(int[] a, int n) { int low = 0, high = a.length, mid; while (low <= high) { mid = (low + high) / 2; // to prevent overflow you can instead do: mid = low + (high - low) / 2 if (n == a[mid]) { return mid; } else if (n < a[mid]) { high = mid - 1; } else { low = mid + 1; } } return -1; } /** * Driver for testing. * * @param a */ public static void main(String[] args) { System.out.println(binarySearch(new int[]{0, 2}, 2)); System.out.println(binarySearch(new int[]{0, 1, 2, 3}, 2)); System.out.println(binarySearch(new int[]{0, 1, 2, 3}, 3)); System.out.println(binarySearch(new int[]{0, 2}, 0)); System.out.println(binarySearch(new int[]{0, 1, 2, 2, 2, 3, 3}, 2)); // doesn't return index of first occurrence System.out.println("---------"); System.out.println(binarySearchNonRecursive(new int[]{0, 2}, 2)); System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 3}, 2)); System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 3}, 3)); System.out.println(binarySearchNonRecursive(new int[]{0, 2}, 0)); System.out.println(binarySearchNonRecursive(new int[]{0, 1, 2, 2, 2, 3, 3}, 2)); } }
logn
1500.java
0.3
class findMedianTwoArrays { public static double main( int[] arr1, int start1, int end1, int[] arr2, int start2, int end2) { if ( end1 - start1 == 1 && end2 - start2 == 1 ) { return ( Math.max(arr1[start1], arr2[start2]) + Math.min(arr1[end1], arr2[end2]) ) / 2; } int median1Index = Math.floor((start1 + end1 ) / 2); int median2Index = Math.floor((start2 + end2 ) / 2); int median1 = arr1[median1Index]; int median2 = arr2[median2Index]; if ( median1 == median2 ) { return median1; } if ( median1 < median2 ) { start1 = median1Index; if ( end2 - start2 > 1 ) { end2 = median2Index; } } else { start2 = median2Index; if ( end1 - start1 > 1 ) { end1 = median1Index; } } return main(arr1, start1, end1, arr2, start2, end2); } } class test { public static void main(String[] args) { int[] A = { 1, 2, 3, 4, 4 }; int[] B = { 6, 9, 10, 11, 15}; findMedianTwoArrays findMedian = new findMedianTwoArrays(); System.out.println("Hello Java"); System.out.println(findMedian.main(A, 0, 4, B, 0, 4)); } }
logn
1501.java
0.3
package com.interview.binarysearch; /** * There are two sorted arrays nums1 and nums2 of size m and n respectively. * Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). * * Solution * Take minimum size of two array. Possible number of partitions are from 0 to m in m size array. * Try every cut in binary search way. When you cut first array at i then you cut second array at (m + n + 1)/2 - i * Now try to find the i where a[i-1] <= b[j] and b[j-1] <= a[i]. So this i is partition around which lies the median. * * Time complexity is O(log(min(x,y)) * Space complexity is O(1) * * https://leetcode.com/problems/median-of-two-sorted-arrays/ * https://discuss.leetcode.com/topic/4996/share-my-o-log-min-m-n-solution-with-explanation/4 */ public class MedianOfTwoSortedArrayOfDifferentLength { public double findMedianSortedArrays(int input1[], int input2[]) { //if input1 length is greater than switch them so that input1 is smaller than input2. if (input1.length > input2.length) { return findMedianSortedArrays(input2, input1); } int x = input1.length; int y = input2.length; int low = 0; int high = x; while (low <= high) { int partitionX = (low + high)/2; int partitionY = (x + y + 1)/2 - partitionX; //if partitionX is 0 it means nothing is there on left side. Use -INF for maxLeftX //if partitionX is length of input then there is nothing on right side. Use +INF for minRightX int maxLeftX = (partitionX == 0) ? Integer.MIN_VALUE : input1[partitionX - 1]; int minRightX = (partitionX == x) ? Integer.MAX_VALUE : input1[partitionX]; int maxLeftY = (partitionY == 0) ? Integer.MIN_VALUE : input2[partitionY - 1]; int minRightY = (partitionY == y) ? Integer.MAX_VALUE : input2[partitionY]; if (maxLeftX <= minRightY && maxLeftY <= minRightX) { //We have partitioned array at correct place // Now get max of left elements and min of right elements to get the median in case of even length combined array size // or get max of left for odd length combined array size. if ((x + y) % 2 == 0) { return ((double)Math.max(maxLeftX, maxLeftY) + Math.min(minRightX, minRightY))/2; } else { return (double)Math.max(maxLeftX, maxLeftY); } } else if (maxLeftX > minRightY) { //we are too far on right side for partitionX. Go on left side. high = partitionX - 1; } else { //we are too far on left side for partitionX. Go on right side. low = partitionX + 1; } } //Only we we can come here is if input arrays were not sorted. Throw in that scenario. throw new IllegalArgumentException(); } public static void main(String[] args) { int[] x = {1, 3, 8, 9, 15}; int[] y = {7, 11, 19, 21, 18, 25}; MedianOfTwoSortedArrayOfDifferentLength mm = new MedianOfTwoSortedArrayOfDifferentLength(); mm.findMedianSortedArrays(x, y); } }
logn
1502.java
0.3
package com.interview.binarysearch; /** * http://www.geeksforgeeks.org/search-floor-and-ceil-in-a-sorted-array/ */ public class FloorAndCeilingSortedArray { public int floor(int input[], int x){ int low = 0; int high = input.length-1; while(low <= high){ int middle = (low + high)/2; if(input[middle] == x || (input[middle] < x && (middle == input.length-1 || input[middle+1] > x))){ return middle; }else if(input[middle] < x){ low = middle+1; }else{ high = middle-1; } } return -1; } public int ceiling(int input[], int x){ int low = 0; int high = input.length-1; while(low <= high){ int middle = (low + high)/2; if(input[middle] == x || (input[middle] > x && (middle == 0 || input[middle-1] < x))){ return middle; }else if(input[middle] < x){ low = middle+1; }else{ high = middle-1; } } return -1; } public static void main(String args[]){ int input[] = {1,2,5,6,11,15}; FloorAndCeilingSortedArray foc = new FloorAndCeilingSortedArray(); System.out.println(foc.floor(input, 15)); System.out.println(foc.ceiling(input, 2)); } }
logn
1503.java
0.3
package com.interview.algorithms.array; /** * Given a sorted array and a value x, the ceiling of x is the smallest element * in array greater than or equal to x, and the floor is the greatest element * smaller than or equal to x. Assume than the array is sorted in non-decreasing * order. Write efficient functions to find floor and ceiling of x. * * For example, let the input array be {1, 2, 8, 10, 10, 12, 19} * For x = 0:floor doesn't exist in array, ceil = 1 * For x = 1: floor = 1, ceil = 1 * For x = 5: floor = 2, ceil = 8 * For x = 20: floor= 19, ceil doesn't exist in array * * * @author ajitkoti */ public class FloorAndCeilingInASortedArray { /** * Instead of using linear search, binary search is used here to find out * the index. Binary search reduces time complexity to O(Logn). * * @param arr * @param low * @param high * @param x * @return */ private static int ceilSearch(int arr[], int low, int high, int x) { int mid; /* * If x is smaller than or equal to the first element, then return the * first element */ if (x <= arr[low]) return low; /* If x is greater than the last element, then return -1 */ if (x > arr[high]) return -1; /* get the index of middle element of arr[low..high] */ mid = (low + high) / 2; /* low + (high - low)/2 */ /* If x is same as middle element, then return mid */ if (arr[mid] == x) return mid; /* * If x is greater than arr[mid], then either arr[mid + 1] is ceiling of * x or ceiling lies in arr[mid+1...high] */ else if (x > arr[mid]) { if (mid + 1 <= high && x <= arr[mid + 1]) return mid + 1; else return ceilSearch(arr, mid + 1, high, x); } /* * If x is smaller than arr[mid], then either arr[mid] is ceiling of x * or ceiling lies in arr[mid-1...high] */ else { if (mid - 1 >= low && x > arr[mid - 1]) return mid; else return ceilSearch(arr, low, mid - 1, x); } } /* Driver program to check above functions */ public static void main(String[] args) { int arr[] = { 1, 2, 8, 10, 10, 12, 19 }; int n = arr.length; int x = 11; int index = ceilSearch(arr, 0, n - 1, x); if (index == -1) System.out.println("Ceiling of doesn't exist in array " + x); else System.out.println("ceiling of" + x + " is " + arr[index]); } }
logn
1504.java
0.3
package geek.livingstone.problems.arrays; /** * Full problem at * http://www.geeksforgeeks.org/search-floor-and-ceil-in-a-sorted-array/ * * @author emmanuel * */ public class FloorInSortedArray { public static int floor(int[] A, int val) throws Exception { if (val < A[0]) throw new Exception("All elements are greater than val"); else if (val > A[A.length - 1]) return A[A.length - 1]; return floor(A, 0, A.length - 1, val); } private static int floor(int[] A, int l, int r, int val) throws Exception { if (l > r) { throw new Exception("Not found"); } int mid = (l + r) / 2; if (val == A[mid]) return A[mid]; else if (val < A[mid]) { if (val >= A[mid - 1]) return A[mid - 1]; return floor(A, l, mid - 1, val); } else { if (val < A[mid + 1]) return A[mid]; return floor(A, mid + 1, r, val); } } public static void main(String[] args) throws Exception { int[] A = new int[] { 1, 3, 4, 7, 8, 12, 32, 45, 78, 98 }; // System.out.println(floor(A, -45)); System.out.println(floor(A, 11)); System.out.println(floor(A, 32)); System.out.println(floor(A, 70)); System.out.println(floor(A, 125)); } }
logn
1505.java
0.3
package com.interview.binarysearch; /** * Regular binary search */ public class BinarySearch { public int search(final int input[], int search) { int low = 0; int high = input.length - 1; int mid; while (low <= high) { mid = low + ((high - low) / 2); if (input[mid] == search) { return mid; } else if (input[mid] < search) { low = mid + 1; } else { high = mid - 1; } } return -1; } public static void main(String args[]) { BinarySearch bSearch = new BinarySearch(); final int arr1[] = {1, 2, 4, 5, 7, 8}; System.out.println(bSearch.search(arr1, -1)); System.out.println(bSearch.search(arr1, 1)); System.out.println(bSearch.search(arr1, 8)); System.out.println(bSearch.search(arr1, 2)); } }
logn
1506.java
0.3
package com.interview.binarysearch; /** * http://www.careercup.com/question?id=4877486110277632 * Given a circle with N defined points and a point M outside the circle, * find the point that is closest to M among the set of N. O(LogN) * Test cases * 1) smallest element at center * 2) smallest element at left/right end * 3) largest element at center * 4) smallest element at left side * 5) smallest element at right side */ public class CircularBinarySearch { //if mid is greater than both ends than result is low of two ends else move in direction //where either mid-1 or mid+1 is less public int search(int arr[]) { int low = 0; int high = arr.length - 1; int mid = 0; while (low < high) { mid = (low + high) / 2; //if middle is less than both mid-1 and mid+1 then mid is the answer if((low == mid || arr[mid] < arr[mid-1])&& arr[mid] < arr[mid+1]){ return arr[mid]; } if ((arr[mid] >= arr[low] && arr[mid] >= arr[high])){ if(arr[low] < arr[high]){ high = mid-1; }else{ low = mid+1; } }else{ if(arr[mid-1] < arr[mid+1]){ high = mid-1; }else{ low = mid+1; } } } return arr[low]; } public static void main(String args[]) { CircularBinarySearch cbs = new CircularBinarySearch(); int arr[] = { 7, 10, 8, 5, 2, 3, 5 }; System.out.print(cbs.search(arr)); int arr1[] = { 5, 8, 10, 7, 5, 3, 2 }; System.out.print(cbs.search(arr1)); int arr2[] = { 3, 5, 7, 10, 8, 5, 2 }; System.out.print(cbs.search(arr2)); int arr3[] = { 8, 5, 2, 3, 5, 7, 10 }; System.out.print(cbs.search(arr3)); int arr4[] = { 5, 3, 2, 5, 8, 10, 7 }; System.out.print(cbs.search(arr4)); int arr5[] = {100,20,10,5,2,8,11,16,19}; System.out.print(cbs.search(arr5)); int arr6[] = {200,2,10,15,20,80,110,160,190}; System.out.print(cbs.search(arr6)); int arr7[] = {5,10,20,50,200,800,1100,1600,1900,2}; System.out.print(cbs.search(arr7)); int arr8[] = {2,5,10,20,50,200,800,1100,1600,1900}; System.out.print(cbs.search(arr8)); int arr9[] = {3,1,8,5,4}; System.out.print(cbs.search(arr9)); } }
logn
1507.java
0.3
package com.interview.binarysearch; /** * http://www.geeksforgeeks.org/find-the-point-where-a-function-becomes-negative/ */ public class MonotonicallyIncreasingFunctionBecomesPositive { private int f(int x){ return x*x - 10*x - 20; } public int findPoint(){ int i=1; while(f(i) <=0 ){ i = i*2; } return binarySearch(i/2,i); } private int binarySearch(int start,int end){ int mid = (start+end)/2; while(start < end){ mid = (start+end)/2; if(f(mid) >0 && f(mid-1) <=0){ return mid; } if(f(mid) <=0 && f(mid+1)>0){ return mid+1; } if(f(mid) <= 0){ start = mid+1; }else{ end = mid-1; } } return mid; } public static void main(String args[]){ MonotonicallyIncreasingFunctionBecomesPositive mif = new MonotonicallyIncreasingFunctionBecomesPositive(); System.out.print(mif.findPoint()); } }
logn
1508.java
0.3
package com.interview.binarysearch; /** * @author Tushar Roy * Date 01/22/17 * * Search in sorted and rotated array. In one version duplicate is not allowed and * in another version duplicate is allowed. * * Time complexity with no duplicate - O(logn) * Time complexity with duplicates - O(n) * * https://leetcode.com/problems/search-in-rotated-sorted-array/ * https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ */ public class SortedAndRotatedArraySearch { /** * Duplicates are not allowed in arr. */ public int search(int arr[],int search){ int low =0; int high = arr.length-1; while(low <= high){ int mid = (low + high)/2; if(arr[mid] == search){ return mid; } if(arr[mid] < arr[high]){ if(arr[mid] < search && search <= arr[high]){ low = mid+1; }else{ high = mid-1; } }else{ if(search >= arr[low] && search < arr[mid]){ high = mid-1; }else{ low = mid+1; } } } return -1; } /** * Duplicates are allowed in arr. */ public boolean searchWithDuplicates(int[] arr, int search) { int low =0; int high = arr.length-1; while(low <= high){ int mid = (low + high)/2; if(arr[mid] == search) { return true; } //if low is same as mid then increment low. if (arr[mid] == arr[low]) { low++; } else if (arr[mid] == arr[high]) { //if high is same as mid then decrement high. high--; } else if (arr[mid] < arr[high]) { if(arr[mid] < search && search <= arr[high]) { low = mid + 1; } else { high = mid - 1; } } else { if(search >= arr[low] && search < arr[mid]) { high = mid - 1; } else { low = mid + 1; } } } return false; } public static void main(String args[]){ SortedAndRotatedArraySearch ras = new SortedAndRotatedArraySearch(); int arr1[] = {1,2,5,6,7,8,11,21}; System.out.print(ras.search(arr1, 1)); System.out.print(ras.search(arr1, 5)); System.out.print(ras.search(arr1, 22)); System.out.println(); int arr2[] = {18,21,1,2,5,6,7,8,10,15}; System.out.print(ras.search(arr2, 1)); System.out.print(ras.search(arr2, 5)); System.out.print(ras.search(arr2, 10)); System.out.print(ras.search(arr2, 14)); System.out.println(); int arr3[] = {7,8,15,17,18,21,1,2,5,6}; System.out.print(ras.search(arr3, 1)); System.out.print(ras.search(arr3, 5)); System.out.print(ras.search(arr3, 10)); System.out.print(ras.search(arr3, 7)); System.out.print(ras.search(arr3, 6)); System.out.print(ras.search(arr3, 16)); } }
logn
1509.java
0.3
package com.interview.binarysearch; /** * Date 07/31/2016 * @author Tushar Roy * * Given a sorted array of integers, find the starting and ending position of a given target value. * * Time complexity O(logn) * Space complexity O(1) * * https://leetcode.com/problems/search-for-a-range/ */ public class SearchForRange { public int[] searchRange(int[] nums, int target) { int first = firstOccurence(nums, target); if (first == -1) { return new int[]{-1, -1}; } int last = lastOccurence(nums, target); return new int[]{first, last}; } private int firstOccurence(int[] nums, int target) { int low = 0; int high = nums.length - 1; while (low <= high) { int mid = low + (high - low)/2; if (nums[mid] == target && (mid == 0 || nums[mid - 1] < target)) { return mid; } else if (nums[mid] >= target) { high = mid - 1; } else { low = mid + 1; } } return -1; } private int lastOccurence(int[] nums, int target) { int low = 0; int high = nums.length - 1; while (low <= high) { int mid = low + (high - low)/2; if (nums[mid] == target && (mid == nums.length - 1 || nums[mid + 1] > target)) { return mid; } else if (nums[mid] <= target) { low = mid + 1; } else { high = mid - 1; } } return -1; } public static void main(String args[]) { SearchForRange searchForRange = new SearchForRange(); int[] nums = {0, 1, 1, 3, 6, 9, 11}; int[] r = searchForRange.searchRange(nums, 11); System.out.println(r[0] + " " + r[1]); r = searchForRange.searchRange(nums, 0); System.out.println(r[0] + " " + r[1]); } }
logn
1510.java
0.3
package com.interview.binarysearch; /** * @author Tushar Roy * Date 01/17/2107 * A peak element is an element that is greater than its neighbors. Find index of peak element in the array. * * Space complexity is O(1) * Time complexity is O(n) * * https://leetcode.com/problems/find-peak-element/ */ public class PeakElement { public int findPeakElement(int[] nums) { int low = 0; int high = nums.length - 1; int middle = 0; while (low <= high) { middle = (low + high)/2; int before = Integer.MIN_VALUE; if (middle > 0) { before = nums[middle - 1]; } int after = Integer.MIN_VALUE; if (middle < nums.length - 1) { after = nums[middle + 1]; } if (nums[middle] > before && nums[middle] > after) { return middle; } else if (before > after) { high = middle - 1; } else { low = middle + 1; } } return middle; } public static void main(String args[]){ int arr[] = {10,5,15,2,23,90,67}; PeakElement pe = new PeakElement(); System.out.println(pe.findPeakElement(arr)); int arr1[] = {10,20,30,40,50}; System.out.println(pe.findPeakElement(arr1)); int arr2[] = {100,90,80,70,60}; System.out.println(pe.findPeakElement(arr2)); } }
logn
1511.java
0.3
package com.interview.binarysearch; /** * http://www.geeksforgeeks.org/check-for-majority-element-in-a-sorted-array/ */ public class FirstOccurrenceOfNumberInSortedArray { public int firstOccurrence(int input[], int x){ int low = 0; int high = input.length-1; while(low <= high){ int middle = (low + high)/2; if(input[middle] == x && (middle == 0 || input[middle-1] < x)){ return middle; }else if(input[middle] < x){ low = middle+1; }else{ high = middle-1; } } return -1; } public static void main(String args[]){ FirstOccurrenceOfNumberInSortedArray fos = new FirstOccurrenceOfNumberInSortedArray(); int input[] = {1,2,2,2,2,2,5,7,7}; System.out.println(fos.firstOccurrence(input, 6)); } }
logn
1512.java
0.3
/** * Given a binary array sorted in non-increasing order, count the number of 1’s in it. **/ /* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static int countOnes(int arr[], int low, int high) { if(high >= low) { // get the middle index int mid = low + (high - low)/2; // check if the element at middle index is last 1 if ( (mid == high || arr[mid+1] == 0) && (arr[mid] == 1)) return mid+1; // If element is not last 1, recur for right side if (arr[mid] == 1) return countOnes(arr, (mid + 1), high); // else recur for left side return countOnes(arr, low, (mid -1)); } return 0; } public static void main (String[] args) throws java.lang.Exception { int arr[] = {1, 1, 1, 1, 0, 0, 0}; int n = arr.length; System.out.println("Count of 1's in given array is " + countOnes(arr, 0, n-1)); }
logn
1513.java
0.3
// http://www.geeksforgeeks.org/count-1s-sorted-binary-array/ class CountOne { public static void main(String[] args) { int[] arr={0, 0, 0, 0, 0, 0, 0}; //int[] arr={1, 1, 1, 1, 1, 1, 1}; //int[] arr = {1, 1, 1, 0, 0, 0, 0}; countOne(arr); } public static void countOne(int[] a) { int l = 0; int h = a.length-1; int mid = -1; while(l<=h) { mid = (l+h)/2; if( (mid == h || a[mid+1] == 0) && a[mid] == 1) { System.out.println("Count " + (mid + 1)); return; } if( a[mid] < 1) { h = mid - 1; } else { l = mid + 1; } } System.out.println(" No one's found "); return; } }
logn
1514.java
0.3
public class Search { public static boolean find2(int[] array, int begin, int end, int element) { if (begin <= end) { int medium = begin + (end - begin) / 2; if (array[medium] == element) return true; else if (medium > begin && array[medium - 1] == element) return true; else if (medium < end && array[medium + 1] == element) return true; if (array[medium] > element) return find2(array, 0, medium - 2, element); return find2(array, medium + 2, end, element); } return false; } public static int find(int[] array, int begin, int end, int element) { if (begin < end) { int medium = begin + (end - begin) / 2; if (array[medium] == element) return medium; else if (medium > begin && array[medium - 1] == element) return medium - 1; else if (medium < end && array[medium + 1] == element) return medium + 1; if (array[medium] > element) return find(array, 0, medium - 2, element); return find(array, medium + 2, end, element); } return -1; } public static void main(String[] args) { int[] array = {12,13, 21, 36, 3, 43, 65, 76, 88, 91, 100}; System.out.println(find2(array, 0, array.length-1, 13)); } }
logn
1515.java
0.3
// Java program to reverse the number // using a stack import java.util.Stack; public class GFG { // Stack to maintain order of digits static Stack<Integer> st= new Stack<>(); // Function to push digits into stack static void push_digits( int number) { while (number != 0 ) { st.push(number % 10 ); number = number / 10 ; } } // Function to reverse the number static int reverse_number( int number) { // Function call to push number's // digits to stack push_digits(number); int reverse = 0 ; int i = 1 ; // Popping the digits and forming // the reversed number while (!st.isEmpty()) { reverse = reverse + (st.peek() * i); st.pop(); i = i * 10 ; } // Return the reversed number formed return reverse; } // Driver program to test above function public static void main(String[] args) { int number = 39997 ; System.out.println(reverse_number(number)); } } // This code is contributed by Sumit Ghosh
logn
218.java
0.3
// Java program to find number of // rotations in a sorted and rotated // array. import java.util.*; import java.lang.*; import java.io.*; class BinarySearch { // Returns count of rotations for an array // which is first sorted in ascending order, // then rotated static int countRotations( int arr[], int low, int high) { // This condition is needed to handle // the case when array is not rotated // at all if (high < low) return 0 ; // If there is only one element left if (high == low) return low; // Find mid // /*(low + high)/2;*/ int mid = low + (high - low)/ 2 ; // Check if element (mid+1) is minimum // element. Consider the cases like // {3, 4, 5, 1, 2} if (mid < high && arr[mid+ 1 ] < arr[mid]) return (mid + 1 ); // Check if mid itself is minimum element if (mid > low && arr[mid] < arr[mid - 1 ]) return mid; // Decide whether we need to go to left // half or right half if (arr[high] > arr[mid]) return countRotations(arr, low, mid - 1 ); return countRotations(arr, mid + 1 , high); } // Driver program to test above functions public static void main (String[] args) { int arr[] = { 15 , 18 , 2 , 3 , 6 , 12 }; int n = arr.length; System.out.println(countRotations(arr, 0 , n- 1 )); } } // This code is contributed by Chhavi
logn
22.java
0.3
// Java program to find minimum element in a sorted and rotated array import java.util.*; import java.lang.*; import java.io.*; class Minimum { static int findMin( int arr[], int low, int high) { // This condition is needed to handle the case when array // is not rotated at all if (high < low) return arr[ 0 ]; // If there is only one element left if (high == low) return arr[low]; // Find mid int mid = low + (high - low)/ 2 ; /*(low + high)/2;*/ // Check if element (mid+1) is minimum element. Consider // the cases like {3, 4, 5, 1, 2} if (mid < high && arr[mid+ 1 ] < arr[mid]) return arr[mid+ 1 ]; // Check if mid itself is minimum element if (mid > low && arr[mid] < arr[mid - 1 ]) return arr[mid]; // Decide whether we need to go to left half or right half if (arr[high] > arr[mid]) return findMin(arr, low, mid- 1 ); return findMin(arr, mid+ 1 , high); } // Driver Program public static void main (String[] args) { int arr1[] = { 5 , 6 , 1 , 2 , 3 , 4 }; int n1 = arr1.length; System.out.println( "The minimum element is " + findMin(arr1, 0 , n1- 1 )); int arr2[] = { 1 , 2 , 3 , 4 }; int n2 = arr2.length; System.out.println( "The minimum element is " + findMin(arr2, 0 , n2- 1 )); int arr3[] = { 1 }; int n3 = arr3.length; System.out.println( "The minimum element is " + findMin(arr3, 0 , n3- 1 )); int arr4[] = { 1 , 2 }; int n4 = arr4.length; System.out.println( "The minimum element is " + findMin(arr4, 0 , n4- 1 )); int arr5[] = { 2 , 1 }; int n5 = arr5.length; System.out.println( "The minimum element is " + findMin(arr5, 0 , n5- 1 )); int arr6[] = { 5 , 6 , 7 , 1 , 2 , 3 , 4 }; int n6 = arr6.length; System.out.println( "The minimum element is " + findMin(arr6, 0 , n6- 1 )); int arr7[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; int n7 = arr7.length; System.out.println( "The minimum element is " + findMin(arr7, 0 , n7- 1 )); int arr8[] = { 2 , 3 , 4 , 5 , 6 , 7 , 8 , 1 }; int n8 = arr8.length; System.out.println( "The minimum element is " + findMin(arr8, 0 , n8- 1 )); int arr9[] = { 3 , 4 , 5 , 1 , 2 }; int n9 = arr9.length; System.out.println( "The minimum element is " + findMin(arr9, 0 , n9- 1 )); } }
logn
5.java
0.3
// Java implementation of recursive Binary Search class BinarySearch { // Returns index of x if it is present in arr[l.. // r], else return -1 int binarySearch( int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2 ; // If the element is present at the // middle itself if (arr[mid] == x) return mid; // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1 , x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 1 , r, x); } // We reach here when element is not present // in array return - 1 ; } // Driver method to test above public static void main(String args[]) { BinarySearch ob = new BinarySearch(); int arr[] = { 2 , 3 , 4 , 10 , 40 }; int n = arr.length; int x = 10 ; int result = ob.binarySearch(arr, 0 , n - 1 , x); if (result == - 1 ) System.out.println( "Element not present" ); else System.out.println( "Element found at index " + result); } } /* This code is contributed by Rajat Mishra */
logn
512.java
0.3
// Java implementation of iterative Binary Search class BinarySearch { // Returns index of x if it is present in arr[], // else return -1 int binarySearch( int arr[], int x) { int l = 0 , r = arr.length - 1 ; while (l <= r) { int m = l + (r - l) / 2 ; // Check if x is present at mid if (arr[m] == x) return m; // If x greater, ignore left half if (arr[m] < x) l = m + 1 ; // If x is smaller, ignore right half else r = m - 1 ; } // if we reach here, then element was // not present return - 1 ; } // Driver method to test above public static void main(String args[]) { BinarySearch ob = new BinarySearch(); int arr[] = { 2 , 3 , 4 , 10 , 40 }; int n = arr.length; int x = 10 ; int result = ob.binarySearch(arr, x); if (result == - 1 ) System.out.println( "Element not present" ); else System.out.println( "Element found at " + "index " + result); } }
logn
513.java
0.3
// Java program for Binary Search import java.util.*; class Binary { public static int f( int x) { return (x*x - 10 *x - 20 ); } // Returns the value x where above // function f() becomes positive // first time. public static int findFirstPositive() { // When first value itself is positive if (f( 0 ) > 0 ) return 0 ; // Find 'high' for binary search // by repeated doubling int i = 1 ; while (f(i) <= 0 ) i = i * 2 ; // Call binary search return binarySearch(i / 2 , i); } // Searches first positive value of // f(i) where low <= i <= high public static int binarySearch( int low, int high) { if (high >= low) { /* mid = (low + high)/2 */ int mid = low + (high - low)/ 2 ; // If f(mid) is greater than 0 and // one of the following two // conditions is true: // a) mid is equal to low // b) f(mid-1) is negative if (f(mid) > 0 && (mid == low || f(mid- 1 ) <= 0 )) return mid; // If f(mid) is smaller than or equal to 0 if (f(mid) <= 0 ) return binarySearch((mid + 1 ), high); else // f(mid) > 0 return binarySearch(low, (mid - 1 )); } /* Return -1 if there is no positive value in given range */ return - 1 ; } // driver code public static void main(String[] args) { System.out.print ( "The value n where f() " + "becomes positive first is " + findFirstPositive()); } } // This code is contributed by rishabh_jain
logn
516.java
0.3
/* Java program to search an element in a sorted and pivoted array*/ class Main { /* Searches an element key in a pivoted sorted array arrp[] of size n */ static int pivotedBinarySearch( int arr[], int n, int key) { int pivot = findPivot(arr, 0 , n- 1 ); // If we didn't find a pivot, then // array is not rotated at all if (pivot == - 1 ) return binarySearch(arr, 0 , n- 1 , key); // If we found a pivot, then first // compare with pivot and then // search in two subarrays around pivot if (arr[pivot] == key) return pivot; if (arr[ 0 ] <= key) return binarySearch(arr, 0 , pivot- 1 , key); return binarySearch(arr, pivot+ 1 , n- 1 , key); } /* Function to get pivot. For array 3, 4, 5, 6, 1, 2 it returns 3 (index of 6) */ static int findPivot( int arr[], int low, int high) { // base cases if (high < low) return - 1 ; if (high == low) return low; /* low + (high - low)/2; */ int mid = (low + high)/ 2 ; if (mid < high && arr[mid] > arr[mid + 1 ]) return mid; if (mid > low && arr[mid] < arr[mid - 1 ]) return (mid- 1 ); if (arr[low] >= arr[mid]) return findPivot(arr, low, mid- 1 ); return findPivot(arr, mid + 1 , high); } /* Standard Binary Search function */ static int binarySearch( int arr[], int low, int high, int key) { if (high < low) return - 1 ; /* low + (high - low)/2; */ int mid = (low + high)/ 2 ; if (key == arr[mid]) return mid; if (key > arr[mid]) return binarySearch(arr, (mid + 1 ), high, key); return binarySearch(arr, low, (mid - 1 ), key); } // main function public static void main(String args[]) { // Let us search 3 in below array int arr1[] = { 5 , 6 , 7 , 8 , 9 , 10 , 1 , 2 , 3 }; int n = arr1.length; int key = 3 ; System.out.println( "Index of the element is : " + pivotedBinarySearch(arr1, n, key)); } }
logn
519.java
0.3
// A Java program to divide and conquer based // efficient solution to find // median of two sorted arrays // of same size. import java.util.*; class GfG { /* This function returns median of ar1[] and ar2[]. Assumptions in this function: Both ar1[] and ar2[] are sorted arrays Both have n elements */ static int getMedian( int ar1[], int ar2[], int n) { /* return -1 for invalid input */ if (n <= 0 ) return - 1 ; if (n == 1 ) return (ar1[ 0 ] + ar2[ 0 ]) / 2 ; if (n == 2 ) return (Math.max(ar1[ 0 ], ar2[ 0 ]) + Math.min(ar1[ 1 ], ar2[ 1 ])) / 2 ; /* get the median of the first array */ int m1 = median(ar1, n); /* get the median of the second array */ int m2 = median(ar2, n); /* If medians are equal then return either m1 or m2 */ if (m1 == m2) return m1; /* if m1 < m2 then median must exist in ar1[m1....] and ar2[....m2] */ if (m1 < m2) { if (n % 2 == 0 ) return getMedian(ar1 + n / 2 - 1 , ar2, n - n / 2 + 1 ); return getMedian(ar1 + n / 2 , ar2, n - n / 2 ); } /* if m1 > m2 then median must exist in ar1[....m1] and ar2[m2...] */ if (n % 2 == 0 ) return getMedian(ar2 + n / 2 - 1 , ar1, n - n / 2 + 1 ); return getMedian(ar2 + n / 2 , ar1, n - n / 2 ); } /* Function to get median of a sorted array */ static int median( int arr[], int n) { if (n % 2 == 0 ) return (arr[n / 2 ] + arr[n / 2 - 1 ]) / 2 ; else return arr[n / 2 ]; } // Driver code public static void main(String[] args) { int ar1[] = { 1 , 2 , 3 , 6 }; int ar2[] = { 4 , 6 , 8 , 10 }; int n1 = ar1.length; int n2 = ar2.length; if (n1 == n2) System.out.println( "Median is " + getMedian(ar1, ar2, n1)); else System.out.println( "Doesn't work for arrays " + "of unequal size" ); } }
logn
521.java
0.3
class Main { /* Function to get index of ceiling of x in arr[low..high]*/ static int ceilSearch( int arr[], int low, int high, int x) { int mid; /* If x is smaller than or equal to the first element, then return the first element */ if (x <= arr[low]) return low; /* If x is greater than the last element, then return -1 */ if (x > arr[high]) return - 1 ; /* get the index of middle element of arr[low..high]*/ mid = (low + high)/ 2 ; /* low + (high - low)/2 */ /* If x is same as middle element, then return mid */ if (arr[mid] == x) return mid; /* If x is greater than arr[mid], then either arr[mid + 1] is ceiling of x or ceiling lies in arr[mid+1...high] */ else if (arr[mid] < x) { if (mid + 1 <= high && x <= arr[mid+ 1 ]) return mid + 1 ; else return ceilSearch(arr, mid+ 1 , high, x); } /* If x is smaller than arr[mid], then either arr[mid] is ceiling of x or ceiling lies in arr[mid-1...high] */ else { if (mid - 1 >= low && x > arr[mid- 1 ]) return mid; else return ceilSearch(arr, low, mid - 1 , x); } } /* Driver program to check above functions */ public static void main (String[] args) { int arr[] = { 1 , 2 , 8 , 10 , 10 , 12 , 19 }; int n = arr.length; int x = 8 ; int index = ceilSearch(arr, 0 , n- 1 , x); if (index == - 1 ) System.out.println( "Ceiling of " +x+ " doesn't exist in array" ); else System.out.println( "ceiling of " +x+ " is " +arr[index]); } }
logn
527.java
0.3
// Java program to count // occurrences of an element class GFG { // A recursive binary search // function. It returns location // of x in given array arr[l..r] // is present, otherwise -1 static int binarySearch( int arr[], int l, int r, int x) { if (r < l) return - 1 ; int mid = l + (r - l) / 2 ; // If the element is present // at the middle itself if (arr[mid] == x) return mid; // If element is smaller than // mid, then it can only be // present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 1 , x); // Else the element can // only be present in // right subarray return binarySearch(arr, mid + 1 , r, x); } // Returns number of times x // occurs in arr[0..n-1] static int countOccurrences( int arr[], int n, int x) { int ind = binarySearch(arr, 0 , n - 1 , x); // If element is not present if (ind == - 1 ) return 0 ; // Count elements on left side. int count = 1 ; int left = ind - 1 ; while (left >= 0 && arr[left] == x) { count++; left--; } // Count elements // on right side. int right = ind + 1 ; while (right < n && arr[right] == x) { count++; right++; } return count; } // Driver code public static void main(String[] args) { int arr[] = { 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 }; int n = arr.length; int x = 2 ; System.out.print(countOccurrences(arr, n, x)); } } // This code is contributed // by ChitraNayal
logn
529.java
0.3
// Java program to check fixed point // in an array using binary search class Main { static int binarySearch( int arr[], int low, int high) { if (high >= low) { /* low + (high - low)/2; */ int mid = (low + high)/ 2 ; if (mid == arr[mid]) return mid; if (mid > arr[mid]) return binarySearch(arr, (mid + 1 ), high); else return binarySearch(arr, low, (mid - 1 )); } /* Return -1 if there is no Fixed Point */ return - 1 ; } //main function public static void main(String args[]) { int arr[] = {- 10 , - 1 , 0 , 3 , 10 , 11 , 30 , 50 , 100 }; int n = arr.length; System.out.println( "Fixed Point is " + binarySearch(arr, 0 , n- 1 )); } }
logn
534.java
0.3
// java program to find maximum // element class Main { // function to find the // maximum element static int findMaximum( int arr[], int low, int high) { /* Base Case: Only one element is present in arr[low..high]*/ if (low == high) return arr[low]; /* If there are two elements and first is greater then the first element is maximum */ if ((high == low + 1 ) && arr[low] >= arr[high]) return arr[low]; /* If there are two elements and second is greater then the second element is maximum */ if ((high == low + 1 ) && arr[low] < arr[high]) return arr[high]; /*low + (high - low)/2;*/ int mid = (low + high)/ 2 ; /* If we reach a point where arr[mid] is greater than both of its adjacent elements arr[mid-1] and arr[mid+1], then arr[mid] is the maximum element*/ if ( arr[mid] > arr[mid + 1 ] && arr[mid] > arr[mid - 1 ]) return arr[mid]; /* If arr[mid] is greater than the next element and smaller than the previous element then maximum lies on left side of mid */ if (arr[mid] > arr[mid + 1 ] && arr[mid] < arr[mid - 1 ]) return findMaximum(arr, low, mid- 1 ); else return findMaximum(arr, mid + 1 , high); } // main function public static void main (String[] args) { int arr[] = { 1 , 3 , 50 , 10 , 9 , 7 , 6 }; int n = arr.length; System.out.println( "The maximum element is " + findMaximum(arr, 0 , n- 1 )); } }
logn
536.java
0.3
// A Java program to find a peak element element using divide and conquer import java.util.*; import java.lang.*; import java.io.*; class PeakElement { // A binary search based function that returns index of a peak // element static int findPeakUtil( int arr[], int low, int high, int n) { // Find index of middle element int mid = low + (high - low)/ 2 ; /* (low + high)/2 */ // Compare middle element with its neighbours (if neighbours // exist) if ((mid == 0 || arr[mid- 1 ] <= arr[mid]) && (mid == n- 1 || arr[mid+ 1 ] <= arr[mid])) return mid; // If middle element is not peak and its left neighbor is // greater than it,then left half must have a peak element else if (mid > 0 && arr[mid- 1 ] > arr[mid]) return findPeakUtil(arr, low, (mid - 1 ), n); // If middle element is not peak and its right neighbor // is greater than it, then right half must have a peak // element else return findPeakUtil(arr, (mid + 1 ), high, n); } // A wrapper over recursive function findPeakUtil() static int findPeak( int arr[], int n) { return findPeakUtil(arr, 0 , n- 1 , n); } // Driver method public static void main (String[] args) { int arr[] = { 1 , 3 , 20 , 4 , 1 , 0 }; int n = arr.length; System.out.println( "Index of a peak point is " + findPeak(arr, n)); } }
logn
538.java
0.3
// Java program to find minimum element in a sorted and rotated array import java.util.*; import java.lang.*; import java.io.*; class Minimum { static int findMin( int arr[], int low, int high) { // This condition is needed to handle the case when array // is not rotated at all if (high < low) return arr[ 0 ]; // If there is only one element left if (high == low) return arr[low]; // Find mid int mid = low + (high - low)/ 2 ; /*(low + high)/2;*/ // Check if element (mid+1) is minimum element. Consider // the cases like {3, 4, 5, 1, 2} if (mid < high && arr[mid+ 1 ] < arr[mid]) return arr[mid+ 1 ]; // Check if mid itself is minimum element if (mid > low && arr[mid] < arr[mid - 1 ]) return arr[mid]; // Decide whether we need to go to left half or right half if (arr[high] > arr[mid]) return findMin(arr, low, mid- 1 ); return findMin(arr, mid+ 1 , high); } // Driver Program public static void main (String[] args) { int arr1[] = { 5 , 6 , 1 , 2 , 3 , 4 }; int n1 = arr1.length; System.out.println( "The minimum element is " + findMin(arr1, 0 , n1- 1 )); int arr2[] = { 1 , 2 , 3 , 4 }; int n2 = arr2.length; System.out.println( "The minimum element is " + findMin(arr2, 0 , n2- 1 )); int arr3[] = { 1 }; int n3 = arr3.length; System.out.println( "The minimum element is " + findMin(arr3, 0 , n3- 1 )); int arr4[] = { 1 , 2 }; int n4 = arr4.length; System.out.println( "The minimum element is " + findMin(arr4, 0 , n4- 1 )); int arr5[] = { 2 , 1 }; int n5 = arr5.length; System.out.println( "The minimum element is " + findMin(arr5, 0 , n5- 1 )); int arr6[] = { 5 , 6 , 7 , 1 , 2 , 3 , 4 }; int n6 = arr6.length; System.out.println( "The minimum element is " + findMin(arr6, 0 , n6- 1 )); int arr7[] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; int n7 = arr7.length; System.out.println( "The minimum element is " + findMin(arr7, 0 , n7- 1 )); int arr8[] = { 2 , 3 , 4 , 5 , 6 , 7 , 8 , 1 }; int n8 = arr8.length; System.out.println( "The minimum element is " + findMin(arr8, 0 , n8- 1 )); int arr9[] = { 3 , 4 , 5 , 1 , 2 }; int n9 = arr9.length; System.out.println( "The minimum element is " + findMin(arr9, 0 , n9- 1 )); } }
logn
539.java
0.3
// Java program to find an element // in an almost sorted array class GFG { // A recursive binary search based function. // It returns index of x in given array // arr[l..r] is present, otherwise -1 int binarySearch( int arr[], int l, int r, int x) { if (r >= l) { int mid = l + (r - l) / 2 ; // If the element is present at // one of the middle 3 positions if (arr[mid] == x) return mid; if (mid > l && arr[mid - 1 ] == x) return (mid - 1 ); if (mid < r && arr[mid + 1 ] == x) return (mid + 1 ); // If element is smaller than mid, then // it can only be present in left subarray if (arr[mid] > x) return binarySearch(arr, l, mid - 2 , x); // Else the element can only be present // in right subarray return binarySearch(arr, mid + 2 , r, x); } // We reach here when element is // not present in array return - 1 ; } // Driver code public static void main(String args[]) { GFG ob = new GFG(); int arr[] = { 3 , 2 , 10 , 4 , 40 }; int n = arr.length; int x = 4 ; int result = ob.binarySearch(arr, 0 , n - 1 , x); if (result == - 1 ) System.out.println( "Element is not present in array" ); else System.out.println( "Element is present at index " + result); } } // This code is contributed by Rajat Mishra
logn
540.java
0.3
// Java program to count 1's in a sorted array class CountOnes { /* Returns counts of 1's in arr[low..high]. The array is assumed to be sorted in non-increasing order */ int countOnes( int arr[], int low, int high) { if (high >= low) { // get the middle index int mid = low + (high - low)/ 2 ; // check if the element at middle index is last 1 if ( (mid == high || arr[mid+ 1 ] == 0 ) && (arr[mid] == 1 )) return mid+ 1 ; // If element is not last 1, recur for right side if (arr[mid] == 1 ) return countOnes(arr, (mid + 1 ), high); // else recur for left side return countOnes(arr, low, (mid - 1 )); } return 0 ; } /* Driver program to test above functions */ public static void main(String args[]) { CountOnes ob = new CountOnes(); int arr[] = { 1 , 1 , 1 , 1 , 0 , 0 , 0 }; int n = arr.length; System.out.println( "Count of 1's in given array is " + ob.countOnes(arr, 0 , n- 1 ) ); } } /* This code is contributed by Rajat Mishra */
logn
543.java
0.3
class SmallestMissing { int findFirstMissing( int array[], int start, int end) { if (start > end) return end + 1 ; if (start != array[start]) return start; int mid = (start + end) / 2 ; // Left half has all elements from 0 to mid if (array[mid] == mid) return findFirstMissing(array, mid+ 1 , end); return findFirstMissing(array, start, mid); } // Driver program to test the above function public static void main(String[] args) { SmallestMissing small = new SmallestMissing(); int arr[] = { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 10 }; int n = arr.length; System.out.println( "First Missing element is : " + small.findFirstMissing(arr, 0 , n - 1 )); } }
logn
56.java
0.3
import java.io.*; import java.util.Arrays; import java.util.Comparator; import java.util.StringTokenizer; import java.util.stream.IntStream; public class B { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); Solver solver = new Solver(); solver.solve(in, out); out.close(); } static class Solver { int n; int n2; InputReader in; PrintWriter out; public void solve(InputReader in, PrintWriter out) { this.in = in; this.out = out; n = in.readInt(); n2 = n/2; int res = find(); out.print("! "); out.println(res); } public int find() { if (n%4 != 0) return -1; int c = compare(0); if (c == 0) return 1; int s = 1; int f = n2-1; if (c > 0) { s = n2+1; f = n-1; } while (s <= f) { int m = (s+f)/2; int v = compare(m); if (v == 0) return m+1; else if (v < 0) s = m+1; else f = m-1; } return -1; } public int compare(int z) { out.print("? "); out.println(z+1); out.flush(); int r1 = in.readInt(); out.print("? "); out.println((z+n2)%n+1); out.flush(); int r2 = in.readInt(); return r1-r2; } } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { this.reader = new BufferedReader(new InputStreamReader(stream)); } public String read() { try { if (tokenizer == null || !tokenizer.hasMoreTokens()) { tokenizer = new StringTokenizer(reader.readLine()); } } catch (IOException ex) { throw new RuntimeException(ex); } return tokenizer.nextToken(); } public int readInt() { return Integer.parseInt(read()); } public long readLong() { return Long.parseLong(read()); } public void readIntArrays(int[]... arrays) { for (int i = 0; i < arrays[0].length; i++) { for (int j = 0; j < arrays.length; j++) { arrays[j][i] = readInt(); } } } } }
logn
715.java
0.3
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; /** * @author Don Li */ public class OlyaAndMagicalSquare { void solve() { long[] dp = new long[32]; dp[0] = 0; for (int i = 1; i < 32; i++) { dp[i] = 4 * dp[i - 1] + 1; } int T = in.nextInt(); L: while (T-- > 0) { int n = in.nextInt(); long k = in.nextLong(); if (n > 31) { out.println("YES " + (n - 1)); continue; } long tot = 0; for (int a = n - 1; a >= 0; a--) { k -= (1L << (n - a)) - 1; if (k < 0) break; if (k == 0) { out.println("YES " + a); continue L; } long limit = (1L << (n + 1 - a)) - 3; if (k <= tot || dp[a] > 0 && (k - tot + dp[a] - 1) / dp[a] <= limit) { out.println("YES " + a); continue L; } tot += dp[a] * limit; } out.println("NO"); } } public static void main(String[] args) { in = new FastScanner(new BufferedReader(new InputStreamReader(System.in))); out = new PrintWriter(System.out); new OlyaAndMagicalSquare().solve(); out.close(); } static FastScanner in; static PrintWriter out; static class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(nextToken()); } public long nextLong() { return Long.parseLong(nextToken()); } public double nextDouble() { return Double.parseDouble(nextToken()); } } }
logn
783.java
0.3
import java.util.*; public class ehab4 { public static void main( String[] args ) { Scanner in = new Scanner( System.in ); int a = 0, b = 0; System.out.println( "? 0 0 " ); System.out.flush(); int c = in.nextInt(); for ( int i = 29; i >= 0; i-- ) { System.out.println( "? " + ( a + ( 1 << i ) ) + " " + b ); System.out.flush(); int q1 = in.nextInt(); System.out.println( "? " + a + " " + ( b + ( 1 << i ) ) ); System.out.flush(); int q2 = in.nextInt(); if ( q1 == q2 ) { if ( c == 1 ) a += ( 1 << i ); else if ( c == -1 ) b += ( 1 << i ); c = q1; } else if ( q1 == -1 ) { a += ( 1 << i ); b += ( 1 << i ); } else if ( q1 == -2 ) return; } System.out.println( "! " + a + " " + b ); System.out.flush(); } }
logn
794.java
0.3
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.concurrent.atomic.AtomicReferenceArray; public class NastyaWardrobe { static long modulo = 1000000007; static long ans = 0; public static void main(String[] args) throws IOException { BufferedReader inp = new BufferedReader(new InputStreamReader(System.in)); String[] s1 = inp.readLine().split(" "); long clothes = Long.parseLong(s1[0]); long months = Long.parseLong(s1[1]); //formula 2^k(2x-1)+1; calc(clothes,months); System.out.print(ans); } static void calc(long clothes,long months){ if(clothes!=0) { long a; long count = 0; ArrayList<Long> list = new ArrayList<>(); if (months >= 2) { a = 2; long c = months; while (c > 1) { if (c % 2 == 1) { count++; list.add(a); } c = c / 2; a = (a * a) % modulo; } while (count > 0) { long b = list.get(0); list.remove(0); a = (a * b) % modulo; count--; } } else { a = (long) Math.pow(2, months); } long b = clothes; //System.out.println(b); b = (2 * b - 1) % modulo; ans = (a * b) % modulo; ans = (ans + 1) % modulo; }else{ ans = 0; } } }
logn
927.java
0.3
// Java program to count occurrences // of an element class Main { /* if x is present in arr[] then returns the count of occurrences of x, otherwise returns -1. */ static int count( int arr[], int x, int n) { // index of first occurrence of x in arr[0..n-1] int i; // index of last occurrence of x in arr[0..n-1] int j; /* get the index of first occurrence of x */ i = first(arr, 0 , n- 1 , x, n); /* If x doesn't exist in arr[] then return -1 */ if (i == - 1 ) return i; /* Else get the index of last occurrence of x. Note that we are only looking in the subarray after first occurrence */ j = last(arr, i, n- 1 , x, n); /* return count */ return j-i+ 1 ; } /* if x is present in arr[] then returns the index of FIRST occurrence of x in arr[0..n-1], otherwise returns -1 */ static int first( int arr[], int low, int high, int x, int n) { if (high >= low) { /*low + (high - low)/2;*/ int mid = (low + high)/ 2 ; if ( ( mid == 0 || x > arr[mid- 1 ]) && arr[mid] == x) return mid; else if (x > arr[mid]) return first(arr, (mid + 1 ), high, x, n); else return first(arr, low, (mid - 1 ), x, n); } return - 1 ; } /* if x is present in arr[] then returns the index of LAST occurrence of x in arr[0..n-1], otherwise returns -1 */ static int last( int arr[], int low, int high, int x, int n) { if (high >= low) { /*low + (high - low)/2;*/ int mid = (low + high)/ 2 ; if ( ( mid == n- 1 || x < arr[mid+ 1 ]) && arr[mid] == x ) return mid; else if (x < arr[mid]) return last(arr, low, (mid - 1 ), x, n); else return last(arr, (mid + 1 ), high, x, n); } return - 1 ; } public static void main(String args[]) { int arr[] = { 1 , 2 , 2 , 3 , 3 , 3 , 3 }; // Element to be counted in arr[] int x = 3 ; int n = arr.length; int c = count(arr, x, n); System.out.println(x+ " occurs " +c+ " times" ); } }
logn
530.java
0.3
import java.util.*; import java.io.*; public class code{ public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); int ok,ok2; int va,vb; va = 0; vb = 0; out.println("? "+va+" "+vb); out.flush(); ok = sc.nextInt(); for(int i=29;i>=0;i--){ if(ok==0){ va += (1<<i); out.println("? "+va+" "+vb); out.flush(); ok2 = sc.nextInt(); if(ok2==1){ va -= (1<<i); }else{ vb += (1<<i); } }else{ va += (1<<i); vb += (1<<i); out.println("? "+va+" "+vb); out.flush(); ok2 = sc.nextInt(); if(ok==ok2){ vb -= (1<<i); out.println("? "+va+" "+vb); out.flush(); ok2 = sc.nextInt(); if(ok2==1){ va -= (1<<i); }else{ vb += (1<<i); } }else{ if(ok==1){ vb -= (1<<i); out.println("? "+va+" "+vb); out.flush(); ok = sc.nextInt(); } else { va -= (1<<i); out.println("? "+va+" "+vb); out.flush(); ok = sc.nextInt(); } } } } out.println("! "+va+" "+vb); out.flush(); } }
logn
1167.java
0.3
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Scanner; public class A1180 { public static void main(String[] args) throws FileNotFoundException { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); int layers = n-1; int counter =0; for(int i =1 ;i < layers + 1 ; i ++) { counter += i ; } //System.out.println(counter); System.out.println(1 + counter*4); } }
n
1000.java
0.5
import java.util.*; import java.io.*; import java.math.*; public class round569d2b { public static void main(String args[]) { FastScanner in = new FastScanner(System.in); int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } if (n % 2 == 0) { for (int i = 0; i < n; i++) { if (arr[i] >= 0) { arr[i] = -1*arr[i]-1; } } } else { int max = Integer.MIN_VALUE; int maxIndex = 0; for (int i = 0; i < n; i++) { int elem = arr[i]; if (elem < 0) { elem = -1*elem-1; } if (elem > max) { max = elem; maxIndex = i; } } for (int i = 0; i < n; i++) { if (i == maxIndex) { if (arr[i] < 0) { arr[i] = -1*arr[i]-1; } } else { if (arr[i] >= 0) { arr[i] = -1*arr[i]-1; } } } } StringBuilder sb = new StringBuilder(); for (int i = 0; i < n ;i++) { sb.append(arr[i] + " "); } System.out.println(sb); } // ====================================================================================== // =============================== Reference Code ======================================= // ====================================================================================== static int greatestDivisor(int n) { int limit = (int) Math.sqrt(n); int max = 1; for (int i = 2; i <= limit; i++) { if (n % i == 0) { max = Integer.max(max, i); max = Integer.max(max, n / i); } } return max; } // Method to return all primes smaller than or equal to // n using Sieve of Eratosthenes static boolean[] sieveOfEratosthenes(int n) { // Create a boolean array "prime[0..n]" and initialize // all entries it as true. A value in prime[i] will // finally be false if i is Not a prime, else true. boolean prime[] = new boolean[n + 1]; for (int i = 0; i <= n; i++) prime[i] = true; prime[0] = false; prime[1] = false; for (int p = 2; p * p <= n; p++) { // If prime[p] is not changed, then it is a prime if (prime[p] == true) { // Update all multiples of p for (int i = p * p; i <= n; i += p) prime[i] = false; } } return prime; } // Binary search for number greater than or equal to target // returns -1 if number not found private static int bin_gteq(int[] a, int key) { int low = 0; int high = a.length; int max_limit = high; while (low < high) { int mid = low + (high - low) / 2; if (a[mid] < key) { low = mid + 1; } else high = mid; } return high == max_limit ? -1 : high; } public static int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } static class Tuple<X, Y> { public final X x; public final Y y; public Tuple(X x, Y y) { this.x = x; this.y = y; } public String toString() { return "(" + x + "," + y + ")"; } } static class Tuple3<X, Y, Z> { public final X x; public final Y y; public final Z z; public Tuple3(X x, Y y, Z z) { this.x = x; this.y = y; this.z = z; } public String toString() { return "(" + x + "," + y + "," + z + ")"; } } static Tuple3<Integer, Integer, Integer> gcdExtended(int a, int b, int x, int y) { // Base Case if (a == 0) { x = 0; y = 1; return new Tuple3(0, 1, b); } int x1 = 1, y1 = 1; // To store results of recursive call Tuple3<Integer, Integer, Integer> tuple = gcdExtended(b % a, a, x1, y1); int gcd = tuple.z; x1 = tuple.x; y1 = tuple.y; // Update x and y using results of recursive // call x = y1 - (b / a) * x1; y = x1; return new Tuple3(x, y, gcd); } // Returns modulo inverse of a // with respect to m using extended // Euclid Algorithm. Refer below post for details: // https://www.geeksforgeeks.org/multiplicative-inverse-under-modulo-m/ static int inv(int a, int m) { int m0 = m, t, q; int x0 = 0, x1 = 1; if (m == 1) return 0; // Apply extended Euclid Algorithm while (a > 1) { // q is quotient q = a / m; t = m; // m is remainder now, process // same as euclid's algo m = a % m; a = t; t = x0; x0 = x1 - q * x0; x1 = t; } // Make x1 positive if (x1 < 0) x1 += m0; return x1; } // k is size of num[] and rem[]. // Returns the smallest number // x such that: // x % num[0] = rem[0], // x % num[1] = rem[1], // .................. // x % num[k-2] = rem[k-1] // Assumption: Numbers in num[] are pairwise // coprime (gcd for every pair is 1) static int findMinX(int num[], int rem[], int k) { // Compute product of all numbers int prod = 1; for (int i = 0; i < k; i++) prod *= num[i]; // Initialize result int result = 0; // Apply above formula for (int i = 0; i < k; i++) { int pp = prod / num[i]; result += rem[i] * inv(pp, num[i]) * pp; } return result % prod; } /** * Source: Matt Fontaine */ static class FastScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int chars; public FastScanner(InputStream stream) { this.stream = stream; } int read() { if (chars == -1) throw new InputMismatchException(); if (curChar >= chars) { curChar = 0; try { chars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (chars <= 0) return -1; } return buf[curChar++]; } boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } boolean isEndline(int c) { return c == '\n' || c == '\r' || c == -1; } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String next() { 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 (isEndline(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndline(c)); return res.toString(); } } }
n
1002.java
0.5