hdu 4919 Exclusive or

Exclusive or

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


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
 

题解:

下面是官方给的答案:


鉴于比较难以理解官方大神的讲解,这里我研究了一下,下面给出求解化简过程:


图片有些不清晰,凑合看吧 T^T


代码:

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

public class Main {

	public static BigInteger zero=BigInteger.valueOf(0);
	public static BigInteger one=BigInteger.valueOf(1);
	public static BigInteger two=BigInteger.valueOf(2); 
	public static BigInteger four=BigInteger.valueOf(4);
	public static BigInteger six=BigInteger.valueOf(6); 
    
	public static HashMap<BigInteger,BigInteger> map=new HashMap<BigInteger,BigInteger>();
	
	public static BigInteger solve(BigInteger n)
	{
		if(map.containsKey(n))
			return map.get(n);
		BigInteger t=BigInteger.valueOf(0);
		BigInteger k=n.divide(two);
		BigInteger r=n.mod(two);
		
		if(r.compareTo(one)==0)
			t=solve(k).multiply(four).add(k.multiply(six));
		else  {
			t=two.multiply(solve(k));
			t=t.add(two.multiply(solve(k.subtract(one))));
			t=t.add(four.multiply(k));
			t=t.subtract(four);
		}
		map.put(n, t);
		return t;
	}
	
	public static void main(String []args)
	{
        BigInteger n;
        map.put(zero, zero);
        map.put(one,zero);
        Scanner cin = new Scanner(System.in); 
        while(cin.hasNext())
        {
        	n=cin.nextBigInteger();
        	System.out.println(solve(n));
        }
	}
}
/*
借鉴别人的代码,学习了一下java大数和HashMap的用法,可以当作模版来使用了。

*转载请注明出处,谢谢。
*/

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值