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. C#ShowCursor光标的显示与隐藏

      使用using System.Runtime.InteropServices; [DllImport("user32.dll" , EntryPoint = "Sho ...

  2. unity3d5.2.3中 调整视角

    按住alt键不放,然后左边的手的图标会变成一个眼睛,在Scene中移动.就会发现可以调整视角了

  3. When you’re nearly 40 and unmarried, and you realize you’re going to be okay

    https://medium.com/the-gathering-kind/when-you-re-nearly-40-and-unmarried-and-you-realize-you-re-goi ...

  4. HNOI2008越狱(快速幂)

    快速幂水过,贴一下模版. ; var x,y,n,m:int64; function power(num,times:int64):int64; var temp:int64; begin then ...

  5. fiddler2抓包工具使用图文教程

    fiddler2抓包工具使用图文教程 三.fiddler实用功能使用说明: 1.fiddler捕获浏览器的会话: 能支持http代理的任意程序都能被fiddler捕获到,由于fiddler的运行机制就 ...

  6. CCScrollView 实现帮助界面、关卡选择

    本文出自[无间落叶]:http://blog.leafsoar.com/archives/2013/07-27.html 本文介绍了 CCScrollView 来编写帮助界面和关卡选择界面的方法,在编 ...

  7. apache开源项目--Synapse

    Apache Synapse一个易于使用.轻量级的XML与Web Services管理和集成中间件.可用于搭建SOA和ESB的基础平台.Apache Synapse支持多种标准包括:XML.XSLT. ...

  8. LeetCode:Sort List

    Title: Sort a linked list in O(n log n) time using constant space complexity. 思路:考虑快速排序和归并排序,但是我的快速排 ...

  9. Linux编译安装Darwin Streaming Server 6.0.3。。。

    目前主流的流媒体服务器有微软的windows media server.RealNetworks的Helixserver和苹果公司的Darwin Streaming Server. 微软的window ...

  10. poj 2373 Dividing the Path

    Dividing the Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2858   Accepted: 1064 ...