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.
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的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();
}
}