Exclusive or
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 398 Accepted Submission(s): 173
Problem Description
Given n, find the value of
Note: ⊕ denotes bitwise exclusive-or.
Input
The input consists of several tests. For each tests:
A single integer n (2≤n<10500).
Output
For each tests:
A single integer, the value of the sum.
Sample Input
3
4
Sample Output
6
4
Author
Xiaoxu Guo (ftiasch)
题解中
若 n = 2k + 1,
f(n) = 4f(k) + 6k
若 n = 2k, f(n) = 2f(k) + 2f(k−1) + 4k−4.
然后在用java大数来写 ,递归的时候用map来记录已经到达的状态。
1 //package hdu4919; 2 3 import java.math.*; 4 import java.util.*; 5 6 public class Main { 7 Map<BigInteger,BigInteger> hashmap=new HashMap<BigInteger,BigInteger>(); 8 9 BigInteger dfs(BigInteger n){ 10 if(hashmap.get(n)!=null){ 11 return hashmap.get(n); 12 } 13 if(n.equals(BigInteger.valueOf(2))){ 14 return BigInteger.ZERO; 15 } 16 if(n.equals(BigInteger.valueOf(3))){ 17 return BigInteger.valueOf(6); 18 } 19 if(n.equals(BigInteger.valueOf(4))){ 20 return BigInteger.valueOf(4); 21 } 22 BigInteger ans=BigInteger.ZERO; 23 if(n.mod(BigInteger.valueOf(2)).equals(BigInteger.ONE)){ 24 ans=dfs(n.divide(BigInteger.valueOf(2))).multiply(BigInteger.valueOf(4)).add(n.divide(BigInteger.valueOf(2)).multiply(BigInteger.valueOf(6))); 25 hashmap.put(n, ans); 26 return ans; 27 } 28 else{ 29 BigInteger x; 30 x=n.divide(BigInteger.valueOf(2)); 31 ans=ans.add(x.multiply(BigInteger.valueOf(4))).add(BigInteger.valueOf(4).negate()); 32 ans=ans.add(dfs(x).multiply(BigInteger.valueOf(2))).add(dfs(x.add(BigInteger.ONE.negate())).multiply(BigInteger.valueOf(2))); 33 hashmap.put(n,ans); 34 return ans; 35 } 36 } 37 void work(){ 38 BigInteger n; 39 hashmap.clear(); 40 Scanner cin=new Scanner(System.in); 41 while(cin.hasNext()){ 42 n=cin.nextBigInteger(); 43 BigInteger ans=dfs(n); 44 System.out.println(ans.toString()); 45 } 46 47 } 48 public static void main(String args[]){ 49 new Main().work(); 50 } 51 }
多校5 hdu4919
时间: 2024-11-10 11:02:53