题意:

定义

\[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. LitePal——安卓数据库library

    简介:一个让开发者使用SQLite数据库更加容易的库文件 LitePal for Android,项目地址:点击打开 LitePal是一个开源的android库,它让开发者使用SQLite数据变得容易 ...

  2. 什么是SpringBoot

    随着动态语言的流行(Ruby,Groovy,Scala,Node.js),Java的开发显得格外的笨重;繁多的配置,低下的开发效率,复杂的部署流程以及第三方技术集成难度大. 在上述环境 下,Sprin ...

  3. mysql 批量修改 表字段/表/数据库 字符集和排序规则

    今天接到一个任务是需要把数据库的字符编码全部修改一下,写了以下修正用的SQL,修正顺序是   表字段 > 表 > 数据库. 表字段修复: #改变字段数据 SELECT TABLE_SCHE ...

  4. spring-boot整合shiro作权限认证

    spring-shiro属于轻量级权限框架,即使spring-security更新换代,市场上大多数企业还是选择shiro 废话不多说  引入pom文件 <!--shiro集成spring--& ...

  5. js数字滑动时钟

    js数字滑动时钟: <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  6. shell中的判断语句

    1.字符串判断 str1 = str2 当两个串有相同内容.长度时为真 str1 != str2 当串str1和str2不等时为真 -n str1 当串的长度大于0时为真(串非空,变量) -z str ...

  7. Html+css实现带图标的控件

    </pre><pre name="code" class="html"><!DOCTYPE html> <html l ...

  8. jQuery中常用的元素查找方法总结

    $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("div&q ...

  9. python时间转换 ticks-FYI

    #设a为字符串 import time a = "2011-09-28 10:00:00" #中间过程,一般都需要将字符串转化为时间数组 time.strptime(a,'%Y-% ...

  10. codeforces 600D Area of Two Circles' Intersection

    分相离,内含,想交三种情况讨论一下. 主要是精度和数据范围的问题,首先数据用long double,能用整型判断就不要用浮点型. 题目中所给的坐标,半径是整型的,出现卡浮点判断的情况还是比较少的. 最 ...