Exclusive or

题目连接

  • 题意:
    每次给一个n,求
    (2≤n<10500)
  • 分析:
    先说一下自己的想法,如果将n换成二进制数,也就一两千位左右,那么一位一位处理是可以接受的。将0-n写成二进制形式后,显然所有数某一个二进制位是有一个循环节的,那么我们就可以从这里入手直接求解
import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    public static BigInteger zero = BigInteger.ZERO;
    public static BigInteger one = BigInteger.ONE;
    public static BigInteger two = BigInteger.valueOf(2);
    public static BigInteger three = BigInteger.valueOf(3);
    public static BigInteger four = BigInteger.valueOf(4);
    public static BigInteger six = BigInteger.valueOf(6);
    
    public static BigInteger Down(BigInteger now, BigInteger L) {
    	BigInteger mid = now.divide(L).multiply(L).add(L.shiftRight(1));
    	if (now.subtract(mid).signum() < 0)
    		return mid;
    	return mid.add(L.shiftRight(1));
    }
    
    public static BigInteger Up(BigInteger now, BigInteger L) {
    	BigInteger start = now.divide(L).multiply(L);
    	BigInteger mid = start.add(L.shiftRight(1));
    	if (now.subtract(mid).signum() < 0)
    		return start.subtract(one);
    	return mid.subtract(one);
    }
    
    public static int getValue(BigInteger now, BigInteger L) {
    	BigInteger mid = now.divide(L).multiply(L).add(L.shiftRight(1));
    	if (now.subtract(mid).signum() < 0)
    		return 0;
    	return 1;
    }
    
    public static BigInteger solve(BigInteger nl, BigInteger nr, BigInteger gl, BigInteger L) {
    	BigInteger ret = zero, step = Down(nl, L).subtract(nl), t = nr.subtract(Up(nr, L));
    	if (step.subtract(t).signum() > 0)
    		step = t;
    	while (nl.add(step).subtract(gl).signum() <= 0) {
    		if ((getValue(nl, L) ^ getValue(nr, L)) == 1)
    			ret = ret.add(step);
    		nl = nl.add(step); nr = nr.subtract(step);
    		step = Down(nl, L).subtract(nl); t = nr.subtract(Up(nr, L));
    		if (step.subtract(t).signum() > 0)
        		step = t;
    	}
    	if (gl.subtract(nl).add(one).signum() >= 0 && (getValue(nl, L) ^ getValue(nr, L)) == 1)
    		ret = ret.add(gl.subtract(nl).add(one));
    	return ret;
    }
    
    public static void main(String[] args) {
    	BigInteger n, L, tans, nl, ans;
    	Scanner cin = new Scanner(System.in);
    	while (cin.hasNext()) {
    		n = cin.nextBigInteger();
    		L = two;
    		ans = zero;
    		while (L.subtract(n.shiftLeft(1)).signum() <= 0)//(L <= n * 2)
    		{
    			tans = zero;
    			if (n.divide(L).shiftRight(1).signum() > 0) {
	    			tans = solve(zero, n, L.subtract(one), L);
	    		}
    			nl = n.divide(L).shiftRight(1).multiply(L);
    			tans = n.divide(L).shiftRight(1).multiply(tans).add(solve(nl, n.subtract(nl), n.subtract(one).shiftRight(1), L));
    			ans = ans.add(tans.multiply(L));
    			L = L.shiftLeft(1);
    		}
    		System.out.println(ans.subtract(n.shiftLeft(1)));
    	}
    }   
}

学习一下题解的方法,关键在于:(2 * k) ^ x = (2 * k + 1) ^ x
之后就学习一下题解的公式化简方法了


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

public class Main {
	static BigInteger n, ret;
	static BigInteger one = BigInteger.valueOf(1);
	static BigInteger two = BigInteger.valueOf(2);
	static BigInteger four = BigInteger.valueOf(4);
	static BigInteger six = BigInteger.valueOf(6);
	static HashMap<BigInteger, BigInteger> mp = new HashMap<BigInteger, BigInteger>();
	public static BigInteger fun(BigInteger n) {
		if (n.equals(BigInteger.ZERO) || n.equals(BigInteger.ONE)) 
			return BigInteger.ZERO;
		if (mp.containsKey(n))
			return mp.get(n);
		BigInteger k = n.shiftRight(1);
		if (n.testBit(0)) {
			ret = four.multiply(fun(k)).add(six.multiply(k));
			mp.put(n, ret);
			return ret;
		}
		else {
			ret = (fun(k).add(fun(k.subtract(one))).add(k.shiftLeft(1)).subtract(two)).shiftLeft(1);
			mp.put(n, ret);
			return ret;
		}
	}
	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			n = cin.nextBigInteger();
			mp.clear();
			System.out.println(fun(n));
		}
	}
}


用C语言解决下列问题:Kirill wants to weave the very beautiful blanket consisting of n×m of the same size square patches of some colors. He matched some non-negative integer to each color. Thus, in our problem, the blanket can be considered a B matrix of size n×m consisting of non-negative integers. Kirill considers that the blanket is very beautiful, if for each submatrix A of size 4×4 of the matrix B is true: A11⊕A12⊕A21⊕A22=A33⊕A34⊕A43⊕A44, A13⊕A14⊕A23⊕A24=A31⊕A32⊕A41⊕A42, where ⊕ means bitwise exclusive OR Kirill asks you to help her weave a very beautiful blanket, and as colorful as possible! He gives you two integers n and m . Your task is to generate a matrix B of size n×m , which corresponds to a very beautiful blanket and in which the number of different numbers maximized. Input The first line of input data contains one integer number t (1≤t≤1000 ) — the number of test cases. The single line of each test case contains two integers n and m (4≤n,m≤200) — the size of matrix B . It is guaranteed that the sum of n⋅m does not exceed 2⋅105 . Output For each test case, in first line output one integer cnt (1≤cnt≤n⋅m) — the maximum number of different numbers in the matrix. Then output the matrix B (0≤Bij<263) of size n×m . If there are several correct matrices, it is allowed to output any one. It can be shown that if there exists a matrix with an optimal number of distinct numbers, then there exists among suitable matrices such a B that (0≤Bij<263) .
03-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值