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

多校5 hdu4919

时间:2014-08-06 22:04:52      阅读:265      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   java   os   io   

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

bubuko.com,布布扣

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来记录已经到达的状态。

bubuko.com,布布扣
 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 }
View Code

 

 

 

多校5 hdu4919,布布扣,bubuko.com

多校5 hdu4919

标签:des   style   blog   http   color   java   os   io   

原文地址:http://www.cnblogs.com/youyouyou/p/3895452.html

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