题意:

定义

\[f(n)=\sum\limits_{i=1}^{n-1}(i\oplus (n-i))
\]

求\(f(n),n \leq 10^{500}\)

分析:

这个数列对应OEIS的A006582

先上公式:

\[f(n)=\left\{\begin{matrix}
4f(k)+6k,n=2k+1\\
2f(k)+2f(k-1)+4k-4,n=2k
\end{matrix}\right.\]

递推的思路就是虽然不知道两个数的异或值,但是如果知道这两个数的奇偶性那么结果的奇偶性也就知道了。

还有一个公式:\(2a \oplus 2b = 2(a \oplus b)\),这个也很容易理解。

下面开始证明:

  • \(n=2k+1\)时:

\(\; \; \; \; \sum\limits_{i=1}^{n-1}(i\oplus (n-i))\)

\(=2\sum\limits_{i=1}^{k}((2i) \oplus (n-2i))\)

\(=2\sum\limits_{i=1}^{k}((2i) \oplus (2k-2i+1))\)

\(=2\sum\limits_{i=1}^{k}((2i) \oplus (2k-2i) + 1)\)

\(=2\sum\limits_{i=1}^{k}((2i) \oplus (2k-2i)) + 2k\)

\(=4\sum\limits_{i=1}^{k}((i) \oplus (k-i)) + 2k\)

\(=4\sum\limits_{i=1}^{k-1}((i) \oplus (k-i)) + 4(k \oplus (k-k)) + 2k\)

\(=4f(k)+6k\)

  • \(n-2k\)时:

\(\; \; \; \; \sum\limits_{i=1}^{n-1}(i\oplus (n-i))\)

\(=\sum\limits_{i=1}^{k-1}((2i) \oplus (n-2i)) + \sum\limits_{i=0}^{k-1}((2i+1) \oplus (n-2i-1))\)





\(\; \; \; \; \sum\limits_{i=1}^{k-1}((2i) \oplus (n-2i))\)

\(=\sum\limits_{i=1}^{k-1}((2i) \oplus (2k-2i))\)

\(=2\sum\limits_{i=1}^{k-1}(i \oplus (k-i))\)

\(=2f(k)\)





\(\; \; \; \; \sum\limits_{i=0}^{k-1}((2i+1) \oplus (n-2i-1))\)

\(=\sum\limits_{i=0}^{k-1}((2i) \oplus (2k-2i-2))\),两边都是奇数,把末位的\(1\)去掉后异或值不变

\(=2\sum\limits_{i=0}^{k-1}i \oplus (k-1-i)\)

\(=2\sum\limits_{i=1}^{k-2}i \oplus (k-1-i) + 2(0 \oplus (k-1)) + 2((k-1) \oplus 0)\)

\(=2f(k-1)+4k-4\)

所以:

\(\; \; \; \; \sum\limits_{i=1}^{n-1}(i\oplus (n-i))\)

\(=\sum\limits_{i=1}^{k-1}((2i) \oplus (n-2i)) + \sum\limits_{i=0}^{k-1}((2i+1) \oplus (n-2i-1))\)

\(=2f(k)+2f(k-1)+4k-4\)

推导完毕。

最后用Java大数记忆化搜索。

import java.util.*;
import java.io.*;
import java.math.*; public class Main {
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 F(BigInteger n) {
if(map.containsKey(n)) return map.get(n);
BigInteger k = n.divide(two);
BigInteger odd = n.mod(two);
BigInteger ans;
if(odd.compareTo(one) == 0) {
ans = F(k).multiply(four).add(k.multiply(six));
} else {
ans = F(k).multiply(two);
ans = ans.add(F(k.subtract(one)).multiply(two));
ans = ans.add(k.multiply(four)).subtract(four);
}
map.put(n, ans);
return ans;
} public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
map.put(BigInteger.ZERO, BigInteger.ZERO);
map.put(BigInteger.ONE, BigInteger.ZERO);
while(cin.hasNext()) {
BigInteger n = cin.nextBigInteger();
System.out.println(F(n));
}
cin.close();
}
}

HDU 4919 Exclusive or 数学的更多相关文章

  1. HDU 4919 Exclusive or (数论 or 打表找规律)

    Exclusive or 题目链接: http://acm.hust.edu.cn/vjudge/contest/121336#problem/J Description Given n, find ...

  2. [JAVA]HDU 4919 Exclusive or

    题意很简单, 就是给个n, 算下面这个式子的值. $\sum\limits_{i=1}^{n-1} i \otimes (n-i)$ 重点是n的范围:2≤n<10500 比赛的时候 OEIS一下 ...

  3. hdu 4919 Exclusive or

    Exclusive or Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) T ...

  4. HDU 4816 Bathysphere(数学)(2013 Asia Regional Changchun)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4816 Problem Description The Bathysphere is a spheric ...

  5. HDU 5584 LCM Walk 数学

    LCM Walk Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5584 ...

  6. HDU 4336 Card Collector 数学期望(容斥原理)

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=4336 题意简单,直接用容斥原理即可 AC代码: #include <iostream> ...

  7. HDU 5570 balls 期望 数学

    balls Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5570 De ...

  8. hdu 4710 Balls Rearrangement (数学思维)

    意甲冠军:那是,  从数0-n小球进入相应的i%a箱号.然后买一个新的盒子. 今天的总合伙人b一个盒子,Bob试图把球i%b箱号. 求复位的最小成本. 每次移动的花费为y - x ,即移动前后盒子编号 ...

  9. HDU 4790 Just Random 数学

    链接:pid=4790">http://acm.hdu.edu.cn/showproblem.php?pid=4790 意:从[a.b]中随机找出一个数字x,从[c.d]中随机找出一个 ...

随机推荐

  1. 解决在eclipse中导入项目名称已存在的有关问题

    新建项目-Import-File System-找到相应的文件夹-Overwrite existing resources without warning打钩,选中项目即可

  2. Struts2初级篇(HelloWorld)

    Struts2的工作流程: 从一个高水平角度看,Struts2 是一个MVC拉动的(或MVC2)框架,Struts2 的模型-视图-控制器模式是通过以下五个核心部分进行实现的: 操作(Actions) ...

  3. 七、SSR(服务端渲染)

    使用框架的问题 下载Vue.js 执行Vue.js 生成HTML页面(首屏显示,依赖于vue.js的加载) 以前没有前端框架时,用jsp/php在服务器端进行数据的填充,发送给客户端就是已经填充好的数 ...

  4. jQuery知识重构

    jQuery知识重构 目录: 一.入口函数 1          $(document).ready(function(){}); 2          $(function(){}); jQuery ...

  5. echarts使用中的那些事儿(一)

    近来由于有几个小项目要用到echarts里的一些图,不得不使用,可是要完全按照自己的意愿来,要对它有些了解,要翻阅资料,遂有以下小结: 1.最开始第一步是把数据调出来就行,能在图上显示就成,以下是最开 ...

  6. meterpreter > run post/windows/capture/keylog_recorder

    meterpreter > migrate 1548[*] Migrating to 1548...[*] Migration completed successfully.meterprete ...

  7. 服网LNMP集群 w/ MySQL PaaS-1.0

    平台: arm 类型: ARM 模板 软件包: haproxy linux mysql nginx application server arm basic software fuwang infra ...

  8. @Valid的坑

    @Valid 只能用来验证 @RequestBody 标注的参数,并且要写在 @RequestBody 之前

  9. 详情介绍win7:编辑文件夹时提示操作无法完成,因为其中的文件夹或文件已在另一个程序中打开的解决过程

    我们在使用电脑中,总会遇到下面这种情况: 那怎么解决呢,现在就开始教程: 在电脑的底下显示各种图标那一行点击右键,再选择“启动任务管理器” 接下来你就可以对你刚刚要操作的文件进行重命名.删除等操作啦! ...

  10. HDU汉诺塔系列

    这几天刷了杭电的汉诺塔一套,来写写题解. HDU1207 汉诺塔II HDU1995 汉诺塔V HDU1996 汉诺塔VI HDU1997 汉诺塔VII HDU2064 汉诺塔III HDU2077  ...