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. objectC时间用法

    #define kDEFAULT_DATE_TIME_FORMAT (@"yyyy-MM-dd HH:mm:ss") //获取当前日期,时间+(NSDate *)getCurren ...

  2. Android开发之通过反射获取到Android隐藏的方法

    在PackageManger中,有些方法被隐藏了,无法直接调用,需要使用反射来获取到该方法. 比如方法:getPackageSizeInfo(),通过这个方法可以获取到apk的CacheSize,Co ...

  3. Ubuntu中Eclipse安装与配置

    安装Eclipse: 第一种是通过Ubuntu自带的程序安装功能安装Eclipse,应用程序 ->Ubtuntu软件中心,搜Eclipse安装即可.第二种方法是用命令:应用程序->附件-& ...

  4. find-all-duplicates-in-an-array(典型的数组中的重复数,不错,我做出来了,可是发现别人有更好的做法)

    https://leetcode.com/problems/find-all-duplicates-in-an-array/ 典型的数组中的重复数.这次是通过跳转法,一个个跳转排查的.因为查过的不会重 ...

  5. iOS开发:视图生命周期

    iOS应用的视图状态分为以下几种 在viewcontroller的父类UIViewController中可以看到如下代码,通过重写不同的方法对操作视图渲染. @available(iOS 2.0, * ...

  6. 在ASP.NET中如何判断用户IE浏览器的版本

    f ( Request.Browser.MajorVersion == ) { // to do } ................................................. ...

  7. NoSQL 数据库系统对比

    虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只是时间问题:被迫使用关系数据库,但最终发现不能适应需求的情况不胜枚举. 但是NoSQL数据库之间的不同,远超过两 SQ ...

  8. 为什么多数游戏服务端是用 C++ 来写

    早年开发游戏必须用C++,这没得说,2000-2004年,java还没有nio,其他动态语言不抗重负,只能C/C++能开发出完整可用的游戏服务端.直到2005年,韩国的游戏很多都还是纯C++写服务端, ...

  9. Self-Paced Training (3) - Docker Operations

    AgendaTroubleshooting ContainersOverview of Security PracticesPrivate RegistryIntro to Docker Machin ...

  10. RESTful API 设计最佳实践(转)

    背景 目前互联网上充斥着大量的关于RESTful API(为方便,下文中“RESTful API ”简写为“API”)如何设计的文章,然而却没有一个”万能“的设计标准:如何鉴权?API 格式如何?你的 ...