题目链接: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. 将从model中获得的数据传到js函数中

    刚遇到了一种情况,从controller中获得的model是一个集合,需要将这个集合循环放到标签中,并且需要为这些标签添加点击事件,每个值传入对应的点击事件函数中,由于model中的值是通过${ite ...

  2. 解决MySql忘记密码

    描述:忘记了mysql的登录密码,无法登录的情况下该怎么办? 环境:CentOS 7,数据库:mysql 5.7 1.停止数据库(先查看mysql服务是否运行) # ps -ef | -i grep ...

  3. webapi接口统一返回请求时间

    webapi接口统一返回请求时间: public class BaseController : ControllerBase { protected ReturnResult<T> Res ...

  4. HDU1181 题解(Floyd最短路)

    题面: 变形课 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Subm ...

  5. Redis中的事务及乐观锁的实现

    介绍 Redis中的事务(transaction)是一组命令的集合.     事务同命令一样都是Redis最小的执行单位,一个事务中的命令要么都执行,要么都不执行.     Redis事务的实现需要用 ...

  6. InisghtFace 制作自定义数据集和模型训练评估

    前言 本文以lfw数据集进行示例 lfw结果集下载地址:http://vis-www.cs.umass.edu/lfw/lfw.tgz insightface源码下载地址:https://github ...

  7. MySQL之Foreign_Key

    MySQL之Foregin_Key 一\\一对多 一.员工表和部门表 dep emp 类似与我们将所有的代码都写在一个py文件内 确立标语表之间的关系 思路:一定要要换位思考问题(必须两方都考虑周全之 ...

  8. python基础知识的入门介绍

    一.什么是编程语言 任何词语都是一种高度的概括和总结,所以找关键字.如下: (1)1.什么是"语言":一个人与另一个人沟通的介质 2人将自己的思维逻辑和想法通过计算机能过识别的语言 ...

  9. R语言 绘图——条形图可以将堆积条形图与百分比堆积条形图配合使用

    在使用堆积条形图时候,新增一个百分比堆积条形图,可以加深读者印象. 封装一个function函数后只需要在调用的数据上改一下pos=‘fill’的代码即可.比较方便. 案例: # 封装函数 fun1& ...

  10. 时钟管脚设置问题 xilinx ERROR:Place:864 - Incompatible IOB's are locked to the same bank 0

    ERROR:Place:1108 - A clock IOB / BUFGMUX clock component pair have been found   that are not placed ...