NKGIFTS - VOI08 Quà tết

Tác giả: khuc_tuan

Ngôn ngữ: Java

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
	
	static int[][] mh = {{0,-1},{1,0},{3,0},{2,-1}};
	static int[][] hm = { {0,0,1,0, 0,1,-1,-1}, {1,0,-1,-1, 0,1,-1,-1}, {0,0,1,0, 0,-1,1,0}, {1,0,-1,-1, 0,-1,1,0} };

	static long K, u, v, p, q;
	static BigInteger[] pow2;
	
	static void xet(long k, long u, long v, BigInteger vt) {
		if(k==0) {
			System.out.println(vt);
			return;
		}
		int t = (u>=(1L<<(k-1))) ? 1 : 0;
		if(v>=(1L<<(k-1))) t += 2;
		BigInteger nvt = BigInteger.ZERO;
		for(int tt=0;tt<mh[t][0];++tt) nvt = nvt.add(pow2[(int)((K-k)*2)]);
		if(mh[t][1]==-1) nvt = nvt.add(pow2[(int)(K-k)*2]).add(pow2[0]).subtract(vt);
		else nvt = nvt.add(vt);
		xet( k-1, hm[t][0]*(1L<<k)+hm[t][1]*(1L<<(k-1))+hm[t][2]*u+hm[t][3], 
		          hm[t][4]*(1L<<k)+hm[t][5]*(1L<<(k-1))+hm[t][6]*v+hm[t][7], nvt);
	}
	
	public static void main(String[] args) throws Exception {
		//BufferedReader kb = new BufferedReader(new InputStreamReader(System.in));
		Scanner sc = new Scanner(System.in);
		K = sc.nextLong();
		u = sc.nextLong();
		v = sc.nextLong();
		p = sc.nextLong();
		q = sc.nextLong();
		pow2 = new BigInteger[100];
		pow2[0] = BigInteger.ONE;
		for(int i=1;i<=80;++i) pow2[i] = pow2[i-1].add(pow2[i-1]);
		xet( K, u, v, pow2[0]);
		xet( K, p, q, pow2[0]);
	}
}

Download