HDU 4919(Exclusive or-java的HashMap类)

Exclusive or

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


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:   5426  5425  5424  5423  5422 
 

Java的HashMap类



package xor2;

import java.math.BigInteger;
import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;

public class Main {
	public static Map<BigInteger , BigInteger> h = new  HashMap<BigInteger , BigInteger>();
	public static BigInteger x0,x1,x2,x4,x6;
	
	
	public static BigInteger a(BigInteger x) {
		if (x.compareTo(x1)<=0) return x0;
		
		
		if (h.get(x)!=null) return h.get(x);
		
		BigInteger ans = x0 , n = x.divide(x2); 
		
		
		
		if ((x.mod(x2)).compareTo(x0)==0) {
			 ans=ans.add(x2.multiply(a(n)));
			 ans=ans.add(x2.multiply(a( n.subtract(x1) )));
			 ans=ans.add(x4.multiply(n).subtract(x4));
		}else {
			 ans=ans.add(x4.multiply(a(n)));
			 ans=ans.add(x6.multiply(n));
		}
		h.put(x, ans);
		
		return ans;		
	}
	
	public static void work()
	{
		BigInteger n;
		
		Scanner cin = new Scanner (System.in);
		
		h.put(x0, x0);
		h.put(x1, x0);
		
		while (cin.hasNext()) {
			n = cin.nextBigInteger();
				
			System.out.println(a(n));
		}
		cin.close();  
	}
	
	public static void main(String[] args)   {
		
		x0=BigInteger.valueOf(0);
		x1=BigInteger.valueOf(1);
		x2=BigInteger.valueOf(2);
		x4=BigInteger.valueOf(4);
		x6=BigInteger.valueOf(6);
		
		work();
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值