HDU 4919 Exclusive or (数论 or 打表找规律)
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语法.
网上找到一份题解有推导过程:

##代码:
``` 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 打表找规律)的更多相关文章
- HDU 4861 Couple doubi (数论 or 打表找规律)
Couple doubi 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/D Description DouBiXp has a ...
- hdu 5391 Zball in Tina Town(打表找规律)
问题描述 Tina Town 是一个善良友好的地方,这里的每一个人都互相关心. Tina有一个球,它的名字叫zball.zball很神奇,它会每天变大.在第一天的时候,它会变大11倍.在第二天的时候, ...
- 数学--数论--HDU - 6124 Euler theorem (打表找规律)
HazelFan is given two positive integers a,b, and he wants to calculate amodb. But now he forgets the ...
- 数学--数论--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 ...
- hdu 3032 Nim or not Nim? (SG函数博弈+打表找规律)
Nim or not Nim? Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Sub ...
- HDU 5753 Permutation Bo (推导 or 打表找规律)
Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...
- HDU 5795 A Simple Nim(SG打表找规律)
SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...
- HDU 4731 Minimum palindrome 打表找规律
http://acm.hdu.edu.cn/showproblem.php?pid=4731 就做了两道...也就这题还能发博客了...虽然也是水题 先暴力DFS打表找规律...发现4个一组循环节.. ...
- hdu 3032 Nim or not Nim? (sg函数打表找规律)
题意:有N堆石子,每堆有s[i]个,Alice和Bob两人轮流取石子,可以从一堆中取任意多的石子,也可以把一堆石子分成两小堆 Alice先取,问谁能获胜 思路:首先观察这道题的数据范围 1 ≤ N ...
随机推荐
- 点(Dot)与像素(Pixel)的区别
DPI中的点(Dot)与图像分辨率中的像素(Pixel)是容易混淆的两个概念, DPI中的点可以说是硬件设备最小的显示单元, 而像素则既可是一个点,又可是多个点的集合.在扫描仪扫描图像时,扫描仪的每一 ...
- gulp.watch监听文件
Gulp.watch()会返回我们熟知的watcher.我们可以利用watcher来监听额外的事件或者向watch中添加文件. 例如,在执行一系列任务和调用一个函数时,你就可以在返回的watcher中 ...
- SQL复制表及表结构
复制表结构和数据SQL语句 1:复制表结构及数据到新表 select * into 目的数据库名.dbo.目的表名 from 原表名 select * into my0735home.dbo.info ...
- asp.net 2.0中新增的web.config的默认namespace功能 (转)
看上去这个题目比较长,但实际上,我在看资料时发现,这就是说,在asp.net 2.0中,只需要在web.config里定义你要用的那些namespace,则在aspx页面中就不需要再象1.1那样,用 ...
- 搭建LAMP测试环境
LAMP:Linux+Apache+Mysql+Php,组合统称为LAMP,关于其中的独立个体,这里就不多介绍了. 1.首先准备一下软件包,如下: mysql-5.0.22.tar.gz httpd- ...
- [转](多实例)mysql-mmm集群
一.需求说明 最近一直在学习mysql-mmm,想以后这个架构也能用在我们公司的业务上,我们公司的业务是单机多实例部署,所以也想把mysql-mmm部署成这样,功夫不负有心人,我成功了,和大家分享一下 ...
- Spring学习之基本概念
Spring 基本概念 Spring优点: 1.Spring不同于其它的Framework,它要提供的是一种管理你的业务对象的方法. 2.DI有效的降低了耦合度 3.AOP提供了通用任务的集中管理 4 ...
- Informatica元数据库解析
Informatica所有的元数据信息均以数据库表的方式存到了元数据库中.当然Infa本身工具提供了很多的人性化的功能,使我们在开发时可以很方便的进行操作,但人们的需求总是万变的,需要方便的取到自己需 ...
- HDU 5878 I Count Two Three
I Count Two Three Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- webdriver(python)学习笔记七——多层框架定位与智能等待
多层框架或窗口定位: switch_to_frame() switch_to_window() 智能等待: implicitly_wait() 现在web应用中经常会遇到框架如(frame)或窗口(w ...