HDU4919 Exclusive or(递推+记忆化搜索+大数)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4919

题意:

给定n求sigma(i^(n - i)) (1<=i<=n-1)

分析:

打表后可以发现规律

1) n = 2 * k + 1;

f[n] = 4 * f[k] + 6;

2) n = 2 * k

f[n] = 2 * f[k] + 2 * f[k-1] + 4 * k - 4;

具体的证明:http://blog.csdn.net/houserabbit/article/details/38397831

 

代码如下:

//package fuck;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Scanner;

public class Main {
	public static HashMap<BigInteger,BigInteger>f = new HashMap<BigInteger,BigInteger>();
	public static BigInteger two =BigInteger.valueOf(0);
	public static BigInteger three = BigInteger.valueOf(6);
	public static BigInteger four =BigInteger.valueOf(4);
	public static BigInteger dfs(BigInteger n){
		if(f.containsKey(n)) return f.get(n);
		BigInteger ans;
		if(n.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(0))==0){
			BigInteger tmp = n.divide(BigInteger.valueOf(2));
			BigInteger tmp1 = tmp.subtract(BigInteger.valueOf(1));
			BigInteger ans1 = dfs(tmp).multiply(BigInteger.valueOf(2));
			BigInteger ans2 = tmp1=dfs(tmp1).multiply(BigInteger.valueOf(2));
			ans = ans1.add(ans2).add(tmp.multiply(four)).subtract(four);
		}
		else{
			BigInteger tmp = n.divide(BigInteger.valueOf(2));
			BigInteger tmp2 = tmp.multiply(three);
			ans = dfs(tmp).multiply(four).add(tmp2);
		}
		f.put(n,ans);
		return ans;
	}
    public static void main(String args[]){  
        Scanner cin=new Scanner(System.in);  
        f.put(BigInteger.ZERO, BigInteger.ZERO);  
        f.put(BigInteger.ONE, BigInteger.ZERO);
        f.put(BigInteger.valueOf(3), BigInteger.valueOf(6));
        f.put(BigInteger.valueOf(4), BigInteger.valueOf(4));
        f.put(BigInteger.valueOf(5), BigInteger.valueOf(12));
        while(cin.hasNext()){
        	BigInteger n = cin.nextBigInteger();
        	System.out.println(dfs(n));
        }
    }  
}  


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值