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.
Note: ⊕ denotes bitwise exclusive-or.
Input
The input consists of several tests. For each tests:
A single integer n (2≤n<10 500).
A single integer n (2≤n<10 500).
Output
For each tests:
A single integer, the value of the sum.
A single integer, the value of the sum.
Sample Input
3 4
Sample Output
6 4
Author
Xiaoxu Guo (ftiasch)
Source
Recommend
今天学了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;
}
}
}
}