Exclusive or

题目链接:

http://acm.hust.edu.cn/vjudge/contest/121336#problem/J

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

##题意:

求如题所示的和,n的范围是1e500.


##题解:

数据这么大肯定要找规律.
先尝试打出前100个数的表,然后找规律....(弱鸡并不能找出来)
先安利一个网站(http://oeis.org/)这是一个在线整数数列查询网站.
搜一下果然有:(http://oeis.org/A006582)
公式为:a(0)=a(1)=0, a(2n) = 2a(n)+2a(n-1)+4n-4, a(2n+1) = 4a(n)+6n.
由于是个递归公式,可以用dfs来计算,用map去重后应该是O(lgn).

一开始用cpp的大数模版一直出现各种问题(版不太熟悉),干脆复习一下java语法.
网上找到一份题解有推导过程:
![](http://images2015.cnblogs.com/blog/764119/201607/764119-20160727173231216-901294503.png)


##代码:
``` java
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Scanner;

public class Main {

public static HashMap<BigInteger, BigInteger> myMap = new HashMap<BigInteger,BigInteger>();

public static BigInteger [] num = new BigInteger[10];

public static BigInteger dfs(BigInteger x) {
if(x == num[0] || x== num[1]) return num[0];
if(myMap.containsKey(x))return myMap.get(x);
if(x.mod(num[2]) == num[0]) {
BigInteger n = x.divide(num[2]);
BigInteger tmp = num[2].multiply(dfs(n).add(dfs(n.subtract(num[1])))).add(num[4].multiply(n.subtract(num[1])));
myMap.put(x, tmp);
return tmp;
} else {
BigInteger n = (x.subtract(num[1])).divide(num[2]);
BigInteger tmp = num[4].multiply(dfs(n)).add(num[6].multiply(n));
myMap.put(x, tmp);
return tmp;
}
} public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); for(int i=0; i<10; i++) {
num[i] = BigInteger.valueOf(i);
} while(scanner.hasNext()){
myMap.clear();
BigInteger n = scanner.nextBigInteger();
BigInteger ans = dfs(n); System.out.println(ans);
} scanner.close();
}

}

HDU 4919 Exclusive or (数论 or 打表找规律)的更多相关文章

  1. HDU 4861 Couple doubi (数论 or 打表找规律)

    Couple doubi 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/D Description DouBiXp has a ...

  2. hdu 5391 Zball in Tina Town(打表找规律)

    问题描述 Tina Town 是一个善良友好的地方,这里的每一个人都互相关心. Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大11倍.在第二天的时候, ...

  3. 数学--数论--HDU - 6124 Euler theorem (打表找规律)

    HazelFan is given two positive integers a,b, and he wants to calculate amodb. But now he forgets the ...

  4. 数学--数论--HDU 1792 A New Change Problem (GCD+打表找规律)

    Problem Description Now given two kinds of coins A and B,which satisfy that GCD(A,B)=1.Here you can ...

  5. hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)

    Nim or not Nim? Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Sub ...

  6. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  7. HDU 5795 A Simple Nim(SG打表找规律)

    SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...

  8. HDU 4731 Minimum palindrome 打表找规律

    http://acm.hdu.edu.cn/showproblem.php?pid=4731 就做了两道...也就这题还能发博客了...虽然也是水题 先暴力DFS打表找规律...发现4个一组循环节.. ...

  9. hdu 3032 Nim or not Nim? (sg函数打表找规律)

    题意:有N堆石子,每堆有s[i]个,Alice和Bob两人轮流取石子,可以从一堆中取任意多的石子,也可以把一堆石子分成两小堆 Alice先取,问谁能获胜 思路:首先观察这道题的数据范围  1 ≤ N ...

随机推荐

  1. yeoman错误提示

    运行 yo angular 出现如下提示: $ yo angular grunt-cli: The grunt command line interface. (v0.1.9) Fatal error ...

  2. 12 Useful “df” Commands to Check Disk Space in Linux

    On the internet you will find plenty of tools for checking disk space utilization in Linux. However, ...

  3. Asp.Net读写XML简单方法

    xml文件 <?xml version="1.0" encoding="utf-8"?> <book> <title>web ...

  4. 前端SPA框架一些看法

    说起前端框架,我个人主张有框架不如无框架,这个观点要先从框架和库的区别说起. 我所理解的库,解决的是代码或是模块级别的复用或者对复杂度的封装问题;而框架,更多的是对模式级别的复用和对程序组织的规范,这 ...

  5. ASP.NET在IE10,IE11中Form表单身份验证失效问题解决方法

    已经研究出解决方案. 在web.config中的forms中增加cookieless="UseCookies"属性即可.   <authentication mode=&qu ...

  6. Android之 环境搭建

    一. 使用ADT Bundle多合一下载包 下载地址:链接:http://pan.baidu.com/s/1gepNRjX  密码: ozdi 说      明:多合一下载包,里面包含了:sdk + ...

  7. 【Struts】服务器文件的上传和下载

    Java中获得文件的文件后缀 import java.io.*; public class FileTest{ public static void main(String args[]){ File ...

  8. winfrom dataGridView 自定义分页实现

    Winfrom 基本处于忘光的阶段.先需要做个winfrom 的软件.然后自己扩展了DataGridView带分页的控件.废话不多说 上图先   现在一步步实现其效果. 1.添加用户控件 上图即可知道 ...

  9. Android selector item 属性大全(按钮按下不同效果)

    <selector>         必须.必须是根元素.包含一个或多个<item>元素.          Attributes:             xmlns:and ...

  10. Int.Parse()、Convert.toInt32()和(int)区别

    通过网上的查询从而了解了Int.Parse().Convert.toInt32()和(int)区别. 一.定义上的差别 int类型表示一种整型,.NET Framework 类型为 System.In ...