码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-05-28 14:10:57      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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));
        }
    }  
}  


 

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

标签:

原文地址:http://blog.csdn.net/bigbigship/article/details/46120747

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!