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<10500).
 
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的使用方法,能够当作模版来使用了。 *转载请注明出处,谢谢。
*/

最新文章

  1. JavaScript作用域原理(一)——作用域链
  2. Adaptive Backgrounds – jQuery 自适应背景插件
  3. Java反射机制(Reflection)
  4. 两个viewport的故事(第一部分)
  5. MAC 下安装opencv遇到问题的解决方法(安装homebrew, wget)
  6. Java Evaluate Reverse Polish Notation(逆波兰式)
  7. WebIM(3)----性能测试
  8. 扩展Session机制
  9. Python基础——字符串
  10. [BZOJ 1079][SCOI 2008]着色方案
  11. Session 快速开始 通过session的attribute通信
  12. 使用JS读取本地文本文件(兼容各种浏览器)
  13. bzoj 1036: [ZJOI2008]树的统计Count 树链剖分+线段树
  14. SFTP文件上传与下载(window 上传文件到linux服务器)
  15. Leetcode 77
  16. [转]C++之多态性与虚函数
  17. Map集合统计字母次数
  18. js判断PC端 移动端 并跳转到对应页面
  19. incast.tcl
  20. Kerberos KDC not reachable

热门文章

  1. SqlServer保留几位小数的两种做法
  2. Red Gate系列之三 SQL Server 开发利器 SQL Prompt 5.3.4.1 Edition T-SQL智能感知分析器 完全破解+使用教程
  3. HDU 1856 More is better(并查集+离散化)
  4. iOS开发 编辑框被系统弹出的软键盘遮挡问题
  5. openstack dhcp调试
  6. Javascript设计模式系列一
  7. 匿名方法,Lambda表达式,高阶函数
  8. 乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
  9. MY WAY程序(十三) 理念和技术
  10. 在ASP.NET中使用SOAP Extensions捕获WebService异常