题目链接: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)); } } }
时间: 2024-11-29 10:05:45