题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5661

题意:

  给你一个长度为n的整数序列,询问任意区间的极端异或和。

  定义极端异或和:

    当长度n > 1 的时候,将数组长度缩小到n-1。

    [a1, a2, a3 ···, an] -> [a1⊕a2,  a2⊕a3, a3⊕a4, ···, an-1⊕an]。

  一直缩小,直到长度减为1, 就是极端异或和。

题解:

  [a1, a2, a3, a4, a5] 操作时

  第一次:[a1⊕a2,  a2⊕a3, a3⊕a4, a4⊕a5]

  第二次:[a1⊕a2⊕a2⊕a,  a2⊕a3⊕a3⊕a4, a3⊕a4⊕a4⊕a5]

  以此类推到最后我们发现 a1用了1次, a2用了4次, a3用了6次, a4用了4次, a5用了1次。

  1 4 6 4 1。这个正好是C(0,4) C(1,4) C(2,4) C(3,4) C(4,4)。

  满足二项式的系数。

  所以对于长度为n的时候,我们需要C(n-1)的系数。

  

  我们就可以预处理C数组。

  1. memset(C, );
  2. for(int i = ;i<=n;i++){
  3. C[i][] = ;
  4. for(int j = ;j<=i;j++) C[i][j] = C[i-][j-] + C[i-][j];
  5. }

  因为只需要考虑奇偶性,就可以 C[i][j] = (C[i-1][j-1] + C[i-1][j])%2

  在第i个位置判断为奇数的时候就异或一下就可以得到ans了。

  但是一次询问O(n) 的复杂度,会导致TLE。就需要优化一下。

  

  我们可以发现 %2 的操作其实就 xor的操作。

  

  1. int C[maxn];
  2. vector <int> pos[maxn];
  3. void init()
  4. {
  5. ms(C, );
  6. for(int i = ;i<maxn;i++)
  7. {
  8. C[] = C[i-] = ;
  9. for(int j = i-;j>=;j--) C[j] = C[j-]^C[j];
  10. for(int j = ;j<i;j++)
  11. if(C[j])
  12. pos[i].pb(j);
  13. }
  14. }

  这里减少了一维的空间(可以参考01背包那里),因为都是滚动数组。

  将第j个位置是奇数,记录起来。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <string>
  5. #include <algorithm>
  6. #include <cmath>
  7. #include <vector>
  8. #include <queue>
  9. #include <map>
  10. #include <stack>
  11. #include <set>
  12. using namespace std;
  13. typedef long long LL;
  14. typedef unsigned long long uLL;
  15. #define ms(a, b) memset(a, b, sizeof(a))
  16. #define pb push_back
  17. #define mp make_pair
  18. #define eps 0.0000000001
  19. #define IOS ios::sync_with_stdio(0);cin.tie(0);
  20. const LL INF = 0x3f3f3f3f3f3f3f3f;
  21. const int inf = 0x3f3f3f3f;
  22. const int mod = 1e9+;
  23. const int maxn = +;
  24. int a[maxn];
  25. int C[maxn];
  26. vector <int> pos[maxn];
  27. void init()
  28. {
  29. ms(C, );
  30. for(int i = ;i<maxn;i++)
  31. {
  32. C[] = C[i-] = ;
  33. for(int j = i-;j>=;j--) C[j] = C[j-]^C[j];
  34. for(int j = ;j<i;j++)
  35. if(C[j])
  36. pos[i].pb(j);
  37. }
  38. }
  39. void solve()
  40. {
  41. int n;scanf("%d", &n);
  42. for(int i = ;i<n;i++) scanf("%d", &a[i]);
  43. int q;scanf("%d", &q);
  44. while(q--){
  45. int l, r;scanf("%d%d", &l, &r);
  46. int len = r-l+;
  47. int ans = ;
  48. int sz = pos[len].size();
  49. for(int k = ;k<sz;k++){
  50. ans ^= a[pos[len][k]+l];
  51. }
  52. printf("%d\n", ans);
  53. }
  54. }
  55. int main() {
  56. #ifdef LOCAL
  57. freopen("input.txt", "r", stdin);
  58. // freopen("output.txt", "w", stdout);
  59. #endif
  60. // IOS
  61. init();
  62. int t;scanf("%d", &t);
  63. int cnt = ;
  64. while(t--){
  65. printf("Case %d:\n", cnt++);
  66. solve();
  67. }
  68. return ;
  69. }

还有一个用Sierpinski sieve优化的,附上链接:http://morris821028.github.io/categories/%E8%A7%A3%E9%A1%8C%E5%8D%80/%E8%A7%A3%E9%A1%8C%E5%8D%80-UVa/

uva live 7639 Extreme XOR Sum (暴力+二项式)的更多相关文章

  1. 【二项式定理】【DFS】UVALive - 7639 - Extreme XOR Sum

    题意:一个序列,q次询问,每次问你某个指定区间内的EXtreme XOR值. 一个长度为l的区间的EXtreme XOR值被定义为,从左到右,将每相邻的两个数XOR起来,产生l-1个新的值,……如此循 ...

  2. UVALive - 7639 G - Extreme XOR Sum(思维)

    题目链接 题意 给出一个序列,相邻两两异或,生成一个新序列,再相邻两两异或,直到只剩下一个元素,问最后结果为多少.m个查询,每次都有一个待查询区间. 分析 既然有多组查询,n只是1e4,那么可以考虑预 ...

  3. 字典树-百度之星-Xor Sum

    Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...

  4. UVA 11426 - GCD - Extreme (II) (数论)

    UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...

  5. 2014 百度之星 1003 题解 Xor Sum

    Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...

  6. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  7. HDU 4825 Xor Sum 字典树+位运算

    点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) ...

  8. 2014百度之星第三题Xor Sum(字典树+异或运算)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  9. Xor Sum 01字典树 hdu4825

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

随机推荐

  1. Linux 下编写.sh文件运行JAR下的Class

    #!/bin/sh #bash文件头 APP_HOME=/opt/CrxToMongodb #目录是/opt/CrxToMongodb CLASSPATH=$APP_HOME #bin目录当然是包含j ...

  2. Qt项目界面文件(.ui)及其作用(超详细)

    http://c.biancheng.net/view/1820.html Qt 项目中,后缀为“.ui”的文件是可视化设计的窗体的定义文件,如 widget.ui.双击项目文件目录树中的文件 wid ...

  3. SpringBoot项目优化和Jvm调优

    https://www.cnblogs.com/jpfss/p/9753215.html 项目调优 作为一名工程师,项目调优这事,是必须得熟练掌握的事情. 在SpringBoot项目中,调优主要通过配 ...

  4. Django中间件拦截未登录url

    1.利用装饰器在视图中拦截未登录的url @login_required(login_url='/user/login/') def homepage(request): pass 这种方法适合于程序 ...

  5. Java并发——DCL问题

    转自:http://www.iteye.com/topic/875420 如果你搜索网上分析dcl为什么在java中失效的原因,都会谈到编译器会做优化云云,我相信大家看到这个一定会觉得很沮丧.很无助, ...

  6. ffmpeg使用分析视频

    https://www.cnblogs.com/Finley/p/8646711.html 先存下

  7. 长沙理工大学第十二届ACM大赛-重现赛 J 武藏牌牛奶促销

    链接:https://ac.nowcoder.com/acm/contest/1/J 来源:牛客网 武藏牌牛奶促销 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他 ...

  8. ubuntu 16.04 安装后需要做的事情

    1. 更改软件源 sudo gedit /etc/apt/source.list 在底部加入:(如果可以,把Ubuntu官方源注释掉“#_____”) # deb cdrom:[Ubuntu 16.0 ...

  9. 开发框架DevExtreme全新发布v19.1.3|附下载

    DevExtreme Complete Subscription是性能最优的 HTML5,CSS 和 JavaScript 移动.Web开发框架,可以直接在Visual Studio集成开发环境,构建 ...

  10. oracle中用case when查询列表

    查询sql语句如下 SELECT * FROM ( SELECT * ,ROW_NUMBER() OVER ( PARTITION BY scene_code ORDER BY (CASE statu ...