src
stringlengths
95
64.6k
complexity
stringclasses
7 values
problem
stringlengths
6
50
from
stringclasses
1 value
import java.util.Scanner; public class B2 { public static void main (String args[]){ Scanner in = new Scanner(System.in); long n = in.nextLong(); long k = in.nextLong(); long upn = k; long tmp=upn; if(n==1){ System.out.println(0); return; } if(n<=k){ System.out.println(1); return; } //--- if(!bS(n, k, upn)){ System.out.println(-1); return; } boolean flag = false; while(bS(n, k, upn)){ tmp = upn; flag = true; upn=5*upn/6; if(tmp==upn) break; } long ans = tmp; if(!flag) upn=0; for(int i = (int)tmp;i>=upn;i--){ if(bS(n, k, i)){ ans=i; } else break; } System.out.println(ans); } static boolean bS(long key,long k ,long n) { long pipe = (n * (k-n+k+1))/2; pipe = pipe - n+1; if(pipe>=key){ return true; } else return false; } }
logn
287_B. Pipeline
CODEFORCES
import java.io.*; import java.util.*; public class Main { long sum ( long n ) { return (n*(n+1))/2; } public void solve ( ) throws Exception { Scanner in = new Scanner ( System.in ); long n = in.nextLong()-1; long k = in.nextLong()-1; long lo = 0, hi = k, mi; while ( lo < hi ) { mi = ( lo + hi ) / 2; if ( sum(k)-sum(k-mi-1) <= n ) lo = mi+1; else hi = mi; } long ans = lo; n -= ( sum(k) - sum(k-ans) ); k -= ans; if ( n > k ) println ( "-1" ); else if ( n == 0 ) println ( ans ); else println ( (ans+1) ); } public static void main ( String[] args ) throws Exception { (new Main()).solve(); } public static void print ( Object o ) { System.out.print ( o ); } public static void println ( Object o ) { System.out.println ( o ); } public static void println ( ) { System.out.println ( ); } }
logn
287_B. Pipeline
CODEFORCES
import java.util.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author dy */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { long N = in.nextLong(); int K = in.nextInt(); long pipes = 1; if(N == 1){ out.println(0);return; } long left = 0, right = K - 1, find = -1; while (left <= right){ long mid = (left + right) / 2; if(mid * mid + (1 - 2 * K) * mid + 2 * (N - 1) <= 0){ find = mid; right = mid - 1; } else left = mid + 1; } out.println(find); } }
logn
287_B. Pipeline
CODEFORCES
import java.util.Scanner; public class B{ public static void main(String args[]){ Scanner input = new Scanner(System.in); long n = input.nextLong(); long k = input.nextLong(); System.out.println(solve(n, k)); input.close(); } public static long solve(long n, long k){ long dis = n - k; if(n == 1) return 0; if((((k - 2) * ((k - 2) + 1)) / 2) + 1 <= dis) return -1; if(k >= n) return 1; // long ans = 2; long now = (((k - 2) * ((k - 2) + 1)) / 2) + 1 + k; long dist = Math.abs(now - n); long delta = 1 + 8 * dist; double ret = (1 + Math.sqrt(delta)) / 2; // now = (((k - 2) * ((k - 2) + 1)) / 2) + 1 + k; dist = Math.abs(now - k); delta = 1 + 8 * dist; double nret = (1 + Math.sqrt(delta)) / 2; // double back = nret - ret; ans = (long) back; return ans + 2; } }
logn
287_B. Pipeline
CODEFORCES
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String[] info = s.split(" "); long n = Long.parseLong(info[0]); long k = Long.parseLong(info[1]); sc.close(); long maximum = k * (k - 1) / 2 + 1; if (n == 1) System.out.println(0); else if (n > maximum) System.out.println(-1); else { long left = 0, right = k - 1; while (left + 1 < right) { long mid = (right + left) / 2; if (mid * (k - 1 + k - mid) / 2 + 1 >= n) right = mid; else left = mid; } System.out.println(right); } } }
logn
287_B. Pipeline
CODEFORCES
import java.io.*; import java.util.*; public class SolutionB { BufferedReader in; StringTokenizer str; PrintWriter out; String SK; String next() throws IOException { while ((str == null) || (!str.hasMoreTokens())) { SK = in.readLine(); if (SK == null) return null; str = new StringTokenizer(SK); } return str.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(next()); } double nextDouble() throws IOException { return Double.parseDouble(next()); } long nextLong() throws IOException { return Long.parseLong(next()); } char[] charArray() throws IOException{ return next().toCharArray(); } public static void main(String[] args) throws IOException { new SolutionB().run(); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter("output.txt"); solve(); out.close(); } void solve() throws IOException { //out.println(Long.MAX_VALUE); long n = nextLong(); long k = nextLong(); long m=num1(k); if(m<n){ out.println(-1); return; } if(m==n){ out.println(k-1); return; } long ans=binS(n,k); out.println(k-ans); } long num1(long k){ long r=k*(k+1)/2-1; r=r-(k-2); return r; } long num2(long k){ return num1(k)-1; } long binS(long n, long k) { long start, end, mid; start = 2; end = k; long ret=-1; while (start <= end) { mid = (start + end) / 2; if (num1(k)-num2(mid) == n) { return mid; } else if (num1(k)-num2(mid) < n) { end = mid - 1; } else { ret=mid; start = mid + 1; } } return ret; } }
logn
287_B. Pipeline
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class TaskB { static BufferedReader in = new BufferedReader(new InputStreamReader( System.in)); static StringTokenizer str; static String SK; static String next() throws IOException { while ((str == null) || (!str.hasMoreTokens())) { SK = in.readLine(); if (SK == null) return null; str = new StringTokenizer(SK); } return str.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static long nextLong() throws IOException { return Long.parseLong(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException { long n = nextLong(); int k = nextInt(); if (n == 1) { System.out.println(0); return; } long sum = (((2 + (long) k)) * ((long) k - 1)) / 2 - ((long) k - 2); if (n > sum) { System.out.println(-1); return; } else if (n <= k) { System.out.println(1); return; } long cnt = 0; long sum2 = 0; int index = binSearch(2, k, k, n); sum2 = (((long) (index) + k) * (long) (k - index + 1)) / 2 - (long) (k - index); cnt = k - index + 1; if (sum2 == n) { System.out.println(cnt); return; } if (sum2 > n) for (int kk = index; kk <= k; kk++) { sum2 = (((long) (kk) + k) * (long) (k - kk + 1)) / 2 - (long) (k - kk); cnt--; if (sum2 <= n) { System.out.println(cnt + 1); return; } } else { for (int kk = index - 1; kk >= 2; kk--) { sum2 = (((long) (kk) + k) * (long) (k - kk + 1)) / 2 - (long) (k - kk); cnt++; if (sum2 >= n) { System.out.println(cnt); return; } } } System.out.println(-1); return; } static int binSearch(int l, int r, int k, long n) { while (true) { int mid = l + (r - l) / 2; long sum2 = (((long) (mid) + k) * (long) (k - mid + 1)) / 2 - (long) (k - mid); if (l >= r || sum2 == n) { return mid; } else if (sum2 > n) { l = mid + 1; } else if (sum2 < n) { r = mid; } } } }
logn
287_B. Pipeline
CODEFORCES
import java.util.*; import java.io.*; public class Contest176B { public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); long n = sc.nextLong(); int k = sc.nextInt(); if( ((long)k * (long)(k + 1))/2 - 1 - (k - 2) < n){ System.out.println(-1); return; } if(n == 1) { System.out.println(0); return; } if(n <= k) { System.out.println(1); return; } int ans = rek(2, k, n, k); System.out.println(ans); } private static int rek(int s, int e, long n, int k){ //System.out.println(s + " " + e ); if(s == e){ return k - s + 1; } if(s + 1 == e){ if(sum(e, k) >= n) return k - e + 1; return k - s + 1; } int m = (s + e)/2; long ans = sum(m, k); if(ans == n) return k - m + 1; if(ans < n) return rek(s, m - 1, n, k); else return rek(m , e, n, k); } private static long sum(int a, int b ){ long sum1 = ((long)a * (long)(a - 1))/2; long sum2 = ((long)b * (long)(b + 1))/2; int numelement = b - a + 1; return sum2 - sum1 - (numelement - 1); } }
logn
287_B. Pipeline
CODEFORCES
import java.util.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Saul */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { long n = in.nextLong() ; int k = in.nextInt(); long top = 1L * k * (k+1) / 2L - k + 1; if ( n == 1L ){ out.print(0); return; } if ( n > top ){ out.print(-1); return; } int ans = 0; if ( n > 0 ){ ++ans; n -= k; k -= 2; } while ( n > 0 ){ ++ans; n -= k; k--; } out.print(ans); } }
logn
287_B. Pipeline
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.StringTokenizer; /** * Created with IntelliJ IDEA. * User: ira * Date: 3/23/13 * Time: 12:19 PM */ public class B2 { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String line = reader.readLine(); StringTokenizer tokenizer = new StringTokenizer(line); long n = Long.parseLong(tokenizer.nextToken()); long k = Long.parseLong(tokenizer.nextToken()); if (n == 1){ System.out.println("0"); return; } if (n <= k){ System.out.println("1"); return; } long first = 0; long end = k; long mid; while (first < end){ mid = first + (end - first)/2; if (is_exist(n, k , mid - 1)){ end = mid; } else { first = mid + 1; } } if (is_exist(n, k, end - 1)){ System.out.println((end )); return; } System.out.println("-1"); return; } static boolean is_exist(long n, long k, long x){ long res = n - (k - 1 + k - x)* (k - 1 - (k - x) + 1)/2; if (res <= k - x ){ return true; } return false; } }
logn
287_B. Pipeline
CODEFORCES
import java.util.Scanner; public class CodeforcesRound176B { /** * @param args */ public static void main(String[] args) { Scanner kde =new Scanner (System.in); Long n = kde.nextLong(); //дома и кол труб Long k = kde.nextLong(); // разветлители if(((k-1)*(k-2)+2*k)<(n*(long)2)) { System.out.println(-1); return; } Long a,b; if(n==1) { System.out.println(0); return; } if(k>=n) { System.out.println(1); return; } else { a=(long)2; b=k-1; } boolean flag =false; while(true) { if(a>=b) { break; } long c =(a+b)/2; if(2*(k-c+1)+(k-1+k-c+1)*(c-1)<(n*2)) { a=c+1; } else { b=c; } flag=true; } if(flag==true ) { System .out.println(a); } else { System .out.println(a); } } }
logn
287_B. Pipeline
CODEFORCES
import java.math.BigInteger; import java.util.Scanner; public class ehsan { public static BigInteger f(BigInteger m,BigInteger n){ BigInteger s,l; s=n.multiply(m.add(BigInteger.valueOf(1))); l=m.multiply(m.add(BigInteger.valueOf(1))); l=l.divide(BigInteger.valueOf(2)); s=s.subtract(l); s=s.subtract(m); return s; } public static BigInteger bs(BigInteger a,BigInteger b,BigInteger n,BigInteger d){ BigInteger c,e; c=a.add(b); c=c.divide(BigInteger.valueOf(2)); e=f(c,n); if(e.equals(d)) return c.add(BigInteger.valueOf(1)); if(a.equals(b.add(BigInteger.valueOf(-1)))) return b.add(BigInteger.valueOf(1)); if(e.compareTo(d)>0) return bs(a,c,n,d); return bs(c,b,n,d); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); BigInteger bi1 = sc.nextBigInteger(); BigInteger bi2 = sc.nextBigInteger(); BigInteger i,n=bi2; BigInteger i2=BigInteger.valueOf(1); BigInteger sum=BigInteger.valueOf(0); if(bi1.compareTo(bi2)<0){ System.out.println(0); return; } if(bi1.compareTo(bi2)==0){ System.out.println(1); return; } bi2=((n.multiply(n.add(BigInteger.valueOf(1)))).divide(BigInteger.valueOf(2))).subtract(n.subtract(BigInteger.valueOf(1))); if(bi1.compareTo(bi2)>0) System.out.println(-1); else{ System.out.println(bs(BigInteger.valueOf(0),n.add(BigInteger.valueOf(-2)),n,bi1)); } } }
logn
287_B. Pipeline
CODEFORCES
/** * Created with IntelliJ IDEA. * User: yuantian * Date: 3/24/13 * Time: 2:18 AM * Copyright (c) 2013 All Right Reserved, http://github.com/tyuan73 */ import java.util.*; public class Pipeline { public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = in.nextLong(); long k = in.nextLong(); if (n == 1) { System.out.println(0); return; } if (k >= n) { System.out.println(1); return; } long total = (k + 2) * (k - 1) / 2 - (k - 2); if (total < n) { System.out.println(-1); return; } /** * non-binary search version, O(n) * it will failed because of "over time limit erro" on this case: * 499999998500000001 1000000000 => 999955279 * total = 1; for(int i = (int)k; i >= 2; i--) { total += i - 1; if(total >= n) { System.out.println(k-i+1); return; } } */ /** * binary search search version. O(lgn) */ int i = 2, j = (int) k; while (i < j) { int m = (i + j + 1) / 2; total = (k + m) * (k - m + 1) / 2 - (k - m); if (total < n) j = m - 1; else i = m; } System.out.println(k - i + 1); } }
logn
287_B. Pipeline
CODEFORCES
import java.io.IOException; import java.util.Scanner; public class TC { static long N; static int k; static long WHOLESUM; static long SUM( long k ) { long res=k*( k+1 )/2; return res-1; } static long returnPipes( int mid ) { long not=SUM( mid-1 ); long totpipes=WHOLESUM-not; int number=k-mid+1; long res=totpipes-number+1; return res; } static long binarySearch( int lo, int hi ) { int res=Integer.MAX_VALUE; int val=0; while( lo <= hi ) { int mid=( lo+hi )/2; long cnt=returnPipes( mid ); val=k-mid+1;; if( cnt < N ) { hi=mid-1; continue; } else { res=Math.min( val, res ); lo=mid+1; } } if( res==Integer.MAX_VALUE ) return -1; else return res; } public static void main( String[] args ) throws IOException { Scanner s=new Scanner( System.in ); N=s.nextLong(); k=s.nextInt(); WHOLESUM=SUM( k ); if( N<=1 ) System.out.println(0 ); else System.out.println( binarySearch( 2, k ) ); } }
logn
287_B. Pipeline
CODEFORCES
import java.util.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Dzmitry Paulenka */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { long n = in.nextLong(); long k = in.nextLong(); if (n == 1) { out.println(0); return; } if (k * (k - 1) < 2 * (n - 1)) { out.println(-1); return; } long sq2 = 4 * k * k - 4 * k + 1 - 4 * (2 * n - 2); long km = Math.max(2, (long) ((Math.sqrt(sq2) + 3) / 2.0) - 3); while (((km + k - 2)*(k - km + 1) >= 2*(n-1))) { ++km; } out.println(k - km + 2); } }
logn
287_B. Pipeline
CODEFORCES
import java.util.Locale; import java.util.Scanner; public class PipelineSolver { private long n; private long k; public static void main(String[] args) { PipelineSolver solver = new PipelineSolver(); solver.readData(); int solution = solver.solve(); solver.print(solution); } private int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); } private int lcm(int a, int b) { return a * b / gcd(a, b); } private void print(int[] values) { StringBuilder builder = new StringBuilder(); for (int value : values) { builder.append(value); builder.append(" "); } print(builder); } private void print(Object value) { System.out.println(value); } private void print(boolean value) { System.out.println(value ? "YES" : "NO"); } private void print(int value) { System.out.println(value); } private void print(long value) { System.out.println(value); } private void print(double value) { System.out.printf(Locale.ENGLISH, "%.10f", value); } private int[] getDigits(int number) { int[] digits = new int[10]; int index = digits.length - 1; int digitsCount = 0; while (number > 0) { digits[index] = number % 10; number /= 10; index--; digitsCount++; } int[] result = new int[digitsCount]; System.arraycopy(digits, digits.length - digitsCount, result, 0, digitsCount); return result; } private int[] readArray(Scanner scanner, int size) { int[] result = new int[size]; for (int i = 0; i < size; i++) { result[i] = scanner.nextInt(); } return result; } private void readData() { Scanner scanner = new Scanner(System.in); n = scanner.nextLong(); k = scanner.nextLong(); } private int solve() { if (n == 1) { return 0; } if (n <= k) { return 1; } int result; long l; long d = (5 - 2 * k) * (5 - 2 * k) - 4 * (2 * n - 4 * k + 4); if (d < 0) { result = -1; } else { l = Math.min(Math.max((int)((2 * k - 3 - Math.sqrt(d)) / 2), 0), Math.max((int)((2 * k - 3 + Math.sqrt(d)) / 2), 0)); long difference = n - k * (l + 1) + l * (l + 3) / 2; if (l > k - 2) { result = -1; } else if (l == k - 2) { result = difference == 0 ? (int) (l + 1) : -1; } else { result = (int) (l + 1 + (difference == 0 ? 0 : 1)); } } return result; } }
logn
287_B. Pipeline
CODEFORCES
import java.util.Scanner; import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Dzmitry Paulenka */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, Scanner in, PrintWriter out) { long n = in.nextLong(); long k = in.nextLong(); if (n == 1) { out.println(0); return; } if (k * (k - 1) < 2 * (n - 1)) { out.println(-1); return; } long sq2 = 4 * k * k - 4 * k + 1 - 4 * (2 * n - 2); double sqrt = Math.sqrt(sq2); long sq = (long) sqrt; if ((sq + 1) * (sq + 1) == sq2) { sq = sq + 1; } else if ((sq - 1) * (sq - 1) == sq2) { sq = sq - 1; } if (sq*sq == sq2) { long kmin = (sq + 3) / 2; out.println(k - kmin + 1); } else { long km = Math.max(2, (long) ((sqrt + 3) / 2.0) - 2); while (((km + k - 2)*(k - km + 1) >= 2*(n-1))) { ++km; } out.println(k - km + 2); } } }
logn
287_B. Pipeline
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; import static java.lang.Math.*; import static java.lang.Integer.*; import static java.lang.Long.*; import static java.lang.Character.*; @SuppressWarnings("unused") public class round176B { static BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); static StringTokenizer st = new StringTokenizer(""); static int nextInt() throws Exception { return Integer.parseInt(next()); } static String next() throws Exception { while (true) { if (st.hasMoreTokens()) { return st.nextToken(); } String s = br.readLine(); if (s == null) { return null; } st = new StringTokenizer(s); } } public static void main(String[] args) throws Exception { long n = parseLong(next()); long k = parseLong(next()); if(n == 1){ System.out.println(0); return; } if (n <= k) { System.out.println(1); return; } if ((((k * (k + 1)) / 2) - 1) - (k - 2) < n) { System.out.println(-1); } else { long lo = 1; long hi = k + 1; int best = Integer.MAX_VALUE; while (lo < hi) { long mid = lo + ((hi - lo) / 2); long first = ((mid * (2 + (2 + (mid - 1)))) / 2) - (mid - 1); long last = ((mid * (k - mid + 1 + k)) / 2) - (mid - 1); if (n < first) { hi = mid; } else { if (n >= first && n <= last) { hi = mid; best = min(best, (int) mid); } else lo = mid + 1; } } System.out.println(best); } } }
logn
287_B. Pipeline
CODEFORCES
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; import static java.util.Arrays.*; import static java.util.Collections.*; // Pipeline // 2013/03/23 public class P287B{ Scanner sc=new Scanner(System.in); long n, k; void run(){ n=sc.nextLong(); k=sc.nextLong(); solve(); } void solve(){ long left=-1, right=k+1; for(; right-left>1;){ long m=(left+right)/2; long ub=k*(k+1)/2-(k-m)*(k-m+1)/2-(m-1); if(ub>=n){ right=m; }else{ left=m; } } println(""+(right>k?-1:right)); } void println(String s){ System.out.println(s); } public static void main(String[] args){ Locale.setDefault(Locale.US); new P287B().run(); } }
logn
287_B. Pipeline
CODEFORCES
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class B { public static void main(String[] args){ FastScanner sc = new FastScanner(); long n = sc.nextLong(); int k = sc.nextInt(); if(n==1){ System.out.println(0); return; } n=n-1; int count = 0; long nextK = k-1; while(true){ //System.out.println("n = " + n); if(nextK < 1 || (nextK <= 1 && n >1)){ System.out.println(-1); return; } nextK = Math.min(n, nextK); if(n==nextK){ System.out.println(count+1); return; } //System.out.println("nextK = " + nextK); //search for a of a...nextK long bSum = nextK * (nextK+1)/2; long a = nextK; long decrement = 1; while(bSum - (a-1)*a/2 <= n && a>=1){ a-= decrement; decrement *= 2; } a+=decrement/2; //System.out.println("a = " + a); count += nextK-a+1; //System.out.println("count = " + count); long nDecr = bSum-a*(a-1)/2; //System.out.println("bSum = " + bSum); //System.out.println("nDecr = " + nDecr); n -= nDecr; nextK = a-1; if(n==0){ System.out.println(count); return; } } } public static class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(String s) { try { br = new BufferedReader(new FileReader(s)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public FastScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String nextToken() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(nextToken()); } long nextLong() { return Long.parseLong(nextToken()); } double nextDouble() { return Double.parseDouble(nextToken()); } } }
logn
287_B. Pipeline
CODEFORCES
import java.util.*; public class Main{ public static void main(String[]args){ Scanner cin=new Scanner(System.in); long z=cin.nextLong()-1<<1,n=cin.nextInt(),l=-1,r=1+n,m; while(l<(m=l+r>>1)) if(z>(m+n-1)*(n-m)) r=m;else l=m; if(0<l) l=n-l; System.out.print(l); } }
logn
287_B. Pipeline
CODEFORCES
// practice with rainboy import java.io.*; import java.util.*; public class CF256B extends PrintWriter { CF256B() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF256B o = new CF256B(); o.main(); o.flush(); } long count(int n, int x, int y, int r) { long a = 2L * r * r + 2 * r + 1; int w; if ((w = 1 - (x - r)) > 0) a -= (long) w * w; if ((w = 1 - (y - r)) > 0) a -= (long) w * w; if ((w = (x + r) - n) > 0) a -= (long) w * w; if ((w = (y + r) - n) > 0) a -= (long) w * w; if ((w = r - 1 - (x - 1) - (y - 1)) > 0) a += (long) w * (w + 1) / 2; if ((w = r - 1 - (x - 1) - (n - y)) > 0) a += (long) w * (w + 1) / 2; if ((w = r - 1 - (n - x) - (y - 1)) > 0) a += (long) w * (w + 1) / 2; if ((w = r - 1 - (n - x) - (n - y)) > 0) a += (long) w * (w + 1) / 2; return a; } void main() { int n = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); int c = sc.nextInt(); int lower = -1, upper = c; while (upper - lower > 1) { int r = (lower + upper) / 2; if (count(n, x, y, r) >= c) upper = r; else lower = r; } println(upper); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.util.*; public class B { final String filename = new String("B").toLowerCase(); int n; int r, c; void solve() throws Exception { n = nextInt(); r = nextInt() - 1; c = nextInt() - 1; long count = nextLong(); long left = -1, right = n * 2L; while (left < right - 1) { long mid = (left + right) / 2; if (getSq(n, r, c, mid) >= count) { right = mid; } else { left = mid; } } // for (int i = 0; i <= 10; i++) { // System.err.println(getSq(n, r, c, i)); // } out.println(right); } long getSq(int n, int x, int y, long size) { long cur = (size + 1) * (size + 1) + size * size; cur -= get(x + size - (n - 1)); cur -= get(y + size - (n - 1)); cur -= get(-(x - size)); cur -= get(-(y - size)); cur += getCorner((x + 1) + (y + 1) - (size + 1)); cur += getCorner((x + 1) + (n - y) - (size + 1)); cur += getCorner((n - x) + (y + 1) - (size + 1)); cur += getCorner((n - x) + (n - y) - (size + 1)); return cur; } private long getCorner(long min) { if (min >= 0) { return 0; } min = -min; return min * (min + 1) / 2; } long get(long a) { if (a <= 0) { return 0; } return a * a; } void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); // in = new BufferedReader(new FileReader("input.txt")); // out = new PrintWriter("output.txt"); solve(); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } BufferedReader in; StringTokenizer st; PrintWriter out; String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(nextToken()); } long nextLong() throws Exception { return Long.parseLong(nextToken()); } double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } public static void main(String[] args) { new B().run(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.Scanner; public class Problem { public static void main(String[] args) throws Exception { Scanner input = new Scanner(System.in); int side = input.nextInt()-1; int x = input.nextInt()-1; int y = input.nextInt()-1; long target = input.nextLong(); int[] to_sides = {y, side - x, side - y, x}; int[] to_corners = {(y+1)+(side-x+1),(side-x+1)+(side-y+1),(side-y+1)+(x+1), (x+1)+(y+1)}; int min = Math.min(Math.min(y, x), Math.min(side - x, side - y)); int[] after_pass = {1, 1, 1, 1}; int[] corner_share = {1,1,1,1}; int steps = 0 , i; long init = 1 ; int grown = 4; while (init < target) { init += grown; steps++; if (steps >= min) { for (i = 0; i < 4; i++) { if (steps > to_sides[i]) { init -= after_pass[i]; after_pass[i] += 2; } if (steps >= to_corners[i]){ init += corner_share[i]++; //corner_share[i]++; } } } grown += 4; } System.out.println(steps); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.Scanner; public class B { public static final boolean DEBUG = false; Scanner sc; public void debug(Object o) { if (DEBUG) { int ln = Thread.currentThread().getStackTrace()[2].getLineNumber(); String fn = Thread.currentThread().getStackTrace()[2].getFileName(); System.out.println("(" + fn + ":" + ln+ "): " + o); } } public void pln(Object o) { System.out.println(o); } long getNumber(int x, int y, int n, int m) { int n1 = Math.max(0, x + m - n); int n2 = Math.max(0, y + m - n); int n3 = Math.max(0, 1 - (x - m)); int n4 = Math.max(0, 1 - (y - m)); int n12 = Math.max(0, n1 + n2 - m - 1); int n23 = Math.max(0, n2 + n3 - m - 1); int n34 = Math.max(0, n3 + n4 - m - 1); int n41 = Math.max(0, n4 + n1 - m - 1); // debug(n1 + " " + n2 + " " + n3 + " " + n4); // debug(n12 + " " + n23 + " " + n34 + " " + n41); int m1 = m+1; long nr = 1 + ((long)m1)*m1*2 - m1*2; nr -= ((long)n1)*n1 + ((long)n2)*n2 + ((long)n3)*n3 + ((long)n4)*n4; nr += ((long)n12)*(n12+1)/2 + ((long)n23)*(n23+1)/2 + ((long)n34)*(n34+1)/2 + ((long)n41)*(n41+1)/2; return nr; } public void run() { // pln(getNumber(1, 1, 10000, 10000000)); // pln(getNumber(1000, 1000, 1000, 1000000)); // if (true) return; sc = new Scanner(System.in); int n = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); int c = sc.nextInt(); if (c <= 1) { pln(0); return; } int ll = 0; int rr = 2*n+20; while(true) { int m = (ll+rr) / 2; if (getNumber(x, y, n, m) >= c) { rr = m; } else { ll = m; } // debug(ll + " " + rr); if (rr - ll < 3) { for (int m2=ll; m2<=rr; m2++) { if (getNumber(x, y, n, m2) >= c) { pln(m2); return; } } } } } public static void main(String[] args) { B t = new B(); t.run(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
//package codeforces.cf156; import java.io.*; import java.util.Arrays; public class ProblemB { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out)); long[] readInts() throws IOException { String[] strings = reader.readLine().split(" "); long[] ints = new long[strings.length]; for(int i = 0; i < ints.length; i++) { ints[i] = Long.parseLong(strings[i]); } return ints; } long foo(long a) { return a > 0 ? a * a : 0; } long boo(long a) { return a <= 0 ? 0 : a * (a + 1) / 2; } void solve() throws IOException { long[] tt = readInts(); long n = tt[0]; long x = tt[1]; long y = tt[2]; long c = tt[3]; long lo = -1, hi = 2 * 1000 * 1000 * 1000 + 2; // hi = 5; while(hi - lo > 1) { long m = (lo + hi) / 2; long s = 2 * m * m + 2 * m + 1; s -= foo(m - x + 1) + foo(m - y + 1) + foo(x + m - n) + foo(y + m - n); s += boo(m + 1 - (n + 1 + n + 1 - x - y)) + boo(m + 1 - (n + 1 - x + y)) + boo(m + 1 - (n + 1 - y + x)) + boo(m + 1 - (x + y)); if(s < c) lo = m; else hi = m; } writer.println(hi); writer.flush(); } public static void main(String[] args) throws IOException{ new ProblemB().solve(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.util.*; public class bender { static long[] dx = new long[]{0, 1, 0, -1}; static long[] dy = new long[]{-1, 0, 1, 0}; static long n, x, y, c; public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); //BufferedReader in = new BufferedReader(new FileReader("bender.in")); StringTokenizer dxdync = new StringTokenizer(in.readLine()); n = Long.parseLong(dxdync.nextToken()); x = Long.parseLong(dxdync.nextToken()); y = Long.parseLong(dxdync.nextToken()); c = Long.parseLong(dxdync.nextToken()); long a = 0; long b = c; while(a < b){ long m = (a + b)/2; long[] dxn = new long[4]; long[] dyn = new long[4]; for(int d = 0; d < 4; d++){ dxn[d] = x + dx[d] * m; dyn[d] = y + dy[d] * m; } long ret = (m+1)*(m+1) + m*m; ret -= h(1 - dyn[0]); ret -= h(dyn[2] - n); ret -= h(dxn[1] - n); ret -= h(1 - dxn[3]); ret += q(1 - dyn[0] - (n-x+1)); ret += q(1 - dyn[0] - x); ret += q(dyn[2] - n - (n - x + 1)); ret += q(dyn[2] - n - (x)); if (ret < c) a = m + 1; else b = m; } System.out.println(a); } public static long h(long x) { if (x <= 0) return 0; return 2*q(x) - x; } private static long q(long x){ if (x <= 0) return 0; return x*(x+1)/2; } }
logn
256_B. Mr. Bender and Square
CODEFORCES
//package code; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.PrintStream; import java.util.Arrays; import java.util.Random; import java.util.Scanner; import java.util.TreeMap; /** * * @author malek */ public class MainA { public static void main(String[] args) throws Exception { //String cwd = System.getProperty("user.dir") + "\\"; Scanner in = new Scanner(new BufferedInputStream(System.in)); PrintStream out = new PrintStream(System.out); //Scanner in = new Scanner(new BufferedInputStream(new FileInputStream(cwd + "src\\code\\in.in"))); //PrintStream out = new PrintStream(cwd + "src\\code\\out.out"); //======================= Solution solution = new Solution(in, out); solution.solve(); //======================= in.close(); out.close(); } static private class Solution { final int inf = (int)1e9; //final int MAX_N = 1000 * 1000 + 100; int n, x, y, c; int f(int u, int r, int sec){ if(u == 0 && r == 0) return 0; if(u == 0){ return r - 1; } if(r == 0){ return u - 1; } return Math.min(sec - 1, u - 1 + r - 1); } boolean isok(int sec){ int up = x - 1; int down = n - x; int right = n - y; int left = y - 1; int u = 0, d = 0, r = 0, l = 0; int total = 1; int add = 4; for(int i = 1; i <= sec; i++){ int cc = 0; if(i > up && ++cc > 0) u++; if(i > down && ++cc > 0) d++; if(i > right && ++cc > 0) r++; if(i > left && ++cc > 0) l++; total += add - cc; total -= Math.max(0, f(u, r, i)); total -= Math.max(0, f(u, l, i)); total -= Math.max(0, f(d, r, i)); total -= Math.max(0, f(d, l, i)); if(total >= c) return true; add += 4; } return false; } public void solve() { n = in.nextInt(); x = in.nextInt(); y = in.nextInt(); c = in.nextInt(); if(c == 1){ out.println(0); return; } int lo = 0, hi = 60000; while(lo < hi){ int mid = (lo + hi)/2; if(isok(mid)){ hi = mid; } else{ lo = mid + 1; } } out.println(lo); } public Solution(Scanner in, PrintStream out) { this.in = in; this.out = out; } Scanner in; PrintStream out; } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.math.*; import java.util.*; public class B { public static void main(String[] args) throws Exception { new B().solve(System.in, System.out); // new FileInputStream(new File("input.txt")), // new PrintStream(new FileOutputStream(new File("output.txt")))); } void solve(InputStream _in, PrintStream out) throws IOException { // BufferedReader in = new BufferedReader(new InputStreamReader(_in)); // String[] sp; Scanner sc = new Scanner(_in); long n = sc.nextLong(); long x = sc.nextLong() - 1; long y = sc.nextLong() - 1; long c = sc.nextLong(); long ub = 2 * n; long lb = -1; while (lb + 1 < ub) { long k = (lb + ub) / 2; long l, u, r, d; l = Math.max(0, x - k); u = Math.max(0, y - k); r = Math.min(n - 1, x + k); d = Math.min(n - 1, y + k); long ss = 0; // lu long lu = x - (k - (y - u)); if (l < lu) { long a = lu - l; ss += a * (a + 1) / 2; } // ld long ld = x - (k - (d - y)); if (l < ld) { long a = ld - l; ss += a * (a + 1) / 2; } // ru long ru = x + (k - (y - u)); if (ru < r) { long a = r - ru; ss += a * (a + 1) / 2; } // rd long rd = x + (k - (d - y)); if (rd < r) { long a = r - rd; ss += a * (a + 1) / 2; } long cc = (r + 1 - l) * (d + 1 - u) - ss; if (c <= cc) { // ok ub = k; } else { // ng lb = k; } } out.println(ub); } } // // // // // // // // // // // // // // // // // // // // // // // //
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author BSRK Aditya ([email protected]) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { long n,x,y,c; public void solve(int testNumber, InputReader in, OutputWriter out) { n = in.readInt(); x = in.readInt(); y = in.readInt(); c = in.readInt(); int lo = -1; int hi = (int)c; while(lo+1<hi) { int mi = (lo+hi)/2; if(P(mi)) hi = mi; else lo = mi; } out.printLine(hi); } private boolean P(int t) { if(t == -1) return false; int ans = 0; for(long i = Math.max(1,y-t); i <= Math.min(y+t,n); ++i) { long a = Math.abs(y - i); ans += D(Math.max(1,x-t+a), Math.min(n,x+t-a)); if(ans >= c) return true; } return false; } private long D(long a, long b) { return b - a + 1; } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { 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 c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.PrintWriter; import java.util.Arrays; import java.util.Scanner; public class B { static Scanner in; static int next() throws Exception {return in.nextInt();}; // static StreamTokenizer in; static int next() throws Exception {in.nextToken(); return (int) in.nval;} // static BufferedReader in; static PrintWriter out; public static long count(long k, long x, long y, long n) { long sum = 2*k*(k+1)+1; if (k >= x-1) { sum -= (k-x+1)*(k-x+1); } if (k >= y-1) { sum -= (k-y+1)*(k-y+1); } if (k + x >= n) { sum -= (k+x-n)*(k+x-n); } if (k + y >= n) { sum -= (k+y-n)*(k+y-n); } if (k > x+y-1) { sum += ((k+1-x-y)*(k+1-x-y+1))/2; } if (k > n-x+y) { sum += ((k+x-n-y)*(k+x-n-y+1))/2; } if (k > n-y+x) { sum += ((k+y-n-x)*(k+y-n-x+1))/2; } if (k > 2*n-x-y+1) { sum += ((k-2*n+x+y-1)*(k-2*n+x+y))/2; } return sum; } public static void main(String[] args) throws Exception { in = new Scanner(System.in); // in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); // in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); long n = in.nextLong(), x = in.nextLong(), y = in.nextLong(), c = in.nextLong(); long res = 0; while (count(res, x, y, n) < c) res++; out.println(res); out.println(); out.close(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.math.BigInteger; import java.util.StringTokenizer; public class Main implements Runnable { private BufferedReader br; private StringTokenizer tok; private PrintWriter out; static final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null; int[] a; int[] b; int[] p; int getCeils(int id, int step) { if (step > a[id] + b[id] - 1) { return 0; } if (a[id] < b[id]) { if (step < a[id]) { return step; } if (step >= a[id] && step <= b[id]) { return a[id]; } ++p[id]; return a[id] - p[id]; } else { if (step < b[id]) { return step; } if (step >= b[id] && step <= a[id]) { return b[id]; } ++p[id]; return b[id] - p[id]; } } void solve() throws IOException { int n = nextInt(), x = nextInt(), y = nextInt(), c = nextInt(); long s = 1; int step = 0; a = new int[4]; b = new int[4]; p = new int[4]; a[0] = x - 1; b[0] = n - y; a[1] = x - 1; b[1] = y - 1; a[2] = n - x; b[2] = y - 1; a[3] = n - x; b[3] = n - y; int xf = x + 1, xb = x - 1, yf = y + 1, yb = y - 1; while (s < c) { ++step; if (xf <= n) { ++s; ++xf; } if (xb > 0) { ++s; --xb; } if (yf <= n) { ++s; ++yf; } if (yb > 0) { ++s; --yb; } if (step == 1) { continue; } for (int i = 0; i < 4; ++i) { s += getCeils(i, step - 1); } } out.println(step); } public void run() { try { if (ONLINE_JUDGE) { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } else { br = new BufferedReader(new FileReader(new File("input.txt"))); out = new PrintWriter(new File("output.txt")); } solve(); br.close(); out.close(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } } public static void main(String[] args) { new Main().run(); } String nextToken() throws IOException { while (tok == null || !tok.hasMoreTokens()) tok = new StringTokenizer(br.readLine()); return tok.nextToken(); } String nextString() throws IOException { return nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } BigInteger nextBigInteger() throws IOException { return new BigInteger(nextToken()); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.StringTokenizer; public class P { public static void main(String[] args) throws NumberFormatException, IOException { Scanner sc = new Scanner(); PrintWriter out = new PrintWriter(System.out); int N = sc.nextInt(), x = sc.nextInt(), y = sc.nextInt(); long C = sc.nextLong(); int lo = 0, hi = (int) (1e6); int answer = -1; while (lo <= hi) { int L = lo + (hi - lo) / 2; long area = 0; for (int steps = 0; steps <= L; ++steps) { // L + 1 steps to right if (y + steps > N) break; long up = Math.min(x, 1 + L - steps), down = Math.min(N - x, L - steps); area += up + down; } for (int steps = 1; steps <= L; ++steps) { // L steps to left if (y - steps < 1) break; long up = Math.min(x, 1 + L - steps), down = Math.min(N - x, L - steps); area += up + down; } if (area >= C) { answer = L; hi = L - 1; } else lo = L + 1; } out.println(answer); out.flush(); out.close(); } static class Scanner { BufferedReader br; StringTokenizer st; Scanner() { br = new BufferedReader(new InputStreamReader(System.in)); } Scanner(String file) throws FileNotFoundException { br = new BufferedReader(new FileReader(file)); } String next() throws IOException { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine()); return st.nextToken(); } int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); } String nextLine() throws IOException { return br.readLine(); } long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); } } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Locale; import java.util.StringTokenizer; public class Main { private void solve() throws IOException { int n = nextInt(); int x = nextInt(); int y = nextInt(); int c = nextInt(); int lux = x; int luy = y + 1; int rux = x + 1; int ruy = y; int ldx = x - 1; int ldy = y; int rdx = x; int rdy = y - 1; int k = 1; int res = 0; while (k < c) { lux--; luy--; rux--; ruy++; rdx++; rdy++; ldx++; ldy--; int p = 0; p += lu(x - 1, luy, lux, y, n); p += ru(x, ruy, rux, y + 1, n); p += ld(x, ldy, ldx, y - 1, n); p += rd(x + 1, rdy, rdx, y, n); k += p; res++; } println(res); } private int lu(int x1, int y1, int x2, int y2, int n) { if (y1 > 0 && x2 > 0) { return x1 - x2 + 1; } else if (y1 > 0 && x2 < 1) { return x1; } else if (y1 < 1 && x2 > 0) { return y2; } else if (x1 - (1 - y1) > 0) { return lu(x1 - (1 - y1), 1, x2, y2, n); } else { return 0; } } private int ru(int x1, int y1, int x2, int y2, int n) { if (y1 <= n && x2 > 0) { return x1 - x2 + 1; } else if (y1 <= n && x2 < 1) { return x1; } else if (y1 > n && x2 > 0) { return n - y2 + 1; } else if (x1 - (y1 - n) > 0) { return ru(x1 - (y1 - n), n, x2, y2, n); } else { return 0; } } private int ld(int x1, int y1, int x2, int y2, int n) { if (y1 > 0 && x2 <= n) { return x2 - x1 + 1; } else if (y1 > 0 && x2 > n) { return n - x1 + 1; } else if (y1 < 1 && x2 <= n) { return y2; } else if (x1 + (1 - y1) <= n) { return ld(x1 + (1 - y1), 1, x2, y2, n); } else { return 0; } } private int rd(int x1, int y1, int x2, int y2, int n) { if (y1 <= n && x2 <= n) { return x2 - x1 + 1; } else if (y1 <= n && x2 > n) { return n - x1 + 1; } else if (y1 > n && x2 <= n) { return n - y2 + 1; } else if (x1 + (y1 - n) <= n) { return rd(x1 + (y1 - n), n, x2, y2, n); } else { return 0; } } private String next() throws IOException { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(reader.readLine()); } return st.nextToken(); } @SuppressWarnings("unused") private int nextInt() throws IOException { return Integer.parseInt(next()); } @SuppressWarnings("unused") private double nextDouble() throws IOException { return Double.parseDouble(next()); } @SuppressWarnings("unused") private long nextLong() throws IOException { return Long.parseLong(next()); } @SuppressWarnings("unused") private void println(Object o) { writer.println(o); } @SuppressWarnings("unused") private void print(Object o) { writer.print(o); } private BufferedReader reader; private PrintWriter writer; private StringTokenizer st; private void run() throws IOException { long time = System.currentTimeMillis(); Locale.setDefault(Locale.US); reader = new BufferedReader(new InputStreamReader(System.in)); writer = new PrintWriter(System.out); solve(); writer.close(); System.err.println(System.currentTimeMillis() - time); } public static void main(String[] args) throws IOException { new Main().run(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.Scanner; public class Main { static Scanner cin = new Scanner(System.in); static int n, x, y; static long c; private static long f(int mid) { long block = 1 + 4 * (long)(mid + 1) * mid / 2; int l = y - mid; int r = y + mid; int u = x - mid; int d = x + mid; if(l <= 0) block -= (long)(1 - l) * (1 - l); if(r > n) block -= (long)(r - n) * (r - n); if(u <= 0) block -= (long)(1 - u) * (1 - u); if(d > n) block -= (long)(d - n) * (d - n); if(u <= 0 && 1 - u > n - y + 1) { int t = (1 - u) - (n - y + 1); block += (long)t * (1 + t) / 2; } if(u <= 0 && (1 - u) > y) { int t = (1 - u) - y; block += (long)t * (1 + t) / 2; } if(d > n && d - n > n - y + 1) { int t = (d - n) - (n - y + 1); block += (long)t * (1 + t) / 2; } if(d > n && d - n > y) { int t = (d - n) - y; block += (long)t * (1 + t) / 2; } // System.out.println(block); return block; } public static void main(String args[]) { n = cin.nextInt(); x = cin.nextInt(); y = cin.nextInt(); c = cin.nextLong(); int low = 0, high = 1000000000; int ans = -1; while(low <= high) { int mid = (low + high) / 2; // System.out.println(mid + " " + f(mid)); if(f(mid) >= c) { ans = mid; high = mid - 1; } else { low = mid + 1; } } System.out.println(ans); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.Scanner; import java.math.BigInteger; import java.io.*; public class Main{ /** * @param args */ static BigInteger n, x, y, c; static BigInteger mk[] = new BigInteger[8]; public static BigInteger f(BigInteger t) { return t.multiply(t); } public static BigInteger g(BigInteger t) { return t.multiply(t.add(BigInteger.ONE)).shiftRight(1); } public static int solve(BigInteger z) { BigInteger ret = z.multiply(z.add(BigInteger.ONE)).shiftLeft(1); ret = ret.add(BigInteger.ONE); //System.out.println(z + " " + ret); for(int i = 0; i < 8; i += 2) { if(z.compareTo(mk[i]) > 0) { ret = ret.subtract(f(z.subtract(mk[i]))); } } for(int i = 1; i < 8; i += 2) { if(z.compareTo(mk[i]) > 0) { ret = ret.add(g(z.subtract(mk[i]))); } } //System.out.println(z + " " + ret); if(ret.compareTo(c) >= 0) return 1; return 0; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin = new Scanner(System.in); while(cin.hasNext()) { n = cin.nextBigInteger(); x = cin.nextBigInteger(); y = cin.nextBigInteger(); c = cin.nextBigInteger(); mk[0] = x.subtract(BigInteger.ONE); mk[2] = n.subtract(y); mk[4] = n.subtract(x); mk[6] = y.subtract(BigInteger.ONE); mk[1] = mk[0].add(mk[2]).add(BigInteger.ONE); mk[3] = mk[2].add(mk[4]).add(BigInteger.ONE); mk[5] = mk[4].add(mk[6]).add(BigInteger.ONE); mk[7] = mk[6].add(mk[0]).add(BigInteger.ONE); BigInteger beg = BigInteger.ZERO, end = mk[0], mid; for(int i = 1; i < 8; ++i) if(end.compareTo(mk[i]) < 0) end = mk[i]; while(beg.compareTo(end) < 0) { mid = beg.add(end).shiftRight(1); if(solve(mid) == 1) end = mid; else beg = mid.add(BigInteger.ONE); } System.out.println(end); } } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.Scanner; public class Main { /** * @param args */ static long[] dx = new long[]{0, 1, 0, -1}; static long[] dy = new long[]{-1, 0, 1, 0}; public static void main(String[] args) { Scanner r = new Scanner(System.in); long N = r.nextLong(); long X = r.nextLong(); long Y = r.nextLong(); long C = r.nextLong(); long lo = 0, hi = N * 2; while(lo < hi){ long T = (lo + hi) / 2; long[] NX = new long[4]; long[] NY = new long[4]; for(int d = 0; d < 4; d++){ NX[d] = X + dx[d] * T; NY[d] = Y + dy[d] * T; } long ret = (T + 1) * (T + 1) + T * T; ret -= half(1 - NY[0]); ret -= half(NY[2] - N); ret -= half(NX[1] - N); ret -= half(1 - NX[3]); ret += quarter(1 - NY[0] - (N - X + 1)); ret += quarter(1 - NY[0] - (X)); ret += quarter(NY[2] - N - (N - X + 1)); ret += quarter(NY[2] - N - (X)); if(ret < C)lo = T + 1; else hi = T; } System.out.println(lo); } private static long half(long x) { if(x <= 0)return 0; else return 2 * quarter(x) - x; } private static long quarter(long x){ if(x <= 0)return 0; return x * (x + 1) / 2; } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.*; public class cf256b { public static void main(String[] args) { Scanner in = new Scanner(System.in); long n = in.nextLong(); long x = in.nextLong()-1; long y = in.nextLong()-1; long c = in.nextLong(); long lo = 0, hi = 2*n+10; while(hi - lo > 2) { long mid = (hi+lo)/2; if(calc(n,x,y,mid) >= c) hi = mid; else lo = mid; } while(calc(n,x,y,lo) < c) lo++; System.out.println(lo); } static long calc(long n, long x, long y, long t) { long ans = (2*t)*(t+1)+1; long top = Math.max(0,t-x); long bottom = Math.max(0,t-(n-1-x)); long left = Math.max(0,t-y); long right = Math.max(0,t-(n-1-y)); ans -= top*top + bottom*bottom + left*left + right*right; long tl = Math.max(0, t - (x+y+1)); long tr = Math.max(0, t - (x+(n-1-y)+1)); long bl = Math.max(0, t - ((n-1-x)+y+1)); long br = Math.max(0, t - ((n-1-x) + (n-1-y) + 1)); ans += (tl*tl+tl)/2 + (tr*tr+tr)/2 + (bl*bl+bl)/2 + (br*br+br)/2; return ans; } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.*; import java.math.BigInteger; import javax.naming.BinaryRefAddr; public class acm { public static BigInteger n; public static BigInteger TWO = new BigInteger("2"); public static BigInteger solve(BigInteger x, BigInteger y, BigInteger c) { BigInteger res = (c.add(BigInteger.ONE)).multiply(c.add(BigInteger.ONE)); res = res.add(c.multiply(c)); BigInteger k; //up k = c.subtract(x.subtract(BigInteger.ONE)); if(k.compareTo(BigInteger.ZERO) > 0) res = res.subtract(k.multiply(k)); //left k = c.subtract(y.subtract(BigInteger.ONE)); if(k.compareTo(BigInteger.ZERO) > 0) res = res.subtract(k.multiply(k)); //down k = c.subtract(n.subtract(x)); if(k.compareTo(BigInteger.ZERO) > 0) res = res.subtract(k.multiply(k)); //right k = c.subtract(n.subtract(y)); if(k.compareTo(BigInteger.ZERO) > 0) res = res.subtract(k.multiply(k)); //upleft k = c.subtract(x.add(y).subtract(BigInteger.ONE)); if(k.compareTo(BigInteger.ZERO) > 0) res = res.add(k.multiply(k.add(BigInteger.ONE)).divide(TWO)); //upright k = c.subtract(x.add(n).subtract(y)); if(k.compareTo(BigInteger.ZERO) > 0) res = res.add(k.multiply(k.add(BigInteger.ONE)).divide(TWO)); //dwleft k = c.subtract(y.add(n).subtract(x)); if(k.compareTo(BigInteger.ZERO) > 0) res = res.add(k.multiply(k.add(BigInteger.ONE)).divide(TWO)); //dwleft k = c.subtract(n.subtract(x).add(n.subtract(y)).add(BigInteger.ONE)); if(k.compareTo(BigInteger.ZERO) > 0) res = res.add(k.multiply(k.add(BigInteger.ONE)).divide(TWO)); return res; } public static void main(String[] args) {TWO = new BigInteger("2"); Scanner in = new Scanner(System.in); n = new BigInteger(in.next()); BigInteger x = new BigInteger(in.next()); BigInteger y = new BigInteger(in.next()); BigInteger c = new BigInteger(in.next()); BigInteger left = new BigInteger("0"); BigInteger right = new BigInteger("1000000000000"); while(left.compareTo(right) < 0) { BigInteger val = left.add(right).divide(TWO); if(solve(x, y, val).compareTo(c) >= 0) right = val; else left = val.add(BigInteger.ONE); } System.out.println(left); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class ProblemB { public static void main(String[] args) throws IOException { ProblemB solver = new ProblemB(); solver.init(); solver.solve(); } private void init() { } private void solve() throws IOException { Reader in = new Reader(System.in); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); int n = in.nextInt(); int x = in.nextInt(); int y = in.nextInt(); int c = in.nextInt(); long lo = 0; long hi = 2 * n; while (lo < hi) { long mid = (lo + hi)/2; long r = count(n, x , y, mid); if (r < c) { lo = mid + 1; } else { hi = mid;; } } out.println(lo); out.flush(); out.close(); } private long count(int n, int x, int y, long steps) { long r = 1 + 2 * steps * (1 + steps); r -= countWall(x - 1 - steps); r -= countWall(y - 1 - steps); r -= countWall(n - (x + steps)); r -= countWall(n - (y + steps)); r += countCorner(steps - (x - 1) - (y - 1) - 1); r += countCorner(steps - (y - 1) - (n - x) - 1); r += countCorner(steps - (n - x) - (n - y) - 1); r += countCorner(steps - (x - 1) - (n - y) - 1); return r; } private long countCorner(long x) { if (x <= 0) return 0; return x * ( x + 1) / 2; } private long countWall(long x) { if (x >= 0) return 0; return x * x; } private static class Reader { BufferedReader reader; StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ Reader(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ public String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt( next() ); } public double nextDouble() throws IOException { return Double.parseDouble( next() ); } public long nextLong() throws IOException { return Long.parseLong(next()); } } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.util.*; public class Example { BufferedReader in; PrintWriter out; StringTokenizer st; String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (Exception e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } public void run() throws Exception { //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter(new FileWriter("output.txt")); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.flush(); out.close(); in.close(); } long n, x, y, c; boolean check(long z) { long res = 1 + 2 * z * (z + 1); long bx1 = z - x + 1; long by1 = z - y + 1; long bxn = z - n + x; long byn = z - n + y; if (bx1 > 0) { res -= bx1 * bx1; } if (bxn > 0) { res -= bxn * bxn; } if (by1 > 0) { res -= by1 * by1; } if (byn > 0) { res -= byn * byn; } if (z > x + y - 1) { long m = z - x - y + 1; res += m * (m + 1) / 2; } if (z > x + n - y) { long m = z - x - n + y; res += m * (m + 1) / 2; } if (z > n - x + y) { long m = z - n - y + x; res += m * (m + 1) / 2; } if (z > n - x + n - y + 1) { long m = z - 2 * n + x + y - 1; res += m * (m + 1) / 2; } return res >= c; } public void solve() throws Exception { n = nextLong(); x = nextLong(); y = nextLong(); c = nextLong(); long l = 0; long r = 2 * n; while (r > l) { long mid = (r + l) / 2; if (check(mid)) { r = mid; } else { l = mid + 1; } } out.println(r); } public static void main(String[] args) throws Exception { new Example().run(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.InputStreamReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.math.BigDecimal; import java.io.BufferedWriter; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Writer; import java.util.StringTokenizer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { long n = in.nextLong(); long x = in.nextLong(); long y = in.nextLong(); long c = in.nextLong(); long tl, tr, tt = -1, t; tl = 0; tr = (long) 4e9; while(tl<tr){ t = (tl+tr)>>1; long cc = f(n, t, x, y); if(cc>=c){ tt = t; tr = t; }else tl = t+1; } out.writeln(tt); } public static long f(long n, long t, long x, long y){ long res = (t*t+t)/2 * 4 + 1; long s; if(x-t<1){ s = t-x+1; res -= s*s; } if(y-t<1){ s = t-y+1; res -= s*s; } if(x+t>n){ s = x+t-n; res -= s*s; } if(y+t>n){ s = y+t-n; res -= s*s; } s = t-(Math.abs(x-1)+Math.abs(y-1))-1; if(s>0) res+=(s*s+s)/2; s = t-(Math.abs(x-1)+Math.abs(n-y))-1; if(s>0) res+=(s*s+s)/2; s = t-(Math.abs(n-x)+Math.abs(n-y))-1; if(s>0) res+=(s*s+s)/2; s = t-(Math.abs(n-x)+Math.abs(y-1))-1; if(s>0) res+=(s*s+s)/2; return res; } } class InputReader{ private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream){ reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String next(){ while (tokenizer == null || !tokenizer.hasMoreTokens()){ try{ tokenizer = new StringTokenizer(reader.readLine()); }catch (IOException e){ throw new RuntimeException(e); } } return tokenizer.nextToken(); } public long nextLong(){ return Long.parseLong(next()); } } class OutputWriter{ private PrintWriter out; public OutputWriter(Writer out){ this.out = new PrintWriter(out); } public OutputWriter(OutputStream out){ this.out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out))); } public void write(Object ... o){ for(Object x : o) out.print(x); } public void writeln(Object ... o){ write(o); out.println(); } public void close(){ out.close(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Alex */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } static class TaskB { int n; int startrow; int startcol; long want; boolean check(long time) { long max = (long) 2 * time * (time + 1) + 1; long highest = startrow - time; if(highest < 0) { max -= Math.abs(highest) * Math.abs(highest); } long lowest = startrow + time; if(lowest >= n) { max -= Math.abs(lowest - n + 1) * Math.abs(lowest - n + 1); } long leftmost = startcol - time; if(leftmost < 0) { max -= Math.abs(leftmost) * Math.abs(leftmost); } long rightmost = startcol + time; if(rightmost >= n) { max -= Math.abs(rightmost - n + 1) * Math.abs(rightmost - n + 1); } long upperright = time - (startrow + 1) - (n - startcol); if(upperright >= 0) { max += (upperright + 1) * (upperright + 2) / 2; } long lowerright = time - (n - startrow) - (n - startcol); if(lowerright >= 0) { max += (lowerright + 1) * (lowerright + 2) / 2; } long upperleft = time - (startrow + 1) - (startcol + 1); if(upperleft >= 0) { max += (upperleft + 1) * (upperleft + 2) / 2; } long lowerleft = time - (n - startrow) - (startcol + 1); if(lowerleft >= 0) { max += (lowerleft + 1) * (lowerleft + 2) / 2; } return max >= want; } public void solve(int testNumber, InputReader in, OutputWriter out) { n = in.readInt(); startrow = in.readInt() - 1; startcol = in.readInt() - 1; want = in.readLong(); long low = 0, high = 2 * n; while(low < high) { long mid = (low + high) / 2; if(check(mid)) high = mid; else low = mid + 1; } out.printLine(low); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if(numChars == -1) throw new InputMismatchException(); if(curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch(IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { 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 readLong() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if(c == '-') { sgn = -1; c = read(); } long res = 0; do { if(c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while(!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if(filter != null) return filter.isSpaceChar(c); return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void printLine(long i) { writer.println(i); } } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.*; import java.math.*; import java.io.*; public class b2 { public static void main(String[] args) throws IOException { input.init(System.in); int n = input.nextInt(), x = input.nextInt(), y = input.nextInt(), c = input.nextInt(); long lo = 0, hi = 2*n; while(hi > lo+1) { long mid = (hi+lo)/2; long covered = go(n, x, y, mid); if(covered < c) lo = mid; else hi = mid; } if(go(n, x, y, lo) < c) lo++; System.out.println(lo); } public static long go(int n, int x, int y, long d) { long res = d*d + (d+1)*(d+1); long maxLeft = d - x; if(maxLeft >= 0) res -= (maxLeft+1)*(maxLeft+1); long maxTop = d - y; if(maxTop >= 0) res -= (maxTop+1)*(maxTop+1); long maxRight = d - (n+1-x); if(maxRight >= 0) res -= (maxRight+1)*(maxRight+1); long maxBot = d - (n+1-y); if(maxBot >= 0) res -= (maxBot+1)*(maxBot+1); long maxTopLeft = d - (x+y); if(maxTopLeft >= 0) res += (maxTopLeft+1)*(maxTopLeft+2)/2; long maxTopRight = d - ((n+1-x)+y); if(maxTopRight >= 0) res += (maxTopRight+1)*(maxTopRight+2)/2; long maxBotLeft = d - (x+(n+1-y)); if(maxBotLeft >= 0) res += (maxBotLeft+1)*(maxBotLeft+2)/2; long maxBotRight = d - ((n+1-x)+(n+1-y)); if(maxBotRight >= 0) res += (maxBotRight+1)*(maxBotRight+2)/2; return res; } public static class input { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.util.*; public class ProblemB { InputReader in; PrintWriter out; void solve() { int n = in.nextInt(); int x = in.nextInt(); int y = in.nextInt(); int c = in.nextInt(); int cur = 1; for (int k = 1; k <= c; k++) { // out.println(k + " " + cur); if (cur >= c) { out.println(k - 1); return; } //x + i; y + k - i; int iLess = n - x; int iMore = y + k - n; if (iLess > k) iLess = k; if (iMore < 0) iMore = 0; // out.println("iless == " + iLess + " imore == " + iMore); int add = iLess - iMore + 1; if (add < 0) add = 0; // out.println("add == " + add); cur += add; //x + i; y - k + i; iLess = n - x; iMore = 1 + k - y; if (iLess > k) iLess = k; if (iMore < 0) iMore = 0; // out.println("iless == " + iLess + " imore == " + iMore); add = iLess - iMore + 1; if (add < 0) add = 0; // out.println("add == " + add); cur += add; //x - i; y - k + i; iLess = x - 1; iMore = 1 + k - y; if (iLess > k) iLess = k; if (iMore < 0) iMore = 0; // out.println("iless == " + iLess + " imore == " + iMore); add = iLess - iMore + 1; if (add < 0) add = 0; // out.println("add == " + add); cur += add; //x - i; y + k - i; iLess = x - 1; iMore = y + k - n; if (iLess > k) iLess = k; if (iMore < 0) iMore = 0; // out.println("iless == " + iLess + " imore == " + iMore); add = iLess - iMore + 1; if (add < 0) add = 0; // out.println("add == " + add); cur += add; // out.println("cur == " + cur); //delete double if (x + k <= n) cur--; if (y - k >= 1) cur--; if (x - k >= 1) cur--; if (y + k <= n) cur--; } // throw new RuntimeException(); } ProblemB(){ boolean oj = System.getProperty("ONLINE_JUDGE") != null; try { if (oj) { in = new InputReader(System.in); out = new PrintWriter(System.out); } else { Writer w = new FileWriter("output.txt"); in = new InputReader(new FileReader("input.txt")); out = new PrintWriter(w); } } catch(Exception e) { throw new RuntimeException(e); } solve(); out.close(); } public static void main(String[] args){ new ProblemB(); } } class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public InputReader(FileReader fr) { reader = new BufferedReader(fr); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.Scanner; import java.util.concurrent.LinkedBlockingQueue; public class D { static long n; static long x; static long y; static long c; static long f(long t){ long s=0; if(t==0) s=1; else{ s=(4+4*t)/2*t+1; } if(x+t>n){ long c=x+t-n; s-=c*c; } if(x-t<=0){ long c=t-x+1; s-=c*c; } if(y+t>n){ long c=y+t-n; s-=c*c; } if(y-t<=0){ long c=t-y+1; s-=c*c; } if(t>x+y-1){ long m=t-x-y+1; s+=m*(m+1)/2; } if (t>x+n-y) { long m=t-x-n+y; s+=m*(m+1)/2; } if (t>n-x+y) { long m=t-n-y+x; s+=m*(m+1)/2; } if (t>n-x+n-y+1) { long m=t-2*n+x+y-1; s+=m*(m+1)/2; } return s; } public static void main(String[] args) { // TODO Auto-generated method stub Scanner in=new Scanner(new InputStreamReader(System.in)); PrintWriter out=new PrintWriter(System.out); n=in.nextLong(); x=in.nextLong(); y=in.nextLong(); c=in.nextLong(); long l=0; long r=2*n; while(l<r){ long m=(l+r)/2; long ff=f(m); if(ff<c){ l=m+1; } else{ r=m; } } out.println(l); out.close(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
//package round156; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class B { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(), x = ni(), y = ni(); long c = nl(); int l = x-1, r = n-x; int u = y-1, d = n-y; long low = -1, high = 2*n+3; while(high - low > 1){ long t = (high + low) / 2; long num = diag(t, l, u) + diag(t, r, u) + diag(t, l, d) + diag(t, r, d) + hor(t, l) + hor(t, r) + hor(t, u) + hor(t, d) + 1; if(num >= c){ high = t; }else{ low = t; } } out.println(high); } long hor(long t, int n) { return Math.min(t, n); } long diag(long t, long r, long u) { if(t > 2+r+u-2){ return r*u; } long ret = t*(t-1)/2; if(t > r)ret -= (t-r)*(t-r-1)/2; if(t > u)ret -= (t-u)*(t-u-1)/2; return ret; } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new B().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import static java.lang.Math.*; import java.io.*; import java.util.*; public class A { BufferedReader in; PrintWriter out; StringTokenizer st; public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch(Exception e) {} } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } boolean bit(int m, int i) { return (m & (1 << i)) > 0; } int n, x, y, c; long cnt(int m) { long ret=0; for (int i=max(1, y-m); i<=min(n, y+m); i++) { int x1 = max(1, x - (m - abs(i - y))); int x2 = min(n, x + (m - abs(i - y))); ret += x2 - x1 + 1; } return ret; } public void run() { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); n = nextInt(); x = nextInt(); y = nextInt(); c = nextInt(); int l = 0, r = 1000000; int ans=0; while (l <= r) { int m = (l+r) / 2; if (cnt(m) >= c) { ans = m; r = m-1; } else l=m+1; } out.println(ans); out.close(); } class Pair implements Comparable<Pair> { long x,y; public Pair(long x, long y) { this.x=x; this.y=y; } public int compareTo(Pair o) { if (x != o.x) return sign(o.x - x); return sign(y - o.y); } } int sign(long x) { if (x < 0) return -1; if (x > 0) return 1; return 0; } public static void main(String[] args) throws Exception { new A().run(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.util.*; public class MrBenderAndSquare { static long n, x, y, c; public static void main(String[] args) throws IOException { Kattio io = new Kattio(System.in); n = io.getLong(); x = io.getLong(); y = io.getLong(); c = io.getLong(); // for (int i = 0; i < 10; i++) io.println(f(i)); // io.println(); long lo = 0; long hi = c; while (lo < hi) { long mid = lo + (hi - lo) / 2; if (f(mid) >= c) { hi = mid; } else { lo = mid + 1; } } io.println(lo); io.close(); } static long f(long t) { long res = 0; // Sides long left = Math.max(0, t - (x - 1)); res -= left*left; long right = Math.max(0, t - (n - x)); res -= right*right; long up = Math.max(0, t - (y - 1)); res -= up*up; long down = Math.max(0, t - (n - y)); res -= down*down; // Middle res += 1 + 2*t*(t+1); // Corners long upLeft = Math.max(0, t - (x + y) + 1); long upRight = Math.max(0, t - (n - x + 1 + y) + 1); long downLeft = Math.max(0, t - (x + n - y + 1) + 1); long downRight = Math.max(0, t - (n - x + 1 + n - y + 1) + 1); res += upLeft * (upLeft + 1) / 2; res += upRight * (upRight + 1) / 2; res += downLeft * (downLeft + 1) / 2; res += downRight * (downRight + 1) / 2; return res; } static class Kattio extends PrintWriter { public Kattio(InputStream i) { super(new BufferedOutputStream(System.out)); r = new BufferedReader(new InputStreamReader(i)); } public Kattio(InputStream i, OutputStream o) { super(new BufferedOutputStream(o)); r = new BufferedReader(new InputStreamReader(i)); } public boolean hasMoreTokens() { return peekToken() != null; } public int getInt() { return Integer.parseInt(nextToken()); } public double getDouble() { return Double.parseDouble(nextToken()); } public long getLong() { return Long.parseLong(nextToken()); } public String getWord() { return nextToken(); } private BufferedReader r; private String line; private StringTokenizer st; private String token; private String peekToken() { if (token == null) try { while (st == null || !st.hasMoreTokens()) { line = r.readLine(); if (line == null) return null; st = new StringTokenizer(line); } token = st.nextToken(); } catch (IOException e) { } return token; } private String nextToken() { String ans = peekToken(); token = null; return ans; } } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.IOException; import java.io.OutputStreamWriter; import java.io.BufferedWriter; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.util.NoSuchElementException; import java.io.Writer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Egor Kulikov ([email protected]) */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, OutputWriter out) { int side = in.readInt(); int row = in.readInt() - 1; int column = in.readInt() - 1; int required = in.readInt(); long left = 0; long right = 2 * side - 2; while (left < right) { long current = (left + right) / 2; long result = calculate(row, column, current) + calculate(column, side - row - 1, current) + calculate(side - row - 1, side - column - 1, current) + calculate(side - column - 1, row, current) + 1; if (result >= required) right = current; else left = current + 1; } out.printLine(left); } private long calculate(int row, int column, long current) { column++; long total = 0; long mn = Math.min(row, column); long mx = Math.max(row, column); if (current <= mn) return current * (current + 1) / 2; total += mn * (mn + 1) / 2; current -= mn; mx -= mn; if (current <= mx) return total + mn * current; total += mn * mx; current -= mx; if (current < mn) return total + (mn - 1) * mn / 2 - (mn - current - 1) * (mn - current) / 2; return total + (mn - 1) * mn / 2; } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public int readInt() { 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); } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main{ public static long howMany(long n, long x, long y, long s){ long res = 0; int cnt = 0; long[] px = new long[9]; long[] py = new long[9]; if(x - s < 1){ px[cnt] = 1; py[cnt++] = y-x+s+1 <= n ? y-x+s+1 : n; px[cnt] = 1; py[cnt++] = x+y-s-1 > 0? x+y-s-1: 1; res += 6; }else{ px[cnt] = x-s; py[cnt++] = y; res += 2; } if(y - s < 1){ py[cnt] = 1; px[cnt++] = x+y-s-1 > 0 ? x+y-s-1 : 1; py[cnt] = 1; px[cnt++] = x-(y-s)+1 <= n ? x-y+s+1: n; res += 6; }else{ px[cnt] = x; py[cnt++] = y-s; res += 2; } if(x + s > n){ px[cnt] = n; py[cnt++] = y-(x+s)+n > 0 ? y-(x+s)+n : 1; px[cnt] = n; py[cnt++] = x+s+y - n <= n ? x+s+y-n : n; res += 6; }else{ px[cnt] = x+s; py[cnt++] = y; res += 2; } if(y + s > n){ py[cnt] = n; px[cnt++] = x+y+s-n <= n? x+y+s-n : n; py[cnt] = n; px[cnt++] = n-(y+s-x) > 0 ? n-(y+s-x) :1; res += 6; }else{ px[cnt] = x; py[cnt++] = y+s; res += 2; } px[cnt] = px[0]; py[cnt] = py[0]; long ret = 0; long sum = 0; for(int i = 0; i < cnt; i++){ ret += px[i]*py[i+1]-py[i]*px[i+1]; sum += Math.max(Math.abs(px[i]-px[i+1]), Math.abs(py[i]-py[i+1]))+1; } return (4*ret + 4*sum - res)/8; } public static void main(String[] args) throws Exception{ BufferedReader r = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer str = new StringTokenizer(r.readLine()); int n = Integer.parseInt(str.nextToken()); int x = Integer.parseInt(str.nextToken()); int y = Integer.parseInt(str.nextToken()); long c = Long.parseLong(str.nextToken()); if(c == 1){ System.out.println(0); return; } long high = 1; while(howMany(n, x, y, high) < c){ high <<= 1; } long low = high>>1; while(high - low > 1){ long med = (high+low)/2; if(howMany(n, x, y, med) < c){ low = med; }else{ high = med; } } System.out.println(high); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.StringTokenizer; public class taskB { StringTokenizer st; BufferedReader in; PrintWriter out; public static void main(String[] args) throws NumberFormatException, IOException { taskB solver = new taskB(); solver.open(); long time = System.currentTimeMillis(); solver.solve(); if (!"true".equals(System.getProperty("ONLINE_JUDGE"))) { System.out.println("Spent time: " + (System.currentTimeMillis() - time)); System.out.println("Memory: " + (Runtime.getRuntime().totalMemory() - Runtime .getRuntime().freeMemory())); } solver.close(); } public void open() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } public String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = in.readLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } long n, x, y, c; long sq(long n) { if (n <= 0) return 0; return n * n; } long lrsp(long len, long max) { long cnt = Math.min(len, max); long arpr = (1 + cnt) * cnt / 2; if (len > max) arpr += (len - max) * max; return arpr; } long onn(long len) { long up, down, left, right; long toup = x - 1, todown = n - x, toleft = y - 1, toright = n - y; left = Math.min(toleft, len); right = Math.min(toright, len); down = up = sq(len); up -= sq(len - toup); down -= sq(len - todown); len--; if (toright < len) { up -= lrsp(len - toright, toup); down -= lrsp(len - toright, todown); } if (toleft < len) { up -= lrsp(len - toleft, toup); down -= lrsp(len - toleft, todown); } return 1 + up + down + left + right; } public void solve() throws NumberFormatException, IOException { n = nextInt(); x = nextInt(); y = nextInt(); c = nextInt(); long down = 0, up = 2 * n + 13; while (up - down > 2) { long tmp = (up + down) / 2; if (onn(tmp) >= c) up = tmp; else down = tmp; } if (onn(down) >= c) out.println(down); else if (onn(down + 1) >= c) out.println(down + 1); else if (onn(down + 2) >= c) out.println(down + 2); else out.println(down + 3); } public void close() { out.flush(); out.close(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class Solution { static long n, x, y, c; static boolean can (long len) { BigInteger blen = BigInteger.valueOf(len); //BigInteger sum = blen.multiply(blen.add(BigInteger.ONE)).multiply(blen.shiftLeft(1).add(BigInteger.ONE)).divide(BigInteger.valueOf(6)); BigInteger sum = BigInteger.ONE; sum = sum.add(blen.multiply(blen.add(BigInteger.ONE)).shiftLeft(1)); long a1 = Math.max(0, len - x); sum = sum.subtract(BigInteger.valueOf(a1).multiply(BigInteger.valueOf(a1))); long a2 = Math.max(0, len - y); sum = sum.subtract(BigInteger.valueOf(a2).multiply(BigInteger.valueOf(a2))); long a3 = Math.max(0, len - (n - 1 - x)); sum = sum.subtract(BigInteger.valueOf(a3).multiply(BigInteger.valueOf(a3))); long a4 = Math.max(0, len - (n - 1 - y)); sum = sum.subtract(BigInteger.valueOf(a4).multiply(BigInteger.valueOf(a4))); if (y - a1 + 1 < 0) { long b1 = Math.abs(y - a1 + 1); sum = sum.add(BigInteger.valueOf(b1).multiply(BigInteger.valueOf(b1 + 1)).shiftRight(1)); } if (y - a3 + 1 < 0) { long b1 = Math.abs(y - a3 + 1); sum = sum.add(BigInteger.valueOf(b1).multiply(BigInteger.valueOf(b1 + 1)).shiftRight(1)); } if (y + a1 - 1 >= n) { long b1 = y + a1 - n; sum = sum.add(BigInteger.valueOf(b1).multiply(BigInteger.valueOf(b1 + 1)).shiftRight(1)); } if (y + a3 - 1 >= n) { long b1 = y + a3 - n; sum = sum.add(BigInteger.valueOf(b1).multiply(BigInteger.valueOf(b1 + 1)).shiftRight(1)); } return sum.compareTo(BigInteger.valueOf(c)) >= 0; } public static void main (String argv[]) { Scanner in = new Scanner(System.in); n = in.nextLong(); x = in.nextLong(); y = in.nextLong(); c = in.nextLong(); x--; y--; long lf = 0, rg = 2 * 1000 * 1000 * 1000 + 3; while (lf != rg) { long mid = (lf + rg) >> 1; if (can(mid)) rg = mid; else lf = mid + 1; } System.out.println(lf); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.*; import java.io.*; import java.math.BigInteger; public class Solution { BufferedReader in; PrintWriter out; StringTokenizer st; String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws Exception { return Integer.parseInt(nextToken()); } long fang(int s, int x, int y) { if (x > y) { int t = x; x = y; y = t; } if (s + 1 <= x) { return (long)(s + 1) * (s + 2) / 2; } if (s + 1 <= y) { return (long)x * (x + 1) / 2 + (long)(s + 1 - x) * x; } if (s + 1 >= x + y - 1) { return (long)x * y; } long q = x + y - 1 - s - 1; return (long)x * y - q * (q + 1) / 2; } long f(int s, int n, int x, int y) { long ans = fang(s, n - x + 1, n - y + 1) + fang(s, n - x + 1, y) + fang(s, x, n - y + 1) + fang(s, x, y); ans -= Math.min(s + 1, n - x + 1) + Math.min(s + 1, x) + Math.min(s + 1, n - y + 1) + Math.min(s + 1, y); return ans + 1; } void solve() throws Exception { int n = nextInt(); int x = nextInt(), y = nextInt(); long c = nextInt(); if (c == 1) { out.println(0); return; } int bg = 0, ed = 2 * n; while (ed > bg + 1) { int mm = (bg + ed) / 2; if (f(mm, n, x, y) >= c) ed = mm; else bg = mm; } out.println(ed); } void run() { try { Locale.setDefault(Locale.US); // in = new BufferedReader(new FileReader("input.txt")); // out = new PrintWriter("output.txt"); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } finally { out.close(); } } public static void main(String[] args) { // TODO Auto-generated method stub new Solution().run(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.InputMismatchException; import java.util.TreeSet; public class B { public static void main(String[] args) { MScanner sc = new MScanner(); PrintWriter out = new PrintWriter(System.out); long N = sc.nextLong(); long X = sc.nextLong(); long Y = sc.nextLong(); long C = sc.nextLong(); long low = 0; long high = N*2; long mid = 0; long ans = 0; while (low <= high) { mid = (low + high) >> 1; long painted = F(mid, X-1, Y-1, N); if (painted < C) { low = mid + 1; } else { ans = mid; high = mid - 1; } } out.println(ans); out.close(); } private static long F(long mid, long x, long y, long n) { long base = 2 * mid * (mid + 1) + 1; base -= excess(mid - x); base -= excess(mid - y); base -= excess(mid - (n-1-x)); base -= excess(mid - (n-1-y)); base += corner(mid - (x + y + 1)); base += corner(mid - (x + (n - y - 1) + 1)); base += corner(mid - ((n - x - 1) + y + 1)); base += corner(mid - (1 + (n - 1 - y) + (n - 1 - x))); return base; } private static long corner(long a) { if (a < 0)return 0; return (a * a + a) >> 1; } private static long excess(long thing) { if(thing<0)return 0; return thing * thing; } static class MScanner { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public MScanner() { stream = System.in; // stream = new FileInputStream(new File("dec.in")); } 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++]; } 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; } int nextInt() { return Integer.parseInt(next()); } int[] nextInt(int N) { int[] ret = new int[N]; for (int a = 0; a < N; a++) ret[a] = nextInt(); return ret; } int[][] nextInt(int N, int M) { int[][] ret = new int[N][M]; for (int a = 0; a < N; a++) ret[a] = nextInt(M); return ret; } long nextLong() { return Long.parseLong(next()); } long[] nextLong(int N) { long[] ret = new long[N]; for (int a = 0; a < N; a++) ret[a] = nextLong(); return ret; } double nextDouble() { return Double.parseDouble(next()); } double[] nextDouble(int N) { double[] ret = new double[N]; for (int a = 0; a < N; a++) ret[a] = nextDouble(); return ret; } 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(); } String[] next(int N) { String[] ret = new String[N]; for (int a = 0; a < N; a++) ret[a] = next(); return ret; } 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(); } String[] nextLine(int N) { String[] ret = new String[N]; for (int a = 0; a < N; a++) ret[a] = nextLine(); return ret; } } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Main implements Runnable { int n; boolean inBound (int x, int y) { return x >= 1 && y >= 1 && x <= n && y <= n; } void solve() throws Exception { n = sc.nextInt(); int y = sc.nextInt(); int x = sc.nextInt(); int c = sc.nextInt(); int yu = y; int yd = y; int xl = x; int xr = x; int current = 1; int time = 0; while (current < c) { time++; yu--; yd++; xl--; xr++; // left - up { int cur = time - 1; if (yu < 1) { cur -= (-yu); } if (xl < 1) { cur -= (-xl); } if (cur > 0) { current += cur; } } // right - up { int cur = time - 1; if (yu < 1) { cur -= (-yu); } if (xr > n) { cur -= (xr - n - 1); } if (cur > 0) { current += cur; } } // left - down { int cur = time - 1; if (yd > n) { cur -= (yd - n - 1); } if (xl < 1) { cur -= (-xl); } if (cur > 0) { current += cur; } } // right - down { int cur = time - 1; if (yd > n) { cur -= (yd - n - 1); } if (xr > n) { cur -= (xr - n - 1); } if (cur > 0) { current += cur; } } if (inBound(x, yd)) current++; if (inBound(x, yu)) current++; if (inBound(xl, y)) current++; if (inBound(xr, y)) current++; } out.println(time); } BufferedReader in; PrintWriter out; FastScanner sc; static Throwable uncaught; @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); solve(); } catch (Throwable t) { Main.uncaught = t; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread t = new Thread(null, new Main(), "", (1 << 26)); t.start(); t.join(); if (uncaught != null) { throw uncaught; } } } class FastScanner { BufferedReader reader; StringTokenizer strTok; public FastScanner(BufferedReader reader) { this.reader = reader; } public String nextToken() throws IOException { while (strTok == null || !strTok.hasMoreTokens()) { strTok = new StringTokenizer(reader.readLine()); } return strTok.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.*; import java.math.*; import java.io.*; public class Main { public static void main(String args[]) throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); //System.out.println(f(1,100,30)); String S[]=br.readLine().split(" "); int N=Integer.parseInt(S[0]); int x=Integer.parseInt(S[1]); int y=Integer.parseInt(S[2]); int c=Integer.parseInt(S[3]); int lo=0; //System.out.println(Long.MAX_VALUE); int hi=1000000000; while(hi-lo>=10) { int steps=(hi+lo)/2; //System.out.println("checking "+steps+" hi= "+hi+" lo = "+lo); long total=f(x,y,steps)+f(N-x+1,y,steps)+f(N-x+1,N-y+1,steps)+f(x,N-y+1,steps); //System.out.println(f(x,y,steps)+" "+f(N-x+1,y,steps)+" "+f(N-x+1,N-y+1,steps)+" "+f(x,N-y+1,steps)); total-=3; //x,y total-=Math.min(steps,x-1); //left total-=Math.min(steps,y-1); //down total-=Math.min(steps,N-x); //right total-=Math.min(steps,N-y); //up //System.out.println("total = "+total); if(total>=c) hi=steps+1; else lo=steps-1; } for(int steps=lo;steps<=hi;steps++) { //System.out.println("checking "+steps); long total=f(x,y,steps)+f(N-x+1,y,steps)+f(N-x+1,N-y+1,steps)+f(x,N-y+1,steps); total-=3; //x,y total-=Math.min(steps,x-1); //left total-=Math.min(steps,y-1); //down total-=Math.min(steps,N-x); //right total-=Math.min(steps,N-y); //up //System.out.println("total = "+total); if(total>=c) { System.out.println(steps); return; } } } public static long f(long a, long b, long steps) { //System.out.println("f("+a+","+b+","+steps+")"); steps++; long A=Math.min(a,b); long B=Math.max(a,b); long ans=0; if(steps>=(A+B)) { //System.out.println("case 1"); ans= A*B; } else if(steps<=A) { //System.out.println("case 2"); ans= (steps*(steps+1))/2; } else if(steps>A&&steps<=B) { //System.out.println("case 3"); ans= (A*(A+1))/2+(steps-A)*A; } else if(steps>B) { //System.out.println("case 4"); ans= (A*(A+1))/2+(B-A)*A+(steps-B)*A-((steps-B)*(steps-B+1))/2; } //System.out.println("\treturning "+ans); return ans; } } //must declare new classes here
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; public class Main { static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} public int i(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;} public double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int[] arr(int n){int[] ret = new int[n];for (int i = 0; i < n; i++) {ret[i] = i();}return ret;} } // |----| /\ | | ----- | // | / / \ | | | | // |--/ /----\ |----| | | // | \ / \ | | | | // | \ / \ | | ----- ------- public static long check(long mid,long x,long y,long n) { long ans=2*mid*mid+2*mid+1; long uleft=Math.max(0,mid-x+1); long dleft=Math.max(0,mid-(n-x)); long lleft=Math.max(0,mid-y+1); long rleft=Math.max(0,mid-(n-y)); ans-=uleft*uleft+dleft*dleft+lleft*lleft+rleft*rleft; ans+=(Math.max(0,mid-(x+y-1))*(Math.max(0,mid-(x+y-1))+1))/2; ans+=(Math.max(0,mid-(x+n-y))*(Math.max(0,mid-(x+n-y))+1))/2; ans+=(Math.max(0,mid-(y+n-x))*(Math.max(0,mid-(y+n-x))+1))/2; ans+=(Math.max(0,mid-(n-x+n-y+1))*(Math.max(0,mid-(n-x+n-y+1))+1))/2; return ans; } public static void main(String[] args)throws IOException { PrintWriter out= new PrintWriter(System.out); Reader sc=new Reader(); long n=sc.l(); long x=sc.l(); long y=sc.l(); long c=sc.l(); long low=0; long high=(long)Math.pow(10,9); while(low<high) { long mid=(low+high)/2; if(check(mid,x,y,n)>=c) high=mid; else low=mid+1; } out.println(low); out.flush(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author AlexFetisov */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { public void solve(int testNumber, InputReader in, PrintWriter out) { long n = in.nextInt(); long x = in.nextInt(); long y = in.nextInt(); long c = in.nextInt(); if (c == 1) { out.println(0); return; } long left = 1, right = 2 * n, middle, res = -1; long val = getNumberOfCells(x, y, n, 2); while (left <= right) { middle = (left + right) / 2; long numberOfCells = getNumberOfCells(x, y, n, middle); if (numberOfCells < c) { left = middle + 1; } else { res = middle; right = middle - 1; } } out.println(res); } private long getNumberOfCells(long x, long y, long n, long middle) { long res = 0; res += calc(x, y, middle + 1); res += calc(n - x + 1, y, middle + 1); res += calc(x, n - y + 1, middle + 1); res += calc(n - x + 1, n - y + 1, middle + 1); res -= calcX(x, n, middle); res -= calcX(y, n, middle); --res; return res; } private long calcX(long x, long n, long size) { long left = x - size; long right = x + size; left = Math.max(left, 1); right = Math.min(right, n); if (left <= right) { return right - left + 1; } return 0; } private long calc(long x, long y, long size) { if (size <= Math.min(x, y)) { return (1 + size) * size / 2; } if (size >= x + y - 1) { return x * y; } if (size > Math.max(x, y)) { return x * y - calc(x, y, x + y - 1 - size); } long min = Math.min(x, y); long res = (1 + min) * min / 2; long rest = size - min; res += rest * min; return res; } } class InputReader { private BufferedReader reader; private StringTokenizer stt; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { return null; } } public String nextString() { while (stt == null || !stt.hasMoreTokens()) { stt = new StringTokenizer(nextLine()); } return stt.nextToken(); } public int nextInt() { return Integer.parseInt(nextString()); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.util.*; public class BBi implements Runnable { public static void main(String[] args) { new Thread(new BBi()).run(); } BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer in; PrintWriter out = new PrintWriter(System.out); public String nextToken() throws IOException { while (in == null || !in.hasMoreTokens()) { in = new StringTokenizer(br.readLine()); } return in.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } int n, x, y; public long count(long z) { long total = 0; long[][] c = new long[][] { { x, y }, { y, n - x + 1 }, { n - x + 1, n - y + 1 }, { n - y + 1, x } }; for (int i = 0; i < c.length; i++) { long inside = z; for (int j = 0; j < c[i].length; j++) { if (z > c[i][j]) { total += Math.min(z - c[i][j], c[i][1 - j]) * c[i][j]; inside -= (z - c[i][j]); } } if (z > c[i][0] && z > c[i][1]) { total -= Math.min(z - c[i][0], c[i][1]) * Math.min(z - c[i][1], c[i][0]); } if (inside > 0) total += inside * (inside + 1) / 2; } for (int i = 0; i < c.length; i++) { total -= Math.min(z, c[i][0]); } return total + 1; } public long solve(int n, int x, int y, long c) throws IOException { this.n = n; this.x = x; this.y = y; long l = 0; long r = 2L * n + 2; while (r - l > 1) { long z = (l + r) / 2; if (c <= count(z)) { r = z; } else { l = z; } } return r - 1; } public void run() { try { out.println(solve(nextInt(), nextInt(), nextInt(), nextLong())); out.close(); } catch (Exception e) { e.printStackTrace(); System.exit(1); } } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.lang.reflect.*; import java.util.*; public class B { final int MOD = (int)1e9 + 7; final double eps = 1e-12; final int INF = (int)1e9; public B () { long N = sc.nextInt(); long X = sc.nextInt() - 1; long Y = sc.nextInt() - 1; long C = sc.nextInt(); long [] A1 = new long [] { X, Y }; long [] A2 = new long [] { X, Y }; long [] B1 = new long [] { X, Y }; long [] B2 = new long [] { X, Y }; long [] C1 = new long [] { X, Y }; long [] C2 = new long [] { X, Y }; long [] D1 = new long [] { X, Y }; long [] D2 = new long [] { X, Y }; long cnt = 1, T = 0; while (cnt < C) { if (A1[0] > 0) --A1[0]; else --A1[1]; if (A2[0] > 0) --A2[0]; else ++A2[1]; if (B1[1] > 0) --B1[1]; else --B1[0]; if (B2[1] > 0) --B2[1]; else ++B2[0]; if (C1[0] < N-1) ++C1[0]; else --C1[1]; if (C2[0] < N-1) ++C2[0]; else ++C2[1]; if (D1[1] < N-1) ++D1[1]; else --D1[0]; if (D2[1] < N-1) ++D2[1]; else ++D2[0]; long [] Z = { B1[0] - A1[0], C1[0] - B2[0], C2[0] - D2[0], D1[0] - A2[0] }; for (long z : Z) if (z >= 0) cnt += (z+1); if (Arrays.equals(A1, A2)) --cnt; if (Arrays.equals(B1, B2)) --cnt; if (Arrays.equals(C1, C2)) --cnt; if (Arrays.equals(D1, D2)) --cnt; ++T; } exit(T); } //////////////////////////////////////////////////////////////////////////////////// static MyScanner sc; static class MyScanner { public String next() { newLine(); return line[index++]; } public char nextChar() { return next().charAt(0); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { line = null; return readLine(); } public String [] nextStrings() { line = null; return readLine().split(" "); } public char [] nextChars() { return next().toCharArray(); } public Integer [] nextInts() { String [] L = nextStrings(); Integer [] res = new Integer [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } public Long [] nextLongs() { String [] L = nextStrings(); Long [] res = new Long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } public Double [] nextDoubles() { String [] L = nextStrings(); Double [] res = new Double [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Double.parseDouble(L[i]); return res; } ////////////////////////////////////////////// private boolean eol() { return index == line.length; } private String readLine() { try { return r.readLine(); } catch (Exception e) { throw new Error(e); } } private final BufferedReader r; MyScanner () { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine() { if (line == null || eol()) { line = readLine().split(" "); index = 0; } } } static void print (Object o, Object... a) { pw.println(build(o, a)); } static void exit (Object o, Object... a) { print(o, a); exit(); } static void exit () { pw.close(); System.out.flush(); System.err.println("------------------"); System.err.println("Time: " + ((millis() - t) / 1000.0)); System.exit(0); } void NO() { throw new Error("NO!"); } //////////////////////////////////////////////////////////////////////////////////// static String build(Object... a) { StringBuilder b = new StringBuilder(); for (Object o : a) append(b, o); return b.toString().trim(); } static void append(StringBuilder b, Object o) { if (o.getClass().isArray()) { int L = Array.getLength(o); for (int i = 0; i < L; ++i) append(b, Array.get(o, i)); } else if (o instanceof Iterable<?>) { for (Object p : (Iterable<?>)o) append(b, p); } else b.append(" ").append(o); } //////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { sc = new MyScanner (); new B(); exit(); } static void start() { t = millis(); } static PrintWriter pw = new PrintWriter(System.out); static long t; static long millis() { return System.currentTimeMillis(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.Arrays; public class ProblemB { public static void main(String[] args) throws IOException { BufferedReader s = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(System.out); String[] line = s.readLine().split(" "); long n = Long.valueOf(line[0]); long y = Long.valueOf(line[1]); long x = Long.valueOf(line[2]); long c = Long.valueOf(line[3]); long min = 0; long max = n*2L+20; for (int cnt = 0 ; cnt < 300 ; cnt++) { long med = (min+max) / 2L; long ct = isok(med, n, x, y, c); if (ct >= c) { max = med; } else { min = med+1; } } long lst = max; for (long d = -2 ; d <= 2 ; d++) { if (max+d >= 0 && isok(max+d, n, x, y, c) >= c) { lst = Math.min(lst, max+d); } } out.println(lst); out.flush(); } private static long isok(long time, long n, long x, long y, long c) { long total = time * 2 * (time + 1) + 1; long top = y - time; if (top <= 0) { long dy = Math.abs(top)+1; total -= dy*dy; long over = dy - x; if (over >= 1) { total += (1L + over) * over / 2; } over = dy - ((n + 1) - x); if (over >= 1) { total += (1L + over) * over / 2; } } long bottom = y + time; if (bottom > n) { long dy = Math.abs(bottom-n); total -= dy*dy; long over = dy - x; if (over >= 1) { total += (1L + over) * over / 2; } over = dy - ((n + 1) - x); if (over >= 1) { total += (1L + over) * over / 2; } } long left = x - time; if (left <= 0) { long dy = Math.abs(left)+1; total -= dy*dy; } long right = x + time; if (right > n) { long dy = Math.abs(right-n); total -= dy*dy; } return total; } public static void debug(Object... os){ System.err.println(Arrays.deepToString(os)); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.StringTokenizer; public class Solver { StringTokenizer st; BufferedReader in; PrintWriter out; public static void main(String[] args) throws NumberFormatException, IOException { Solver solver = new Solver(); solver.open(); solver.solve(); solver.close(); } public void open() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); } public String nextToken() throws IOException { while (st == null || !st.hasMoreTokens()) { String line = in.readLine(); if (line == null) return null; st = new StringTokenizer(line); } return st.nextToken(); } public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(nextToken()); } public double nextDouble() throws NumberFormatException, IOException { return Double.parseDouble(nextToken()); } int n; class Otr { int x1, y1, x2, y2; int dx, dy; public Otr(int x, int y, int dx, int dy) { super(); this.x1 = x; this.y1 = y; this.x2 = x; this.y2 = y; this.dx = dx; this.dy = dy; } int getAns() { if (x1 == x2 && y1 == y2) { int nx1 = x1 + dx; int ny2 = y2 + dy; if ((nx1 <= 0 || nx1 > n) && (ny2 <= 0 || ny2 > n)) { return 0; } } x1 += dx; if (x1 <= 0) { x1 = 1; y1 += dy; } if (x1 > n) { x1 = n; y1 += dy; } y2 += dy; if (y2 <= 0) { y2 = 1; x2 += dx; } if (y2 > n) { y2 = n; x2 += dx; } return Math.abs(x1 - x2) + 1; } @Override public String toString() { return "(" + x1 + "," + y1 + ")->(" + x2 + "," + y2 + ")"; } } int[] dxs = { -1, -1, 1, 1 }; int[] dys = { -1, 1, -1, 1 }; public void solve() throws NumberFormatException, IOException { n = nextInt(); int x = nextInt(); int y = nextInt(); long c = nextLong(); long now = 1; Otr[] otr = new Otr[4]; for (int i = 0; i < 4; i++) { otr[i] = new Otr(x, y, dxs[i], dys[i]); } int result = 0; while (now < c) { for (int i = 0; i < 4; i++) { now += otr[i].getAns(); } for (int i = 0; i < 3; i++) { for (int j = i + 1; j < 4; j++) { Otr o1 = otr[i]; Otr o2 = otr[j]; if (o1.x1!=o1.x2 || o1.y1!=o1.y2){ if (o2.x1!=o2.x2 || o2.y1!=o2.y2){ if (o1.x1 == o2.x1 && o1.y1 == o2.y1) { now--; } if (o1.x1 == o2.x2 && o1.y1 == o2.y2) { now--; } if (o1.x2 == o2.x1 && o1.y2 == o2.y1) { now--; } if (o1.x2 == o2.x2 && o1.y2 == o2.y2) { now--; } }else{ if (o1.x1 == o2.x1 && o1.y1 == o2.y1) { now--; } if (o1.x2 == o2.x1 && o1.y2 == o2.y1) { now--; } } }else{ if (o2.x1!=o2.x2 || o2.y1!=o2.y2){ if (o1.x2 == o2.x1 && o1.y2 == o2.y1) { now--; } if (o1.x2 == o2.x2 && o1.y2 == o2.y2) { now--; } }else{ if (o1.x1 == o2.x1 && o1.y1 == o2.y1) { now--; } } } } } result++; } out.println(result); } public void close() { out.flush(); out.close(); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.util.*; import java.math.*; public class P255D { @SuppressWarnings("unchecked") public void run() throws Exception { long n = nextLong(); long x = nextLong(); long y = nextLong(); long c = nextLong(); if ((n == 1) || (c == 1)) { println(0); return; } x = Math.min(x, n - x + 1); y = Math.min(y, n - y + 1); long t = 0; long s = 1; while (s < c) { t++; s += (t * 4) + ((t >= x + y) ? (t - x - y + 1) : 0) - ((t >= x) ? (t - x) * 2 + 1 : 0) - ((t >= y) ? (t - y) * 2 + 1 : 0) + ((t >= x + n - y + 1) ? (t - x - n + y) : 0) + ((t >= n - x + 1 + y) ? (t - n + x - y) : 0) - ((t >= n - x + 1) ? (t - n + x - 1) * 2 + 1 : 0) - ((t >= n - y + 1) ? (t - n + y - 1) * 2 + 1 : 0); } println(t); } public static void main(String... args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedOutputStream(System.out)); new P255D().run(); br.close(); pw.close(); } static BufferedReader br; static PrintWriter pw; StringTokenizer stok; String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } void print(byte b) { print("" + b); } void print(int i) { print("" + i); } void print(long l) { print("" + l); } void print(double d) { print("" + d); } void print(char c) { print("" + c); } void print(Object o) { if (o instanceof int[]) { print(Arrays.toString((int [])o)); } else if (o instanceof long[]) { print(Arrays.toString((long [])o)); } else if (o instanceof char[]) { print(Arrays.toString((char [])o)); } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o)); } else if (o instanceof short[]) { print(Arrays.toString((short [])o)); } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o)); } else if (o instanceof float[]) { print(Arrays.toString((float [])o)); } else if (o instanceof double[]) { print(Arrays.toString((double [])o)); } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o)); } else { print("" + o); } } void print(String s) { pw.print(s); } void println() { println(""); } void println(byte b) { println("" + b); } void println(int i) { println("" + i); } void println(long l) { println("" + l); } void println(double d) { println("" + d); } void println(char c) { println("" + c); } void println(Object o) { print(o); println(""); } void println(String s) { pw.println(s); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String next() throws IOException { return nextToken(); } String nextLine() throws IOException { return br.readLine(); } int [] readInt(int size) throws IOException { int [] array = new int [size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long [] readLong(int size) throws IOException { long [] array = new long [size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } double [] readDouble(int size) throws IOException { double [] array = new double [size]; for (int i = 0; i < size; i++) { array[i] = nextDouble(); } return array; } String [] readLines(int size) throws IOException { String [] array = new String [size]; for (int i = 0; i < size; i++) { array[i] = nextLine(); } return array; } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.InputStreamReader; import java.io.IOException; import java.util.InputMismatchException; import java.io.PrintStream; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Nipuna Samarasekara */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); FastPrinter out = new FastPrinter(outputStream); TaskB solver = new TaskB(); solver.solve(1, in, out); out.close(); } } class TaskB { ///////////////////////////////////////////////////////////// long n,x,y,c; public void solve(int testNumber, FastScanner in, FastPrinter out) { n=in.nextLong(); x=in.nextLong(); y=in.nextLong(); c=in.nextLong(); long td=-1,tup=2*(n-1); while(Math.abs(td-tup)>1){ long mid=(td+tup)/2; if(chk(mid))tup=mid; else td=mid; } out.println(tup); } private boolean chk(long t) { long ct=-3; long d=x,w=y; if(w>d){ long tt=w; w=d;d=tt; } if(t>=d+w-2) ct+=d*w; else if(t<w&&t<d) { // long k=w; ct+=(t+1)*(t+2)/2; } else if(t>=w&&t<d) { // long k=w; ct+=w*(w+1)/2; long k=t-(w-1); ct+=k*w; } else if(t>=w&&t>=d) { // long k=w; ct+=w*d; long k=w-2-(t-d); ct-=k*(k+1)/2; } //// w=x;d=n+1-y; if(w>d){ long tt=w; w=d;d=tt; } if(t>=d+w-2) ct+=d*w; else if(t<w&&t<d) { // long k=w; ct+=(t+1)*(t+2)/2; } else if(t>=w&&t<d) { // long k=w; ct+=w*(w+1)/2; long k=t-(w-1); ct+=k*w; } else if(t>=w&&t>=d) { // long k=w; ct+=w*d; long k=w-2-(t-d); ct-=k*(k+1)/2; } w=n+1-x;d=y; if(w>d){ long tt=w; w=d;d=tt; } if(t>=d+w-2) ct+=d*w; else if(t<w&&t<d) { // long k=w; ct+=(t+1)*(t+2)/2; } else if(t>=w&&t<d) { // long k=w; ct+=w*(w+1)/2; long k=t-(w-1); ct+=k*w; } else if(t>=w&&t>=d) { // long k=w; ct+=w*d; long k=w-2-(t-d); ct-=k*(k+1)/2; } w=n+1-x;d=n+1-y; if(w>d){ long tt=w; w=d;d=tt; } if(t>=d+w-2) ct+=d*w; else if(t<w&&t<d) { // long k=w; ct+=(t+1)*(t+2)/2; } else if(t>=w&&t<d) { // long k=w; ct+=w*(w+1)/2; long k=t-(w-1); ct+=k*w; } else if(t>=w&&t>=d) { // long k=w; ct+=w*d; long k=w-2-(t-d); ct-=k*(k+1)/2; } ct-=Math.min(t,x-1); ct-=Math.min(t,y-1); ct-=Math.min(t,n-x); ct-=Math.min(t,n-y); // System.out.println(t+" "+ct); if(ct>=c)return true; else return false; } } class FastScanner extends BufferedReader { public FastScanner(InputStream is) { super(new InputStreamReader(is)); } public int read() { try { int ret = super.read(); // if (isEOF && ret < 0) { // throw new InputMismatchException(); // } // isEOF = ret == -1; return ret; } catch (IOException e) { throw new InputMismatchException(); } } public String next() { StringBuilder sb = new StringBuilder(); int c = read(); while (isWhiteSpace(c)) { c = read(); } if (c < 0) { return null; } while (c >= 0 && !isWhiteSpace(c)) { sb.appendCodePoint(c); c = read(); } return sb.toString(); } static boolean isWhiteSpace(int c) { return c >= 0 && c <= 32; } public long nextLong() { return Long.parseLong(next()); } public String readLine() { try { return super.readLine(); } catch (IOException e) { return null; } } } class FastPrinter extends PrintWriter { public FastPrinter(OutputStream out) { super(out); } public FastPrinter(Writer out) { super(out); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.util.*; public class B { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; long f(int x, int y, int sz) { if (x > y) { int tmp = x; x = y; y = tmp; } if (sz <= x) return (long) sz * (sz + 1) / 2; if (sz >= x + y - 1) return (long) x * y; long val = x * (x + 1) / 2; if (sz <= y) return val + (long) (sz - x) * x; long rest = x + y - 1 - sz; return (long) x * y - (long) rest * (rest + 1) / 2; } long count(int x, int y, int n, int time) { long DL = f(x + 1, y + 1, time + 1); long DR = f(n - x, y + 1, time + 1); long UL = f(x + 1, n - y, time + 1); long UR = f(n - x, n - y, time + 1); // if (time == 1) // System.err.println(DL + " " + DR + " " + UL + " " + UR); long L = Math.min(x + 1, time + 1); long R = Math.min(n - x, time + 1); long U = Math.min(n - y, time + 1); long D = Math.min(y + 1, time + 1); // if (time == 1) // System.err.println(L + " " + R + " " + U + " " + D); return DL + DR + UL + UR - L - R - U - D + 1; } void solve() throws IOException { int n = nextInt(); int x = nextInt() - 1; int y = nextInt() - 1; long need = nextLong(); if (need == 1) { out.println(0); return; } int low = 0; int high = Math.max(x, n - 1 - x) + Math.max(y, n - 1 - y); // for (int i = 0; i <= 100; i++) // System.err.println(count(x, y, n, i)); while (low < high - 1) { int mid = (int) (((long) low + high) / 2); if (count(x, y, n, mid) >= need) high = mid; else low = mid; } out.println(high); } B() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new B(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
// practice with rainboy import java.io.*; import java.util.*; public class CF256B extends PrintWriter { CF256B() { super(System.out, true); } Scanner sc = new Scanner(System.in); public static void main(String[] $) { CF256B o = new CF256B(); o.main(); o.flush(); } long count(int n, int x, int y, int r) { long a = 2L * r * r + 2 * r + 1; int w; if ((w = 1 - (x - r)) > 0) a -= (long) w * w; if ((w = 1 - (y - r)) > 0) a -= (long) w * w; if ((w = (x + r) - n) > 0) a -= (long) w * w; if ((w = (y + r) - n) > 0) a -= (long) w * w; if ((w = r - 1 - (x - 1) - (y - 1)) > 0) a += (long) w * (w + 1) / 2; if ((w = r - 1 - (x - 1) - (n - y)) > 0) a += (long) w * (w + 1) / 2; if ((w = r - 1 - (n - x) - (y - 1)) > 0) a += (long) w * (w + 1) / 2; if ((w = r - 1 - (n - x) - (n - y)) > 0) a += (long) w * (w + 1) / 2; return a; } void main() { int n = sc.nextInt(); int x = sc.nextInt(); int y = sc.nextInt(); int c = sc.nextInt(); int lower = -1, upper = c; while (upper - lower > 1) { int r = (lower + upper) / 2; if (count(n, x, y, r) >= c) upper = r; else lower = r; } println(upper); } }
logn
256_B. Mr. Bender and Square
CODEFORCES
import java.io.*; import java.math.BigInteger; import java.util.*; public class Proj implements Runnable { BufferedReader in; PrintWriter out; StringTokenizer str; public void solve() throws IOException { long l = nextLong(); long r = nextLong(); int g = 0; long x = l ^ r; long i = 1; while (x >= i) { i = i * 2; } if (x >= i) { out.println(x); } else out.println(i - 1); } public String nextToken() throws IOException { while (str == null || !str.hasMoreTokens()) { str = new StringTokenizer(in.readLine()); } return str.nextToken(); } public int nextInt() throws IOException { return Integer.parseInt(nextToken()); } public long nextLong() throws IOException { return Long.parseLong(nextToken()); } public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); in.close(); out.close(); } catch (IOException e) { } } public static void main(String[] args) { new Thread(new Proj()).start(); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
/** * Created with IntelliJ IDEA. * User: brzezinsky * Date: 12/16/12 * Time: 7:44 PM * To change this template use File | Settings | File Templates. */ import java.io.*; import java.util.*; import java.util.StringTokenizer; public class E extends Thread { public E(String inputFileName, String outputFileName) { try { if (inputFileName != null) { this.input = new BufferedReader(new FileReader(inputFileName)); } else { this.input = new BufferedReader(new InputStreamReader(System.in)); } if (outputFileName != null) { this.output = new PrintWriter(outputFileName); } else { this.output = new PrintWriter(System.out); } this.setPriority(Thread.MAX_PRIORITY); } catch (Throwable e) { System.err.println(e.getMessage()); e.printStackTrace(); System.exit(666); } } private void solve() throws Throwable { long l = nextLong(), r = nextLong(); output.println(Math.max(Long.highestOneBit(l ^ r) * 2 - 1, 0L)); } public void run() { try { solve(); } catch (Throwable e) { System.err.println(e.getMessage()); e.printStackTrace(); System.exit(666); } finally { output.close(); } } public static void main(String... args) { new E(null, null).start(); } private int nextInt() throws IOException { return Integer.parseInt(next()); } private double nextDouble() throws IOException { return Double.parseDouble(next()); } private long nextLong() throws IOException { return Long.parseLong(next()); } private String next() throws IOException { while (tokens == null || !tokens.hasMoreTokens()) { tokens = new StringTokenizer(input.readLine()); } return tokens.nextToken(); } private StringTokenizer tokens; private BufferedReader input; private PrintWriter output; }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.awt.Point; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.StringTokenizer; import static java.lang.Math.*; import static java.lang.Integer.*; import static java.lang.Long.*; import static java.lang.Character.*; import static java.lang.String.*; @SuppressWarnings("unused") public class round169D { static PrintWriter out = new PrintWriter(System.out); static BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); static StringTokenizer st = new StringTokenizer(""); static int nextInt() throws Exception { return Integer.parseInt(next()); } static String next() throws Exception { while (true) { if (st.hasMoreTokens()) { return st.nextToken(); } String s = br.readLine(); if (s == null) { return null; } st = new StringTokenizer(s); } } public static void main(String[] args)throws Exception { long l = parseLong(next()); long r = parseLong(next()); long [] min = new long [61]; for(int i = 1 ; i <= 60 ; ++i){//(2^i)-1 is obtained by min[i]^min[i]+1 min[i] = (long) pow(2, i - 1) - 1; } for(int i = 60 ; i >= 0 ; --i){//try to get 2^i-1 as answer. if(min[i] >= r) continue; if(min[i] >= l && min[i] + 1 <= r){ out.println((1L << i) - 1); out.flush(); return; } if(min[i] < l){ long one_jump = (long) pow(2, i); long jumps = (long) ceil((l - min[i]) / (one_jump * 1.0)); long cur = min[i] + (jumps * one_jump); if(cur >= l && cur + 1 <= r){ out.println((1L << i) - 1); out.flush(); return; } } } out.println(0); out.flush(); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.Scanner; public class D { static long max; public static long[] getOneRange(long min, long max,int bit) { long st=(min&(((1l<<63)-1)&~((1l<<(bit+1))-1)))|(1l<<bit); long end=st|((1l<<(bit+1))-1); long interSt=Math.max(st,min); long interEnd=Math.min(end, max); if(interSt>interEnd) return null; return new long[]{interSt,interEnd}; } public static long[] getZeroRange(long min, long max,int bit) { long st=min&(((1l<<63)-1)&~((1l<<(bit+1))-1)); long end=st|((1l<<bit)-1); long interSt=Math.max(st,min); long interEnd=Math.min(end, max); if(interSt>interEnd) return null; return new long[]{interSt,interEnd}; } public static void solve(int bitPosition, long min1, long max1, long min2, long max2, long curNum) { if (bitPosition == -1) { max = Math.max(max, curNum); return; } long[] firZeroRange = getZeroRange(min1, max1,bitPosition); long[] secZeroRange = getZeroRange(min2, max2,bitPosition); long[] firOneRange = getOneRange(min1, max1,bitPosition); long[] secOneRange = getOneRange(min2, max2,bitPosition); if ((firOneRange != null && secZeroRange != null) || (firZeroRange != null && secOneRange != null)) { long newNum = curNum | (1l << bitPosition); if (firOneRange != null && secZeroRange != null&& (firOneRange[1]-firOneRange[0]+1)==1l<<bitPosition&& (secZeroRange[1]-secZeroRange[0]+1)==1l<<bitPosition) { solve(bitPosition - 1, firOneRange[0], firOneRange[1], secZeroRange[0], secZeroRange[1], newNum); return; } if (firZeroRange != null && secOneRange != null&& (firZeroRange[1]-firZeroRange[0]+1)==1l<<bitPosition&& (secOneRange[1]-secOneRange[0]+1)==1l<<bitPosition) { solve(bitPosition - 1, firZeroRange[0], firZeroRange[1], secOneRange[0], secOneRange[1], newNum); return; } if (firOneRange != null && secZeroRange != null) { solve(bitPosition - 1, firOneRange[0], firOneRange[1], secZeroRange[0], secZeroRange[1], newNum); } if (firZeroRange != null && secOneRange != null) { solve(bitPosition - 1, firZeroRange[0], firZeroRange[1], secOneRange[0], secOneRange[1], newNum); } } else { solve(bitPosition - 1, min1, max1, min2, max2, curNum); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long l = sc.nextLong(); long r = sc.nextLong(); max = 0; solve(62, l, r, l, r, 0); System.out.println(max); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.*; import java.io.*; public class a { public static void main(String[] args) throws IOException { //Scanner input = new Scanner(new File("input.txt")); //PrintWriter out = new PrintWriter(new File("output.txt")); PrintWriter out = new PrintWriter(System.out); input.init(System.in); long a = input.nextLong(), b = input.nextLong(); if(a==b) { out.println(0); out.close(); return; } long res = 0; for(int i = 0; i<63; i++) { if(a%(1l<<i) >= b%(1l<<i)) res += (1l<<i); else if(b/((1l<<i)) > a/((1l<<i))) res += (1l<<i); } out.println(res); out.close(); } public static long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a%b); } public static class input { static BufferedReader reader; static StringTokenizer tokenizer; /** call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input) ); tokenizer = new StringTokenizer(""); } /** get next word */ static String next() throws IOException { while ( ! tokenizer.hasMoreTokens() ) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(); } static int nextInt() throws IOException { return Integer.parseInt( next() ); } static double nextDouble() throws IOException { return Double.parseDouble( next() ); } static long nextLong() throws IOException { return Long.parseLong( next() ); } } static class IT { int[] left,right, val, a, b; IT(int n) { left = new int[3*n]; right = new int[3*n]; val = new int[3*n]; a = new int[3*n]; b = new int[3*n]; init(0,0, n); } int init(int at, int l, int r) { a[at] = l; b[at] = r; if(l==r) left[at] = right [at] = -1; else { int mid = (l+r)/2; left[at] = init(2*at+1,l,mid); right[at] = init(2*at+2,mid+1,r); } return at++; } //return the sum over [x,y] int get(int x, int y) { return go(x,y, 0); } int go(int x,int y, int at) { if(at==-1) return 0; if(x <= a[at] && y>= b[at]) return val[at]; if(y<a[at] || x>b[at]) return 0; return go(x, y, left[at]) + go(x, y, right[at]); } //add v to elements x through y void add(int x, int y, int v) { go3(x, y, v, 0); } void go3(int x, int y, int v, int at) { if(at==-1) return; if(y < a[at] || x > b[at]) return; val[at] += (Math.min(b[at], y) - Math.max(a[at], x) + 1)*v; go3(x, y, v, left[at]); go3(x, y, v, right[at]); } } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.InputStreamReader; import java.io.IOException; import java.util.InputMismatchException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author Zakhar Voit */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { public void solve(int testNumber, Scanner in, PrintWriter out) { out.println(solve(in.nextLong(), in.nextLong())); /*for (long l = 1; l < 500; l++) { for (long r = l; r < 500; r++) { if (badSolve(l, r) != solve(l, r)) { out.println(l + " " + r); return; } } } out.println("OK");*/ } long solve(long l, long r) { if (l == r) return 0; long ans = l ^ (l + 1); for (int i = 0; i < 62; i++) { l |= (1l << i); if (l + 1 <= r) ans = (1l << (i + 2l)) - 1; } return ans; } } class Scanner { BufferedReader in; StringTokenizer tok; public Scanner(InputStream in) { this.in = new BufferedReader(new InputStreamReader(in)); tok = new StringTokenizer(""); } public String nextToken() { if (!tok.hasMoreTokens()) { try { String newLine = in.readLine(); if (newLine == null) throw new InputMismatchException(); tok = new StringTokenizer(newLine); } catch (IOException e) { throw new InputMismatchException(); } return nextToken(); } return tok.nextToken(); } public long nextLong() { return Long.parseLong(nextToken()); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.*; import java.util.*; public class D { BufferedReader br; PrintWriter out; StringTokenizer st; boolean eof; String toStr(long a) { String s = Long.toBinaryString(a); while (s.length() < 64) s = "0" + s; return s; } void solve() throws IOException { long a = nextLong(); long b = nextLong(); String sa = toStr(a); String sb = toStr(b); int i = 0; while (i < 64 && sa.charAt(i) == sb.charAt(i)) i++; int left = 64 - i; out.println((1L << left) - 1); } D() throws IOException { br = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); } public static void main(String[] args) throws IOException { new D(); } String nextToken() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (Exception e) { eof = true; return null; } } return st.nextToken(); } String nextString() { try { return br.readLine(); } catch (IOException e) { eof = true; return null; } } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.Scanner; public class D { public static void main(String [] args){ Scanner in = new Scanner(System.in); long a = in.nextLong(); long b = in.nextLong(); String sa = Long.toBinaryString(a); String sb = Long.toBinaryString(b); long ans = 0; if(sb.length()-sa.length()>0){ for(int i = 0 ; i < sb.length() ; i++){ ans += 1l<<i; } System.out.println(ans); } else{ int num = -1 ; for(int i = 62 ; i >= 0 ; i--){ if((b & 1l << i) != 0 && ((~a) & (1l << i)) != 0){ num = i; break; } } ans = 0; if(num!=-1) for(int i = 0 ; i <= num ; i ++){ ans += 1l<<i; } System.out.println(ans); } } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.Scanner; public class Main3 { public static void main(String[] args) { Scanner s = new Scanner(System.in); long l = s.nextLong(); long r = s.nextLong(); String a = Long.toBinaryString(l); String b = Long.toBinaryString(r); while(a.length() < b.length()) a = "0" + a; while(b.length() < a.length()) b = "0" + b; String ans = ""; int ix = -1; for (int i = 0; i < a.length(); i++) { if(a.charAt(i) != b.charAt(i)){ break; } ans += a.charAt(i); ix++; } // System.out.println(a); // System.out.println(b); for (int i = ix + 1; i < a.length(); i++) { int c1 = a.charAt(i) - '0'; int c2 = b.charAt(i) - '0'; if(c1 == 0 && c2 == 0) ans += "1"; else if(c1 == 1 && c2 == 1) ans += "0"; else ans += (char)(c1 + '0'); } long a1 = Long.parseLong(ans, 2); long a2 = Long.parseLong(b,2); // System.out.println(ans); // System.out.println(b); // System.out.println(); long xor = a1 ^ a2; System.out.println(xor); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.Scanner; public class d_169 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long x=sc.nextLong(); long y=sc.nextLong(); String s=Long.toBinaryString(x); String p=Long.toBinaryString(y); int id=p.length()-s.length(); for (int i =1; i <=id; i++) { s="0"+s; } if(x==y){ System.out.println(0); return; } for (int i = 0; i <p.length(); i++) { if(s.charAt(i)!=p.charAt(i)){ System.out.println((long)Math.pow(2, s.length()-i)-1); return; } } } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.*; import java.lang.reflect.*; public class D { final int MOD = (int)1e9 + 7; final double eps = 1e-12; final int INF = (int)1e9; public D () { long L = sc.nextLong(); long R = sc.nextLong(); for (int i = 60; i >= 0; --i) { long b = (1L << i); long A = (L & b), B = (R & b); if (A != B) exit(2*b-1); } exit(0); } //////////////////////////////////////////////////////////////////////////////////// /* Dear hacker, don't bother reading below this line, unless you want to help me debug my I/O routines :-) */ static MyScanner sc = new MyScanner(); static class MyScanner { public String next() { newLine(); return line[index++]; } public char nextChar() { return next().charAt(0); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } public double nextDouble() { return Double.parseDouble(next()); } public String nextLine() { line = null; return readLine(); } public String [] nextStrings() { line = null; return readLine().split(" "); } public char [] nextChars() { return next().toCharArray(); } public Integer [] nextInts() { String [] L = nextStrings(); Integer [] res = new Integer [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Integer.parseInt(L[i]); return res; } public Long [] nextLongs() { String [] L = nextStrings(); Long [] res = new Long [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Long.parseLong(L[i]); return res; } public Double [] nextDoubles() { String [] L = nextStrings(); Double [] res = new Double [L.length]; for (int i = 0; i < L.length; ++i) res[i] = Double.parseDouble(L[i]); return res; } public String [] next (int N) { String [] res = new String [N]; for (int i = 0; i < N; ++i) res[i] = sc.next(); return res; } public Integer [] nextInt (int N) { Integer [] res = new Integer [N]; for (int i = 0; i < N; ++i) res[i] = sc.nextInt(); return res; } public Long [] nextLong (int N) { Long [] res = new Long [N]; for (int i = 0; i < N; ++i) res[i] = sc.nextLong(); return res; } public Double [] nextDouble (int N) { Double [] res = new Double [N]; for (int i = 0; i < N; ++i) res[i] = sc.nextDouble(); return res; } public String [][] nextStrings (int N) { String [][] res = new String [N][]; for (int i = 0; i < N; ++i) res[i] = sc.nextStrings(); return res; } public Integer [][] nextInts (int N) { Integer [][] res = new Integer [N][]; for (int i = 0; i < N; ++i) res[i] = sc.nextInts(); return res; } public Long [][] nextLongs (int N) { Long [][] res = new Long [N][]; for (int i = 0; i < N; ++i) res[i] = sc.nextLongs(); return res; } public Double [][] nextDoubles (int N) { Double [][] res = new Double [N][]; for (int i = 0; i < N; ++i) res[i] = sc.nextDoubles(); return res; } ////////////////////////////////////////////// private boolean eol() { return index == line.length; } private String readLine() { try { return r.readLine(); } catch (Exception e) { throw new Error(e); } } private final BufferedReader r; MyScanner () { this(new BufferedReader(new InputStreamReader(System.in))); } MyScanner(BufferedReader r) { try { this.r = r; while (!r.ready()) Thread.sleep(1); start(); } catch (Exception e) { throw new Error(e); } } private String [] line; private int index; private void newLine() { if (line == null || eol()) { line = readLine().split(" "); index = 0; } } } static void print(Object o, Object... a) { printDelim(" ", o, a); } static void cprint(Object o, Object... a) { printDelim("", o, a); } static void printDelim (String delim, Object o, Object... a) { pw.println(build(delim, o, a)); } static void exit (Object o, Object... a) { print(o, a); exit(); } static void exit () { pw.close(); System.out.flush(); System.err.println("------------------"); System.err.println("Time: " + ((millis() - t) / 1000.0)); System.exit(0); } void NO() { throw new Error("NO!"); } //////////////////////////////////////////////////////////////////////////////////// static String build(String delim, Object o, Object... a) { StringBuilder b = new StringBuilder(); append(b, o, delim); for (Object p : a) append(b, p, delim); return b.toString().trim(); } static void append(StringBuilder b, Object o, String delim) { if (o.getClass().isArray()) { int L = Array.getLength(o); for (int i = 0; i < L; ++i) append(b, Array.get(o, i), delim); } else if (o instanceof Iterable<?>) { for (Object p : (Iterable<?>)o) append(b, p, delim); } else b.append(delim).append(o); } //////////////////////////////////////////////////////////////////////////////////// public static void main(String[] args) { new D(); exit(); } static void start() { t = millis(); } static PrintWriter pw = new PrintWriter(System.out); static long t; static long millis() { return System.currentTimeMillis(); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.*; import java.util.*; public class D { public static void main(String[] args) throws IOException { new D().solve(); } void solve() throws IOException { Scanner sc = new Scanner(System.in); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String[] sp; long L = sc.nextLong(); long R = sc.nextLong(); if (L == R) { System.out.println(0); } else { System.out.println(Long.highestOneBit(R ^ L) * 2 - 1); } } } // // // // // // // // // // // // // // // // // // // // // // // // // // // // // //
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
// @author Sanzhar import java.io.*; import java.util.*; import java.awt.Point; public class Template { BufferedReader in; PrintWriter out; StringTokenizer st; String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (Exception e) { } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } public void run() throws Exception { //in = new BufferedReader(new FileReader("input.txt")); //out = new PrintWriter(new FileWriter("output.txt")); in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.flush(); out.close(); in.close(); } public void solve() throws Exception { long l = nextLong(); long r = nextLong(); long[] x = new long[100]; int kx = 0; while (r > 0) { x[kx++] = r % 2; r /= 2; } long[] y = new long[100]; int ky = 0; while (l > 0) { y[ky++] = l % 2; l /= 2; } long[] ans = new long[100]; boolean ok = false; for (int k = kx - 1; k >= 0; k--) { if (ok) { ans[k] = 1; continue; } if (x[k] > y[k]) { ans[k] = 1; ok = true; } } long a = 0; long p2 = 1; for (int i = 0; i < kx; i++) { a += p2 * ans[i]; p2 *= 2; } out.println(a); } public static void main(String[] args) throws Exception { new Template().run(); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.InputMismatchException; import java.io.OutputStream; import java.io.PrintWriter; import java.math.BigInteger; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { long l = in.readLong(), r = in.readLong(); int pow = 62; long mask = 1L << pow; while (((r | l) & mask) == 0){ pow--; mask = 1L << pow; } while (true) { if (((r ^ l) & mask) == mask || pow < 0) { break; } mask >>= 1; l = l & ~(1L << pow); r = r & ~(1L << pow); pow--; } pow++; out.print((1L << pow) - 1); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public long readLong() { 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 static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.Scanner; public class D { public static void main(String[] args) { Scanner s = new Scanner(System.in); long l = s.nextLong(); long r = s.nextLong(); long a = l ^ r; long b = a; while (b != 0) { a = b; b = (b-1) & b; } if (a != 0) { a = (a << 1) - 1; } System.out.println(a); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.*; public class Dj { public static long run(long l, long r) { if(l == r) { return 0; } long[] sq2 = new long[62]; sq2[0] = 1; for(int i = 1; i < 62; i++) sq2[i] = sq2[i-1]*2; //System.out.println(sq2[61]); for(int i = sq2.length - 1; i >= 0; i--) { //log("L = " + l + " R = " + r + " 2^" + i + "=" + sq2[i]); if(l >= sq2[i] && r >= sq2[i]) { l -= sq2[i]; r -= sq2[i]; } else if(l < sq2[i] && sq2[i] <= r) { break; } } for(int i = sq2.length - 1; i >= 0; i--) { //log("L = " + l + " R = " + r + " 2^" + i + "=" + sq2[i]); if(l < sq2[i] && sq2[i] <= r) { return sq2[i+1]-1; } } return -1; } public static void log(String str) { System.out.println(str); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); long l = sc.nextLong(); long r = sc.nextLong(); System.out.println(run(l, r)); //System.out.println(run(9999999999998l, 9999999999999l)); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.math.BigInteger; import java.util.StringTokenizer; public class LittleGirlAndXor { static long L, R; static Long[][][][][] dp = new Long[64][2][2][2][2]; public static long go(int index, int low1, int high1, int low2, int high2) { if (index == -1) { return 0; } if (dp[index][low1][high1][low2][high2] != null) return dp[index][low1][high1][low2][high2]; int bit1 = (L & (1L << index)) == 0 ? 0 : 1; int bit2 = (R & (1L << index)) == 0 ? 0 : 1; long res = 0; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { int nl1 = low1, nh1 = high1, nl2 = low2, nh2 = high2; boolean can = true; if (low1 == 0) { if (i == bit1) { nl1 = 0; } else if (i < bit1) { can = false; } else if (i > bit1) { nl1 = 1; } } if (high1 == 0) { if (i == bit2) { nh1 = 0; } else if (i < bit2) { nh1 = 1; } else if (i > bit2) { can = false; } } if (low2 == 0) { if (j == bit1) { nl2 = 0; } else if (j < bit1) { can = false; } else if (j > bit1) { nl2 = 1; } } if (high2 == 0) { if (j == bit2) { nh2 = 0; } else if (j < bit2) { nh2 = 1; } else if (j > bit2) { can = false; } } if (can){ long xor = i^j; res = Math.max(res, (xor<<index)+go(index - 1, nl1, nh1, nl2, nh2)); } } } return dp[index][low1][high1][low2][high2] = res; } public static void main(String[] args) { InputReader r = new InputReader(System.in); L = r.nextLong(); R = r.nextLong(); System.out.println(go(63,0,0,0,0)); } static class InputReader { private BufferedReader reader; private StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream)); tokenizer = null; } public String nextLine() { try { return reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.*; import java.math.BigInteger; import java.util.*; import java.text.*; public class cf276d { static BufferedReader br; static Scanner sc; static PrintWriter out; public static void initA() { try { br = new BufferedReader(new InputStreamReader(System.in)); //br = new BufferedReader(new FileReader("input.txt")); sc = new Scanner(System.in); //out = new PrintWriter("output.txt"); out = new PrintWriter(System.out); } catch (Exception e) { } } public static void initB() { try { br = new BufferedReader(new FileReader("input.txt")); sc = new Scanner(new FileReader("input.txt")); out = new PrintWriter("output.txt"); } catch (Exception e) { } } public static String getString() { try { return br.readLine(); } catch (Exception e) { } return ""; } public static Integer getInt() { try { return Integer.parseInt(br.readLine()); } catch (Exception e) { } return 0; } public static Integer[] getIntArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); Integer temp2[] = new Integer[n]; for (int i = 0; i < n; i++) { temp2[i] = Integer.parseInt(temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static Long[] getLongArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); Long temp2[] = new Long[n]; for (int i = 0; i < n; i++) { temp2[i] = Long.parseLong(temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static String[] getStringArr() { try { StringTokenizer temp = new StringTokenizer(br.readLine()); int n = temp.countTokens(); String temp2[] = new String[n]; for (int i = 0; i < n; i++) { temp2[i] = (temp.nextToken()); } return temp2; } catch (Exception e) { } return null; } public static int getMax(Integer[] ar) { int t = ar[0]; for (int i = 0; i < ar.length; i++) { if (ar[i] > t) { t = ar[i]; } } return t; } public static void print(Object a) { out.println(a); } public static int nextInt() { return sc.nextInt(); } public static double nextDouble() { return sc.nextDouble(); } public static void main(String[] ar) { initA(); solve(); out.flush(); } public static void print2(Object o){System.out.println(o);} public static void solve() { Long xx[] = getLongArr(); long l = xx[0]; long r = xx[1]; BigInteger a = BigInteger.valueOf(l); BigInteger b = BigInteger.valueOf(r); if(l==r){ print(0);return; } String a2 = a.toString(2); String b2 = b.toString(2); int selisihpjg = Math.abs(a2.length() - b2.length()); while (selisihpjg-- > 0) { a2 = "0" + a2; //print2("wewe"); } //print2(a2); //print2(b2); String out = ""; for (int i = 0; i < b2.length(); i++) { //print2("i="+i); if (a2.charAt(i) != b2.charAt(i)) { for (int ii = i; ii < b2.length(); ii++) { out += "1"; } //print2(out); print2(new BigInteger(out, 2)); return; } } } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.*; import java.io.*; import java.math.*; import java.awt.geom.*; import static java.lang.Math.*; public class Solution implements Runnable { boolean hasBit(long n, int pos) { return (n & (1L << pos)) != 0; } public void solve() throws Exception { long l = sc.nextLong(), r = sc.nextLong(); int bit = 62; while (bit >= 0 && (hasBit(l, bit) == hasBit(r, bit))) { bit--; } out.println((1L << (bit + 1)) - 1); } static Throwable uncaught; BufferedReader in; FastScanner sc; PrintWriter out; @Override public void run() { try { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); sc = new FastScanner(in); solve(); } catch (Throwable uncaught) { Solution.uncaught = uncaught; } finally { out.close(); } } public static void main(String[] args) throws Throwable { Thread thread = new Thread (null, new Solution(), "", (1 << 26)); thread.start(); thread.join(); if (Solution.uncaught != null) { throw Solution.uncaught; } } } class FastScanner { BufferedReader in; StringTokenizer st; public FastScanner(BufferedReader in) { this.in = in; } public String nextToken() throws Exception { while (st == null || !st.hasMoreTokens()) { st = new StringTokenizer(in.readLine()); } return st.nextToken(); } public int nextInt() throws Exception { return Integer.parseInt(nextToken()); } public long nextLong() throws Exception { return Long.parseLong(nextToken()); } public double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.*; import static java.lang.System.*; public class D276 { Scanner sc = new Scanner(in); public void run() { long l=sc.nextLong(),r=sc.nextLong(); long tes=l^r; int d=0; while(tes!=0){ tes/=2; d++; } ln((1L<<d)-1); } public static void main(String[] _) { new D276().run(); } public static void pr(Object o) { out.print(o); } public static void ln(Object o) { out.println(o); } public static void ln() { out.println(); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
//package codeforces; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.io.StreamTokenizer; import java.util.Scanner; public class CodeForces { //private static MyScanner sc; private static MyPrinter out; public static void solve() throws IOException { Scanner sc = new Scanner(System.in); long l = sc.nextLong(); long r = sc.nextLong(); String ls = Long.toBinaryString(l); String rs = Long.toBinaryString(r); while (ls.length() < rs.length()) { ls = "0" + ls; } String res = ""; boolean ok = false; for (int i = 0; i < ls.length(); i++) { if (ok) { res += "1"; } else { if (ls.charAt(i) != rs.charAt(i)) { res += "1"; ok = true; } } } long all = 0; for (int i = 0; i < res.length(); i++) { all += (long) Math.pow((long) 2, (long) res.length() - 1 - i); } System.out.println(all); } public static void main(String[] args) throws IOException { //sc = new MyScanner(System.in); out = new MyPrinter(System.out); solve(); out.close(); } } class MyScanner { private StreamTokenizer st; public MyScanner(InputStream is) { st = new StreamTokenizer(new BufferedReader(new InputStreamReader(is))); } public MyScanner(File f) throws FileNotFoundException { st = new StreamTokenizer(new BufferedReader(new FileReader(f))); } public int nextInt() throws IOException { st.nextToken(); return ((int) st.nval); } public double nextDouble() throws IOException { st.nextToken(); return (st.nval); } public String nextString() throws IOException { st.nextToken(); if (st.ttype == StreamTokenizer.TT_WORD) { return (st.sval); } else { return ("not found"); } } } class MyPrinter { private BufferedWriter out; public MyPrinter(OutputStream os) { out = new BufferedWriter(new PrintWriter(os)); } public MyPrinter(File f) throws IOException { out = new BufferedWriter(new FileWriter(f)); } public void println(int i) throws IOException { out.write(Integer.toString(i)); out.newLine(); } public void println(double d) throws IOException { out.write(Double.toString(d)); out.newLine(); } public void println(long l) throws IOException { out.write(Long.toString(l)); out.newLine(); } public void println(String s) throws IOException { out.write(s); out.newLine(); } public void println(char c) throws IOException { out.write(Character.toString(c)); out.newLine(); } public void print(int i) throws IOException { out.write(Integer.toString(i)); } public void print(double d) throws IOException { out.write(Double.toString(d)); } public void print(long l) throws IOException { out.write(Long.toString(l)); } public void print(String s) throws IOException { out.write(s); } public void print(char c) throws IOException { out.write(Character.toString(c)); } public void close() throws IOException { out.flush(); out.close(); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.*; import java.io.*; public class Main { BufferedReader in; StringTokenizer str = null; private String next() throws Exception{ if (str == null || !str.hasMoreElements()) str = new StringTokenizer(in.readLine()); return str.nextToken(); } private int nextInt() throws Exception{ return Integer.parseInt(next()); } private long nextLong() throws Exception{ return Long.parseLong(next()); } private double nextDouble() throws Exception{ return Double.parseDouble(next()); } public void run() throws Exception{ in = new BufferedReader(new InputStreamReader(System.in)); long l = nextLong(); long r = nextLong(); int bit = 63; while(bit >= 0 && (hasBit(l, bit) == hasBit(r, bit))) { bit--; } System.out.println((1L<<bit+1)-1); } private boolean hasBit(long x, int i){ return (x & (1L<<i)) > 0; } public static void main(String[] args) throws Exception{ new Main().run(); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
/* Codeforces Template */ import java.io.*; import java.util.*; import static java.lang.Math.*; import static java.util.Arrays.fill; import static java.util.Arrays.binarySearch; import static java.util.Arrays.sort; public class Main { static long initTime; static final Random rnd = new Random(7777L); static boolean writeLog = false; public static void main(String[] args) throws IOException { initTime = System.currentTimeMillis(); try { writeLog = "true".equals(System.getProperty("LOCAL_RUN_7777")); } catch (SecurityException e) {} new Thread(null, new Runnable() { public void run() { try { try { if (new File("input.txt").exists()) System.setIn(new FileInputStream("input.txt")); } catch (SecurityException e) {} long prevTime = System.currentTimeMillis(); new Main().run(); log("Total time: " + (System.currentTimeMillis() - prevTime) + " ms"); log("Memory status: " + memoryStatus()); } catch (IOException e) { e.printStackTrace(); } } }, "1", 1L << 24).start(); } void run() throws IOException { in = new BufferedReader(new InputStreamReader(System.in)); out = new PrintWriter(System.out); solve(); out.close(); in.close(); } /*************************************************************** * Solution **************************************************************/ void solve() throws IOException { long leftBorder = nextLong(); long rightBorder = nextLong(); long[][][][][] dp = new long [64][2][2][2][2]; for (long[][][][] a : dp) for (long[][][] b : a) for (long[][] c : b) for (long[] d : c) fill(d, -1L); dp[63][0][0][0][0] = 0L; for (int lastBit = 63; lastBit > 0; lastBit--) { int curBit = lastBit - 1; int leftBit = (int) ((leftBorder >> curBit) & 1L); int rightBit = (int) ((rightBorder >> curBit) & 1L); for (int agl = 0; agl < 2; agl++) { for (int alr = 0; alr < 2; alr++) { for (int bgl = 0; bgl < 2; bgl++) { for (int blr = 0; blr < 2; blr++) { long prvXor = dp[lastBit][agl][alr][bgl][blr]; if (prvXor < 0L) continue; for (int ab = 0; ab < 2; ab++) { int nagl = left(agl, leftBit, ab); int nalr = right(alr, rightBit, ab); if (nagl < 0 || nalr < 0) continue; for (int bb = 0; bb < 2; bb++) { int nbgl = left(bgl, leftBit, bb); int nblr = right(blr, rightBit, bb); if (nbgl < 0 || nblr < 0) continue; long setBit = ab ^ bb; dp[curBit][nagl][nalr][nbgl][nblr] = max(dp[curBit][nagl][nalr][nbgl][nblr], prvXor | (setBit << curBit)); } } } } } } } long answer = -1L; for (int agl = 0; agl < 2; agl++) { for (int alr = 0; alr < 2; alr++) { for (int bgl = 0; bgl < 2; bgl++) { for (int blr = 0; blr < 2; blr++) { answer = max(answer, dp[0][agl][alr][bgl][blr]); } } } } // System.err.println(Long.toBinaryString(leftBorder)); // System.err.println(Long.toBinaryString(rightBorder)); // System.err.println(Long.toBinaryString(answer)); out.println(answer); } int left(int gl, int leftBit, int b) { if (gl == 0) { if (b < leftBit) return -1; if (b == leftBit) return 0; if (b > leftBit) return 1; } return 1; } int right(int lr, int rightBit, int b) { if (lr == 0) { if (b < rightBit) return 1; if (b == rightBit) return 0; if (b > rightBit) return -1; } return 1; } /*************************************************************** * Input **************************************************************/ BufferedReader in; PrintWriter out; StringTokenizer st = new StringTokenizer(""); String nextToken() throws IOException { while (!st.hasMoreTokens()) st = new StringTokenizer(in.readLine()); return st.nextToken(); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } int[] nextIntArray(int size) throws IOException { int[] ret = new int [size]; for (int i = 0; i < size; i++) ret[i] = nextInt(); return ret; } long[] nextLongArray(int size) throws IOException { long[] ret = new long [size]; for (int i = 0; i < size; i++) ret[i] = nextLong(); return ret; } double[] nextDoubleArray(int size) throws IOException { double[] ret = new double [size]; for (int i = 0; i < size; i++) ret[i] = nextDouble(); return ret; } String nextLine() throws IOException { st = new StringTokenizer(""); return in.readLine(); } boolean EOF() throws IOException { while (!st.hasMoreTokens()) { String s = in.readLine(); if (s == null) return true; st = new StringTokenizer(s); } return false; } /*************************************************************** * Output **************************************************************/ void printRepeat(String s, int count) { for (int i = 0; i < count; i++) out.print(s); } void printArray(int[] array) { if (array == null || array.length == 0) return; for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(long[] array) { if (array == null || array.length == 0) return; for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array) { if (array == null || array.length == 0) return; for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.print(array[i]); } out.println(); } void printArray(double[] array, String spec) { if (array == null || array.length == 0) return; for (int i = 0; i < array.length; i++) { if (i > 0) out.print(' '); out.printf(Locale.US, spec, array[i]); } out.println(); } void printArray(Object[] array) { if (array == null || array.length == 0) return; boolean blank = false; for (Object x : array) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } @SuppressWarnings("rawtypes") void printCollection(Collection collection) { if (collection == null || collection.isEmpty()) return; boolean blank = false; for (Object x : collection) { if (blank) out.print(' '); else blank = true; out.print(x); } out.println(); } /*************************************************************** * Utility **************************************************************/ static String memoryStatus() { return (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory() >> 20) + "/" + (Runtime.getRuntime().totalMemory() >> 20) + " MB"; } static void checkMemory() { System.err.println(memoryStatus()); } static long prevTimeStamp = Long.MIN_VALUE; static void updateTimer() { prevTimeStamp = System.currentTimeMillis(); } static long elapsedTime() { return (System.currentTimeMillis() - prevTimeStamp); } static void checkTimer() { System.err.println(elapsedTime() + " ms"); } static void chk(boolean f) { if (!f) throw new RuntimeException("Assert failed"); } static void chk(boolean f, String format, Object ... args) { if (!f) throw new RuntimeException(String.format(format, args)); } static void log(String format, Object ... args) { if (writeLog) System.err.println(String.format(Locale.US, format, args)); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.awt.Point; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.HashSet; import java.util.StringTokenizer; import static java.lang.Math.*; import static java.lang.Integer.*; import static java.lang.Long.*; import static java.lang.Character.*; import static java.lang.String.*; @SuppressWarnings("unused") public class round169D { static PrintWriter out = new PrintWriter(System.out); static BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); static StringTokenizer st = new StringTokenizer(""); static int nextInt() throws Exception { return Integer.parseInt(next()); } static String next() throws Exception { while (true) { if (st.hasMoreTokens()) { return st.nextToken(); } String s = br.readLine(); if (s == null) { return null; } st = new StringTokenizer(s); } } public static void main(String[] args)throws Exception { long l = parseLong(next()); long r = parseLong(next()); long [] min = new long [61]; for(int i = 1 ; i <= 60 ; ++i){//(2^i)-1 is obtained by min[i]^min[i]+1 min[i] = (long) pow(2, i - 1) - 1; } for(int i = 60 ; i >= 0 ; --i){//try to get 2^i-1 as answer. if(min[i] >= r) continue; if(min[i] >= l && min[i] + 1 <= r){ out.println((long) pow(2, i) - 1); out.flush(); return; } if(min[i] < l){ long one_jump = (long) pow(2, i); long jumps = (long) ceil((l - min[i]) / (one_jump * 1.0)); long cur = min[i] + (jumps * one_jump); if(cur >= l && cur + 1 <= r){ out.println((long) pow(2, i) - 1); out.flush(); return; } } } out.println(0); out.flush(); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.util.*; public class Main{ public static void main(String[]args){ for(Scanner cin=new Scanner(System.in);cin.hasNextLong();System.out.println(Math.max(0,(Long.highestOneBit(cin.nextLong()^cin.nextLong())<<1)-1))); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
//package round169; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; public class D2 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { long L = nl(), R = nl(); out.println(Math.max(0, Long.highestOneBit(L^R)*2-1)); } void run() throws Exception { is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); tr(System.currentTimeMillis()-s+"ms"); } public static void main(String[] args) throws Exception { new D2().run(); } private byte[] inbuf = new byte[1024]; private int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private int ni() { int num = 0, b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private boolean oj = System.getProperty("ONLINE_JUDGE") != null; private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.InputStreamReader; import java.io.IOException; import java.io.BufferedReader; import java.io.OutputStream; import java.io.PrintWriter; import java.util.StringTokenizer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author ocelopilli */ 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); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { long LL = in.nextLong(); long RR = in.nextLong(); long L = LL; long R = RR; long ans = 0L; long X = Long.highestOneBit( R ); while ( X > 0L && (L & X) == (R & X) ) X >>= 1; while ( X > 0L ) { long a = L & X; long b = R & X; if ( (a ^ b) == X ) ans |= X; else { if ( b == 0L ) { if ( (R | X) <= RR ) { R |= X; ans |= X; } else if ( (L | X) <= RR ) { L |= X; ans |= X; } } else { if ( (L ^ X) >= LL ) { L ^= X; ans |= X; } else if ( (R ^ X) >= LL ) { R ^= X; ans |= X; } } } X >>= 1; } out.println( ans ); } } class InputReader { BufferedReader br; StringTokenizer st; public InputReader(InputStream in) { br = new BufferedReader(new InputStreamReader(in)); st = null; } public String next() { while (st==null || !st.hasMoreTokens()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public long nextLong() { return Long.parseLong(next()); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.InputMismatchException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author George Marcus */ 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); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { public void solve(int testNumber, InputReader in, PrintWriter out) { long left = in.nextLong(); long right = in.nextLong(); long ans = go(left, right); out.println(ans); } private long go(long A, long B) { int bA = -1; for(int i = 62; i >= 0; i--) if((A & (1L << i)) > 0) { bA = i; break; } int bB = -1; for(int i = 62; i >= 0; i--) if((B & (1L << i)) > 0) { bB = i; break; } if(bB == -1) return 0; if(bA < bB) return allOne(bB); else return go(A ^ (1L << bA), B ^ (1L << bB)); } private long allOne(int bits) { long ret = 0; for(int i = 0; i <= bits; i++) ret |= (1L << i); return ret; } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public long nextLong() { return Long.parseLong(nextString()); } public String nextString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuffer res = new StringBuffer(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } private boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger; public class Main { public static void main(String [] args ) { try{ String str; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedOutputStream bos = new BufferedOutputStream(System.out); String eol = System.getProperty("line.separator"); byte [] eolb = eol.getBytes(); byte[] spaceb= " ".getBytes(); str = br.readLine(); int blank = str.indexOf( " "); long l = Long.parseLong(str.substring(0,blank)); long r = Long.parseLong(str.substring(blank+1)); String one = ""; String two = ""; while(l>0) { if((l%2)==0) { one = "0".concat(one); } else { one = "1".concat(one); } l/=2; } while(r>0) { if((r%2)==0) { two = "0".concat(two); } else { two = "1".concat(two); } r/=2; } while(one.length()<60) { one = "0".concat(one); } while(two.length()<60) { two = "0".concat(two); } int iter = 0; String xor = ""; boolean big = false; boolean small = false; while(one.charAt(iter) == two.charAt(iter)) { xor = xor.concat("0"); iter++; if(iter==60) { break; } } for(int i = iter ; i < 60 ; i++) { xor = xor.concat("1"); } bos.write(new BigInteger(xor,2).toString().getBytes()); bos.write(eolb); bos.flush(); } catch(IOException ioe) { ioe.printStackTrace(); } } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.*; import java.util.*; public class CFDiv1 { FastScanner in; PrintWriter out; boolean canBe(int from, long curVal, long l, long r) { if (curVal > r) return false; for (int i = 0; i <= from; i++) curVal += 1L << i; if (curVal >= l) return true; return false; } long stupid(long l, long r) { long ans = 0; for (long i = l; i <= r; i++) for (long j = l; j <= r; j++) ans = Math.max(ans, i ^ j); return ans; } long solve2(long l, long r) { long min = 0; long max = 0; for (int i = 62; i >= 0; i--) { boolean max0 = canBe(i - 1, max, l, r); boolean max1 = canBe(i - 1, max | (1L << i), l, r); boolean min0 = canBe(i - 1, min, l, r); boolean min1 = canBe(i - 1, min | (1L << i), l, r); if (max0 && min1 && max > min) { min = min | (1L << i); } else { if (max1 && min0) { max |= (1L << i); } else { if (max1 && min1) { max |= (1L << i); min |= 1L << i; } else { } } } } return min ^ max; } void solve() { out.println(solve2(in.nextLong(), in.nextLong())); // System.err.println(solve2(4, 9)); // Random rnd = new Random(312); // for (int test = 0; test < 10000; test++) { // System.err.println(test); // int l = rnd.nextInt(100); // int r = rnd.nextInt(100) + l; // long v1 = stupid(l, r); // long v2 = solve2(l, r); // if (v1 != v2) { // System.err.println(v1 + " " + v2); // System.err.println(l + " " + r); // throw new AssertionError(); // } // } } void run() { try { in = new FastScanner(new File("test.in")); out = new PrintWriter(new File("test.out")); solve(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } void runIO() { in = new FastScanner(System.in); out = new PrintWriter(System.out); solve(); out.close(); } class FastScanner { BufferedReader br; StringTokenizer st; public FastScanner(File f) { try { br = new BufferedReader(new FileReader(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public FastScanner(InputStream f) { br = new BufferedReader(new InputStreamReader(f)); } String next() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return null; st = new StringTokenizer(s); } return st.nextToken(); } boolean hasMoreTokens() { while (st == null || !st.hasMoreTokens()) { String s = null; try { s = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if (s == null) return false; st = new StringTokenizer(s); } return true; } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } } public static void main(String[] args) { new CFDiv1().runIO(); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
/** * Created with IntelliJ IDEA. * User: brzezinsky * Date: 12/16/12 * Time: 7:44 PM * To change this template use File | Settings | File Templates. */ import java.io.*; import java.util.*; import java.util.StringTokenizer; public class E extends Thread { public E(String inputFileName, String outputFileName) { try { if (inputFileName != null) { this.input = new BufferedReader(new FileReader(inputFileName)); } else { this.input = new BufferedReader(new InputStreamReader(System.in)); } if (outputFileName != null) { this.output = new PrintWriter(outputFileName); } else { this.output = new PrintWriter(System.out); } this.setPriority(Thread.MAX_PRIORITY); } catch (Throwable e) { System.err.println(e.getMessage()); e.printStackTrace(); System.exit(666); } } private void solve() throws Throwable { long l = nextLong(), r = nextLong(); int []bitL = new int[63]; int []bitR = new int[63]; int szL = doit(l, bitL); int szR = doit(r, bitR); int ret = szR; while (ret >= 0 && bitL[ret] == bitR[ret]) --ret; if (ret < 0) { output.println(0); } else { output.println((1L << (ret + 1)) - 1); } } static final int doit(long q, int []a) { int sz = 0; while (q != 0L) { a[sz++] = (int)(q &1L); q >>= 1; } return sz; } public void run() { try { solve(); } catch (Throwable e) { System.err.println(e.getMessage()); e.printStackTrace(); System.exit(666); } finally { output.close(); } } public static void main(String... args) { new E(null, null).start(); } private int nextInt() throws IOException { return Integer.parseInt(next()); } private double nextDouble() throws IOException { return Double.parseDouble(next()); } private long nextLong() throws IOException { return Long.parseLong(next()); } private String next() throws IOException { while (tokens == null || !tokens.hasMoreTokens()) { tokens = new StringTokenizer(input.readLine()); } return tokens.nextToken(); } private StringTokenizer tokens; private BufferedReader input; private PrintWriter output; }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.OutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.Writer; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top * @author emotionalBlind */ 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); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } } class TaskD { int[] lo; int[] hi; long[][][][][] dp; long[] posVal; long go(int pos, int isAGreaterLo, int isALessHi, int isBGreaterLo, int isBLessHi) { if (pos == 63) { return 0; } if (dp[pos][isAGreaterLo][isALessHi][isBGreaterLo][isBLessHi] != -1) { return dp[pos][isAGreaterLo][isALessHi][isBGreaterLo][isBLessHi]; } // range a int ua = 0; int va = 1; if (isALessHi == 0 && hi[pos] == 0) { va = 0; } if (isAGreaterLo == 0 && lo[pos] == 1) { ua = 1; } // range b; int ub = 0; int vb = 1; if (isBLessHi == 0 && hi[pos] == 0) { vb = 0; } if (isBGreaterLo == 0 && lo[pos] == 1) { ub = 1; } long res = 0; dp[pos][isAGreaterLo][isALessHi][isBGreaterLo][isBLessHi] = 0; for (int i = ua; i <= va; ++i) { int newIsAGreaterLo = isAGreaterLo; int newIsALessHi = isALessHi; if (i < hi[pos]) newIsALessHi = 1; if (i > lo[pos]) newIsAGreaterLo = 1; for (int j = ub; j <= vb; ++j) { int newIsBGreaterLo = isBGreaterLo; int newIsBLessHi = isBLessHi; if (j < hi[pos]) newIsBLessHi = 1; if (j > lo[pos]) newIsBGreaterLo = 1; long val = 0; if (i != j) val = posVal[pos]; val += go(pos + 1, newIsAGreaterLo, newIsALessHi, newIsBGreaterLo, newIsBLessHi); res = Math.max(res, val); } } dp[pos][isAGreaterLo][isALessHi][isBGreaterLo][isBLessHi] = res; return res; } public void solve(int testNumber, InputReader in, OutputWriter out) { lo = new int[63]; hi = new int[63]; long a = in.readLong(); long b = in.readLong(); Binary.convertBinary(a, lo); Binary.convertBinary(b, hi); posVal = new long[63]; posVal[62] = 1; for (int i = 61; i >= 0; --i) { posVal[i] = posVal[i + 1] * 2; } dp = new long[65][2][2][2][2]; for (long[][][][] a1 : dp) { for (long[][][] a2 : a1) { for (long[][] a3 : a2) { for (long[] a4 : a3) { Arrays.fill(a4, -1); } } } } long res = go(0, 0, 0, 0, 0); out.printLine(res); } } class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { // InputMismatchException -> UnknownError if (numChars == -1) throw new UnknownError(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new UnknownError(); } if (numChars <= 0) return -1; } return buf[curChar++]; } public long readLong() { 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 static boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } } class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(outputStream); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void print(Object...objects) { for (int i = 0; i < objects.length; i++) { if (i != 0) writer.print(' '); writer.print(objects[i]); } } public void printLine(Object...objects) { print(objects); writer.println(); } public void close() { writer.close(); } } class Binary { public static void convertBinary(long val, int[] a) { int last = a.length - 1; Arrays.fill(a, 0); while (val > 0) { a[last] = (int) (val % 2); last--; val /= 2; } } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES
import java.io.InputStream; import java.io.PrintStream; import java.util.Arrays; import java.util.Scanner; /** * @author Abhimanyu Singh * */ public class LittleGirlAndMaximumXOR { private InputStream input; private PrintStream output; private Scanner inputSc; static final boolean ONE = true; static final boolean ZERO = false; char dp[][][]; public LittleGirlAndMaximumXOR(InputStream input, PrintStream output) { this.input = input; this.output = output; init(); } private void init() { inputSc = new Scanner(input); } static int lineToInt(String line) { return Integer.parseInt(line); } public void solve() { solveTestCase(1); } void fill(char a[], long value) { String str = Long.toBinaryString(value); char array[] = str.toCharArray(); int len = array.length; int index = 63; while (len > 0) { a[index] = array[len - 1]; len--; index--; } } void init(char a[]) { for (int i = 0; i < 64; i++) { a[i] = '0'; } } /* char[] getMax0(char lBit[], char rBit[], int lIndex, int rIndex) { char ans[]=new char[64]; if(lIndex) } char[] getMax(char lBit[], char rBit[], int lIndex, int rIndex) { }*/ private void solveTestCase(int testN) { long l = inputSc.nextLong(); long r = inputSc.nextLong(); char lBit[] = new char[64]; char rBit[] = new char[64]; init(lBit); init(rBit); fill(lBit, l); fill(rBit, r); int i = 0; char ansBit[] = new char[64]; char a[] = new char[64]; char b[] = new char[64]; init(a); init(b); init(ansBit); for (; i < 64; i++) { if (lBit[i] == '0' && rBit[i] == '0') { } else if (lBit[i] == '1' && rBit[i] == '0') { throw new RuntimeException("Wrong Input"); } else if (lBit[i] == '0' && rBit[i] == '1') { a[i] = '0'; b[i] = '1'; break; } else { a[i] = '1'; b[i] = '1'; } } boolean aLessB = true; boolean aBig = false; boolean bSmall = false; for (; i < 64; i++) { if (lBit[i] == '0' && rBit[i] == '0') { a[i] = '1'; b[i] = '0'; aBig = true; } else if (lBit[i] == '1' && rBit[i] == '0') { a[i] = '1'; b[i] = '0'; } else if (lBit[i] == '0' && rBit[i] == '1') { a[i] = '0'; b[i] = '1'; } else { a[i] = '1'; b[i] = '0'; bSmall = true; } } for (i = 0; i < 64; i++) { if (a[i] == '0' && b[i] == '0') { ansBit[i] = '0'; } else if (a[i] == '1' && b[i] == '0') { ansBit[i] = '1'; } else if (a[i] == '0' && b[i] == '1') { ansBit[i] = '1'; } else { ansBit[i] = '0'; } } String ansStr = new String(ansBit); long ansValue = Long.parseLong(ansStr, 2); output.println(ansValue); } /** * @param args the command line arguments */ public static void main(String[] args) { LittleGirlAndMaximumXOR lgamx = new LittleGirlAndMaximumXOR(System.in, System.out); lgamx.solve(); } }
logn
276_D. Little Girl and Maximum XOR
CODEFORCES