hdu 4919 java大数

Exclusive or

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 277    Accepted Submission(s): 107


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<10 500).
 

Output
For each tests:

A single integer, the value of the sum.
 

Sample Input
  
  
3 4
 

Sample Output
  
  
6 4
 

Author
Xiaoxu Guo (ftiasch)
 

Source
 


为java记录一下。

import java.math.BigInteger;
import java.io.*;
import java.util.*;

public class Main {
    public static HashMap<BigInteger,BigInteger>  h;
    public static BigInteger solve(BigInteger n){
        if(h.containsKey(n)) return h.get(n);
        if (n.equals(BigInteger.ZERO)) return BigInteger.ZERO;
        if (n.equals(BigInteger.ONE)) return BigInteger.ZERO;
        BigInteger tp;
        BigInteger tp1=n.divide(BigInteger.valueOf(2));
        BigInteger er=BigInteger.valueOf(2);
        
        
        if(n.mod(BigInteger.valueOf(2)).equals(BigInteger.ONE)){
            BigInteger tmp=solve(tp1);
            tp=tmp.multiply(BigInteger.valueOf(4)).add(tp1.multiply(BigInteger.valueOf(6)));
        }
        else {
            BigInteger tmp=solve(tp1);
            BigInteger tmp1=solve(tp1.subtract(BigInteger.ONE));
            tp=tmp.multiply(BigInteger.valueOf(2)).add(tmp1.multiply(BigInteger.valueOf(2)))
                    .add(tp1.multiply(BigInteger.valueOf(4))).subtract(BigInteger.valueOf(4));
        }
        h.put(n,tp);
        return tp;
    }
    public static void main(String[] args){
        h=new HashMap<BigInteger, BigInteger>();
        Scanner in=new Scanner(System.in); 
        BigInteger n;
        for(int i=2;i<=10000;i++){
            solve(BigInteger.valueOf(i));
        }
        while (in.hasNext()) {
            n = in.nextBigInteger();
            System.out.println(solve(n));
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值