HDU 4919 Exclusive or

18 篇文章 0 订阅

题意:

求题目中的式子 - -b

思路:

推递推公式  比赛时候队友就说数字上有关系  but没推出来 - -b  题解有过程:


推的过程中最巧妙的就是利用异或的性质  相邻两个数字相当于修改二进制最后两位  不过这样做通过异或出来的结果是相同的

题目中数字太大  用java比较好写  处理递推的问题常用记忆化搜索

代码:

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

public class Main {

	public static BigInteger two = BigInteger.valueOf(2);
	public static BigInteger four = BigInteger.valueOf(4);
	public static BigInteger six = BigInteger.valueOf(6);
	public static HashMap<BigInteger, BigInteger> map = new HashMap<BigInteger, BigInteger>();

	public static BigInteger F(BigInteger n) {
		if (map.containsKey(n))
			return map.get(n);
		BigInteger tmp1 = n.divide(two);
		BigInteger tmp2 = n.mod(two);
		BigInteger tmp3 = tmp1.subtract(BigInteger.ONE);
		if (tmp2.compareTo(BigInteger.ONE) == 0)
			tmp1 = F(tmp1).multiply(four).add(tmp1.multiply(six));
		else
			tmp1 = F(tmp1).multiply(two).add(F(tmp3).multiply(two))
					.add(tmp1.multiply(four)).subtract(four);
		map.put(n, tmp1);
		return tmp1;
	}

	public static void main(String[] args) {
		Scanner cin = new Scanner(System.in);
		map.put(BigInteger.ZERO, BigInteger.ZERO);
		map.put(BigInteger.ONE, BigInteger.ZERO);
		while (cin.hasNext()) {
			BigInteger n = cin.nextBigInteger();
			System.out.println(F(n));
		}
		cin.close();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值