题目链接: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数组。

     memset(C, );
for(int i = ;i<=n;i++){
C[i][] = ;
for(int j = ;j<=i;j++) C[i][j] = C[i-][j-] + C[i-][j];
}

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

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

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

  

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

  

 int C[maxn];
vector <int> pos[maxn];
void init()
{
ms(C, );
for(int i = ;i<maxn;i++)
{
C[] = C[i-] = ;
for(int j = i-;j>=;j--) C[j] = C[j-]^C[j];
for(int j = ;j<i;j++)
if(C[j])
pos[i].pb(j);
}
}

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

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

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+;
const int maxn = +;
int a[maxn];
int C[maxn];
vector <int> pos[maxn];
void init()
{
ms(C, );
for(int i = ;i<maxn;i++)
{
C[] = C[i-] = ;
for(int j = i-;j>=;j--) C[j] = C[j-]^C[j];
for(int j = ;j<i;j++)
if(C[j])
pos[i].pb(j);
}
}
void solve()
{
int n;scanf("%d", &n);
for(int i = ;i<n;i++) scanf("%d", &a[i]);
int q;scanf("%d", &q);
while(q--){
int l, r;scanf("%d%d", &l, &r);
int len = r-l+;
int ans = ;
int sz = pos[len].size();
for(int k = ;k<sz;k++){
ans ^= a[pos[len][k]+l];
}
printf("%d\n", ans);
}
}
int main() {
#ifdef LOCAL
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
// IOS
init();
int t;scanf("%d", &t);
int cnt = ;
while(t--){
printf("Case %d:\n", cnt++);
solve();
}
return ;
}

还有一个用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. MySql-Mysql技术内幕~SQL编程学习笔记(N)

    1._rowid 类似Oracle的rowid mysql> ; +-------+----+----------------+-------------+---------------+--- ...

  2. 极*Java速成教程 - (4)

    Java语言基础 多态 多态是面向对象的一大重要特性,如果说封装是隐藏一个类怎么做,继承是确定一系列的类做什么,那多态就是通过手段去分离做什么和怎么做. 向上转型与收窄 在开发者将一类事物封装成类以后 ...

  3. 《剑指offer》面试题23 从上往下打印二叉树 Java版

    注意层序遍历的时候对每一层的处理方式可能不同,这里把每一层的元素保存进一个List中了,那么就需要记录每一层的数量. public List<List<Integer>> se ...

  4. linux中文件属性

    一. linux系统如何管理文件 1.1. 硬盘中的静态文件和inode a. 文件平时都在存放在硬盘中的,硬盘中存储的文件以一种固定的形式存放的,我们叫静态文件. b. 一块硬盘中可以分为两大区域: ...

  5. Simpsons’ Hidden Talents

    Homer: Marge, I just figured out a way to discover some of the talents we weren’t aware we had. Marg ...

  6. lambda map() filter() zip()练习

    练习: 用map来处理字符串列表,把列表中所有人都变成sb,比方alex_sb l=[{'name':'alex'},{'name':'y'}] l=[{'name':'alex'},{'name': ...

  7. Linux命令(持续更新)

    1. tail 命令    tail 命令可用于查看文件的内容,有一个常用的参数 -f 常用于查阅正在改变的日志文件. tail  -f  filename 会把 filename 文件里的最尾部的内 ...

  8. C++ 中头文件<bits/stdc++.h>的优缺点

    在编程竞赛中,我们常见一个头文件: #include <bits/stdc++.h> 发现它是部分C++中支持的一个几乎万能的头文件,包含所有的可用到的C++库函数,如<istrea ...

  9. vuex介绍和vuex数据传输流程

    1.什么是vuex? 公共状态管理:解决多个非父子组件传值麻烦的问题:简单说就是多个页面都能用Vuex中store公共的数据 a.并不是所有的数据都要放在Vuex中,只有各个组件公用的一些数据会放在V ...

  10. 理解PHP面向对象三大特性

    一.封装性 目的:保护类里面的数据,让类更安全, protected和private只能在类中或子类访问,通过public提供有限的接口供外部访问,封装是控制访问,而不是拒绝访问 封装关键字:publ ...