题意:

定义

\[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. springboot 学习笔记(四)

    (四)springboot整合mybatis 1.以mysql为例,在pom文件中添加如下依赖,依次为mybatis.jdbc.db pool依赖 <dependency> <gro ...

  2. Backbone源码解析系列

    01 编码风格.继承 02 Backbone.Events 03 Backbone.Model 04 Backbone.View 05 Backbone.Router 06 Backbone应用于we ...

  3. JNI教程

    一.什么是JNI JNI(Java Native Interface ),它是Java SDK的一部分,主要用于实现Java对其他语言编写的代码和库的调用,比如C和C++.JNI提供的API也能让JV ...

  4. HDU 1305 Immediate Decodability 可直接解码吗?

    题意:一个码如果是另一个码的前缀,则 is not immediately decodable,不可直接解码,也就是给一串二进制数字给你,你不能对其解码,因解码出来可能有多种情况. 思路:将每个码按长 ...

  5. IP地址与数字地址相互转换

    /// <summary> /// IP地址转换成数字 /// </summary> /// <param name="addr">IP地址&l ...

  6. 在github中的READEME中添加图片或者动图

    在github中reademe中添加动图或者图片 将你需要展示的图片放在这个项目中的某个文件夹中,然后再reademe中这样引入 ![maze](https://github.com/GainLoss ...

  7. 机器学习-octave使用

    1 == 2    % false 1 ~=2     % true % 隐藏版本,只显示>> . PS1('>> '); % 输出两位小数格式 disp(sprintf('2 ...

  8. Redis常用特性

    发布订阅 ·服务器状态在pubsub_channels字典保存了所有频道的订阅关系:SUBSCRIBE命令负责将客户端和被订阅的频道关联到这个字典里面,而UNSUBSCRIBE命令则负责解除客户端和被 ...

  9. 【BZOJ1030】[JSOI2007] 文本生成器(AC自动机上跑DP)

    点此看题面 大致题意: 给你\(N\)个字符串(只含大写字母),要你求出有多少个由\(M\)个大写字母构成的字符串含有这\(N\)个字符串中的至少一个. \(AC\)自动机 看到题目,应该比较容易想到 ...

  10. FW 数据库迁移之从oracle 到 MySQL

    方式一: 手动方式导入导出 手动的方式导入, 就是操作步骤会比较繁琐一些. 对Table 的结构和数据: 1. 使用 SQL Developer 把 oracle 的 table 的schema 和 ...