hdu 4919 ( Exclusive or ) 数论递推

Exclusive or

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


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
 

Recommend
We have carefully selected several similar problems for you:   4920  4918  4917  4916  4915 

今天学了java 重写了一遍





import java.util.*;
import java.math.*;

public class Main {

    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        init();
        BigInteger N;
        while(cin.hasNext())
        {
            N = cin.nextBigInteger();
            System.out.println(get(N));
        }
        cin.close();
    }

	static Map<BigInteger,BigInteger> map = new HashMap<BigInteger,BigInteger>();
    
    
	public static void init()
	{
		map.put(BigInteger.valueOf(1), BigInteger.valueOf(0));
		map.put(BigInteger.valueOf(2), BigInteger.valueOf(0));
		map.put(BigInteger.valueOf(3), BigInteger.valueOf(6));
		map.put(BigInteger.valueOf(4), BigInteger.valueOf(4));
		map.put(BigInteger.valueOf(5), BigInteger.valueOf(12));
		map.put(BigInteger.valueOf(6), BigInteger.valueOf(20));
		map.put(BigInteger.valueOf(7), BigInteger.valueOf(42));
		map.put(BigInteger.valueOf(8), BigInteger.valueOf(32));
		map.put(BigInteger.valueOf(9), BigInteger.valueOf(40));


	}
	public static BigInteger get(BigInteger i)
	{
		BigInteger ret;
		if(map.containsKey(i)) return map.get(i);
		else {
			BigInteger tp= (i.mod(BigInteger.valueOf(2)));
			if(tp.equals(BigInteger.valueOf(1))){
				BigInteger ie=i.divide(BigInteger.valueOf(2));
				ret=(get(ie).multiply(BigInteger.valueOf(4)).add(ie.multiply(BigInteger.valueOf(6))));
				//get(ie)*4+ie*6;
				map.put(i, ret);
				return ret;
			}
			else {
				BigInteger ie=i.divide(BigInteger.valueOf(2));
				ret=get(ie).add(get(ie.add(BigInteger.valueOf(-1))).add(ie.multiply(BigInteger.valueOf(2))).add(BigInteger.valueOf(-2)));
				ret=ret.multiply(BigInteger.valueOf(2));
				map.put(i, ret);
				return ret;
			}
		}
	}
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值