题意:

定义

\[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. jQuery图片组展示插件----Galleria使用简介

    1.技术目标 掌握Galleria插件的基本操作 2.Galleria简介 Galleria是一个jQuery插件,可用于展示多张图片,操作也比较简单, 展示效果也非常不错,如图: 提示:Galler ...

  2. js中的load先执行还是Jquery的ready先执行问题

    onload需要页面上所有的资源都加载上之后执行,而ready则是DOM文档树已经解析完成时,说ready比onload快最显著的是比如一个页面上有一个很大的图片,加载要好久,onload只有在图片加 ...

  3. Android 类似360悬浮窗口实现源码

    当我们在手机上安装360安全卫士时,手机屏幕上时刻都会出现一个小浮动窗口,点击该浮动窗口可跳转到安全卫士的操作界面,而且该浮动窗口不受其他activity的覆盖影响仍然可见(多米音乐也有相关的和主界面 ...

  4. JS浏览器获取宽高

    screen.availHeight is the height the browser's window can have if it is maximized. (including all th ...

  5. 卸载VS2013 2015

    我有两个VS,特别讨厌,每当使用window程序删除时候,就出现 停止工作! 然后从知乎上发现了这个 https://github.com/Microsoft/VisualStudioUninstal ...

  6. MeshLab中插件的添加过程

    MeshLab中主要插件类型有 filter plugins, i/o plugins, edit plugins,这些插件实现了MeshLab的大部分功能.新加入的插件命名规则最好也遵循规范,可命名 ...

  7. Flexbox与Grid属性比较

    网格容器(container)属性 网格项目(item)属性 Flex容器(container)属性 Flex项目(item)属性

  8. Element-ui多选下拉实现全部与其他互斥

    1.以事件类型为例,给下拉绑定选项改变的change事件 2.当已选项个数大于1(即先选了其他,再选不限)且最后选的是不限时,取消其他选项选中状态: 当已选项个数等于2(即先选了不限,再选其他)且第一 ...

  9. spark集群配置细则总结

    修改目录与目录组: sudo chown -R hadoop:hadoop spark-1.6.1-bin-hadoop2.6 sudo chown -R hadoop:hadoop jdk1.8.0 ...

  10. Linux uart程序

    我用的是jetson tx1 开发板 都是linux系统出了串口文件可能不同其他的没有什么不同都能用. 我安装的是qt5 新建一个none  qt c工程,用c 语言开发 期间调试了两天结果还是发送和 ...