HDU4919 Exclusive or(递推+记忆化搜索+大数)
发布时间:2020-12-14 02:34:12 所属栏目:大数据 来源:网络整理
导读:题目链接: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://ww
题目链接: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://www.voidcn.com/article/p-sgiykbig-po.html ? 代码如下: //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)); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容