【HDU4919】递推java大数 递归优化
来源:互联网 发布:小清新治愈电影知乎 编辑:程序博客网 时间:2024/06/05 19:07
Exclusive or
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 391 Accepted Submission(s): 167
Problem Description
Given n, find the value of
Note: ⊕ denotes bitwise exclusive-or.
Note: ⊕ denotes bitwise exclusive-or.
Input
The input consists of several tests. For each tests:
A single integer n (2≤n<10500).
A single integer n (2≤n<10500).
Output
For each tests:
A single integer, the value of the sum.
A single integer, the value of the sum.
Sample Input
34
Sample Output
64
Author
Xiaoxu Guo (ftiasch)
Source
2014 Multi-University Training Contest 5
现在写java都是先写成c++再对着翻译,先上一份,思路跟标程类似
c++
#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <list>#include <bitset>using namespace std ;#define zero {0}#define INF 0x3f3f3f3f#define EPS 1e-6#define TRUE true#define FALSE falsetypedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){ return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}#define N 100005int dfs(int n){ if (n <= 2) return 0; if (n == 3) return 6; int k; if (n & 1) { k = (n - 1) / 2; return 4 * dfs(k) + 6 * k; } else { k = n / 2; return 2 * dfs(k) + 2 * dfs(k - 1) + 4 * k - 4; }}int contribution(int n,int coe){ int result; if(n&1) { result=(n/2)*6; } else { result=n*2-4; } return result*coe;}struct node{ int a; int b;};int main(){#ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);#endif int n; while (scanf("%d", &n) + 1) { // printf("%d\n", dfs(n)); node val; val.a=1; val.b=0; int result=0; while(n) { result=result+contribution(n,val.a)+contribution(n+1,val.b); node new_nn; if(n&1) { n=n/2; new_nn.a=val.a*4+val.b*2; new_nn.b=val.b*2; } else { n=n/2-1; new_nn.a=val.a*2; new_nn.b=val.a*2+val.b*4; } val=new_nn; } printf("%d\n", result); } return 0;}java大数翻译
import java.io.*;import java.util.Scanner;import java.math.BigInteger;class Main{ public static class Node { BigInteger a; BigInteger b; } public static BigInteger get(BigInteger n, BigInteger coe ) { BigInteger result; if ((n.intValue() & 1) == 1) { result = n.divide(BigInteger.valueOf(2)).multiply(BigInteger.valueOf(6)); } else { result = n.multiply(BigInteger.valueOf(2)).add(BigInteger.valueOf(-4)); } return result.multiply(coe); } public static void main(String[]Args) throws FileNotFoundException { InputStream in=System.in; // InputStream in = new FileInputStream(new File("C:\\Users\\Sky\\Desktop\\1.in")); Scanner s = new Scanner(in); BigInteger n; while (s.hasNext()) { n = s.nextBigInteger(); Node val = new Node(); val.a = BigInteger.valueOf(1); val.b = BigInteger.valueOf(0); BigInteger result = BigInteger.ZERO; while (n.intValue() != 0) { result = result.add(get(n, val.a)).add(get(n.add(BigInteger.ONE), val.b)); Node new_val = new Node(); if ((n.intValue() & 1) == 1) { n = n.divide(BigInteger.valueOf(2)); new_val.a = val.a.multiply(BigInteger.valueOf(4)).add(val.b.multiply(BigInteger.valueOf(2))); new_val.b = val.b.multiply(BigInteger.valueOf(2)); } else { n = n.divide(BigInteger.valueOf(2)).add(BigInteger.ONE.negate()); new_val.a = val.a.multiply(BigInteger.valueOf(2)); new_val.b = val.a.multiply(BigInteger.valueOf(2)).add(val.b.multiply(BigInteger.valueOf(4))); } val=new_val; } System.out.println(result); } }}
直接递归写了发现很慢,后来听锋爷一个hashmap就过了~~O(∩_∩)O~~
c++版
#define DeBUG#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <vector>#include <stack>#include <queue>#include <string>#include <set>#include <sstream>#include <map>#include <list>#include <bitset>using namespace std ;#define zero {0}#define INF 0x3f3f3f3f#define EPS 1e-6#define TRUE true#define FALSE falsetypedef long long LL;const double PI = acos(-1.0);//#pragma comment(linker, "/STACK:102400000,102400000")inline int sgn(double x){ return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);}#define N 100005std::map<int, int> mp;int dfs(int n){ printf("%d\n", n); if(mp[n]!=0) return mp[n]; if (n <= 2) return 0; if (n == 3) return 6; int k; if (n & 1) { k = (n - 1) / 2; mp[n] = 4 * dfs(k) + 6 * k; return mp[n]; } else { k = n / 2; mp[n] = 2 * dfs(k) + 2 * dfs(k - 1) + 4 * k - 4; return mp[n]; }}int main(){#ifdef DeBUGs freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);#endif int n; while (scanf("%d", &n) + 1) { cout<<dfs(n)<<endl; } return 0;}
java版
import java.io.*;import java.math.BigInteger;import java.util.HashMap;import java.util.Scanner;class Main{public static BigInteger two = BigInteger.valueOf(2);public static BigInteger three = BigInteger.valueOf(3);public static BigInteger onefu = BigInteger.ONE.negate();public static HashMap<BigInteger, BigInteger>hash=new HashMap<BigInteger, BigInteger>();public static BigInteger dfs(BigInteger n){if(hash.get(n)!=null)return hash.get(n);if (n.compareTo(two) <= 0)return BigInteger.ZERO;if (n.compareTo(three) == 0)return BigInteger.valueOf(6);BigInteger k;if ((n.intValue() & 1) == 1){k = (n.add(onefu).divide(two));BigInteger now=BigInteger.valueOf(4).multiply(dfs(k)).add(BigInteger.valueOf(6).multiply(k));hash.put(n, now);return now;}else{k = n.divide(two);BigInteger now=two.multiply(dfs(k)).add(two.multiply(dfs(k.add(onefu)))).add(BigInteger.valueOf(4).multiply(k)).add(BigInteger.valueOf(-4));hash.put(n, now);return now;}}public static void main(String[] Args) throws FileNotFoundException{InputStream in = System.in;// InputStream in = new FileInputStream(new// File("C:\\Users\\Sky\\Desktop\\1.in"));Scanner s = new Scanner(in);BigInteger n;while (s.hasNext()){n = s.nextBigInteger();System.out.println(dfs(n));}}}
0 0
- 【HDU4919】递推java大数 递归优化
- HDU4919 Exclusive or(Java大数推公式)
- HDU4919 Exclusive or(递推+记忆化搜索+大数)
- 递推,递归及其优化
- 大菲波数(递推 + JAVA大数)
- poj 2506 递推+Java大数
- [POJ](2506)Tiling ---递推+Java大数
- 1sting(JAVA大数+ 递推)
- hdu6222(递推规律+java大数)
- HDU 5047 Sawtooth(大数优化+递推公式)
- 大数递推
- poj2506 大数递推
- 大数+递推
- UVA11375 递推+大数
- hdu 1865 1sting (java大数&递推)
- POJ 2506 Tiling(大数递推&&(数组模拟||JAVA))
- ACdream 1420 High Speed Trains【Java大数高精度 + 递推】
- HDU5047 Sawtooth 大数乘法|数学递推 Java&&C
- MMX Intrinsics各函数介绍
- 制作CYDIA发布源的DEB文件,详解!
- 空心三角形(2091)
- java中的printf
- 移动Android端视频通话的Java代码
- 【HDU4919】递推java大数 递归优化
- JS加载顺序
- java连接mysql增删改查测试通过
- hdu2444The Accomodation of Students
- bestcoder3(1003)hdu4909(状态压缩+乱搞)
- 三层架构(我的理解及详细分析)
- 面试笔记
- 指针的问题
- Hbase shell 常用命令