欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java实现 洛谷 P1582 倒水

程序员文章站 2022-06-08 14:08:39
...

Java实现 洛谷 P1582 倒水

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Main {
	static long m = 0, f = 0;
	public static void main(String[] args) {
		FastScanner fs = new FastScanner();
		int a = fs.nextInt();
		int b = fs.nextInt();
		long x[] = new long[32];
		boolean y[] = new boolean[32];
		int n = 0;
		x[0] = 1;
		x[1] = 2;
		for (int i = 2; i < y.length; i++) {
			x[i] = 2 * x[i - 1];
		}
		for (int i = 31; i >= 0; i--) {
			if (a >= x[i]) {
				a -= x[i];
				y[i] = true;
				n++;
			}
		}
		if (n > b) {
			int d = n - b;
			for (int i = 0; i < y.length; i++) {
				if (y[i] && d == 0) {
					f = x[i];
					break;
				}
				if (y[i] && d != 0) {
					m += x[i];
					d--;
				}
			}
			System.out.println(f - m);
		} else {
			System.out.println(0);
		}
 
	}
 
	public static class FastScanner {
		private BufferedReader br;
		private StringTokenizer st;
 
		public FastScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
 
		public String nextToken() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				} catch (Exception e) {
 
				}
 
			}
			return st.nextToken();
		}
 
		public int nextInt() {
			return Integer.valueOf(nextToken());
		}
	}
 
}