COCI2017-2018#3 Dojave || 洛谷P4443
The biggest event of the year ended tragically for Croatian teams. The most influential theoretician of CERC of all time, the founder of the popular page CERC Tips, and in his free time an outstanding bass player, in his most recent performance failed to get his team to the finals.
In order to get over his existential troubles, our subject is spending time playing games of chance. He is especially interested in the following game:
You are given a positive integer M. Our protagonist sees in front of him a permutation of an array of numbers 0, 1, 2, ..., 2^M2M - 1.
The computer chooses a nonempty contiguous subsequence of the given permutation, which it then lights up over a capital city of one of the countries in Southeastern Europe.
Our confidant, after fighting off tears caused by memories of old times, must choose two distinct elements of the permutation and swap their places. Our man of the hour wins if and only if the bitwise XOR of the numbers in the lit up subsequence after the substitution is precisely 2^M2M - 1.
Our hero wants to know the number of contiguous subsequences the computer can light up so that he can win.
Help our hero overcome his (id)entity crisis so our favourite page can be fully active again.
输入输出格式
输入格式:
The first line of input contains the integer M (1 ≤ M ≤ 20),
The following line contains 2^M2M space-separated numbers that make up a permutation of the array 0, 1, 2, ..., 2^M2M - 1.
输出格式:
You must output the total number of contiguous subsequences that a computer can light up so our hero can win.
输入输出样例
说明
In test cases worth 50% of total points, it will hold 1 ≤ M ≤ 14.
Clarification of the test cases:
In the first test case, if the computer chooses the subsequence [1 2 3], our hero can replace the numbers 0 and 3. In this case, he can actually win for every chosen contiguous subsequence, except the entire array.
In the second test case, if the computer chooses the entire array [3 7 0 4 6 1 5 2] as the lit up subsequence, our hero can’t change the XOR of the subsequence (which is 0), no matter which two elements are swapped.
https://www.luogu.org/problemnew/show/P4443
题目大意 ::
给定一个正整数M,和0到2M-1共2M个数的排列,计算机先选择一段连续的子串,你必须选两个不同的元素出来,交换它们的位置,使得计算机选出来的子串在交换后的元素的异或值为2M-1,如果可以,你将获胜。现在请你统计有多少种不同的选法,你可以获胜。
就是叫你求出所有满足条件的区间的个数。。。
官方标程是 线段树 + !@#¥%…… 我看不懂......(orz洛谷大佬写的线段树)......
线段树 :: //这个真的看不懂QAQ......
//for test!
#include <bits/stdc++.h>
using namespace std;
#define ms(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORd(i, a, b) for (int i = (a) - 1; i >= (b); i--)
#define FORall(it, a) for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
#define sz(a) int((a).size())
#define present(t, x) (t.find(x) != t.end())
#define all(a) (a).begin(), (a).end()
#define uni(a) (a).erase(unique(all(a)), (a).end())
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define prec(n) fixed<<setprecision(n)
#define bit(n, i) (((n) >> (i)) & 1)
#define bitcount(n) __builtin_popcountll(n)
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;
typedef vector<pi> vii;
;
;
const int INF = (int) 1e9;
const ll LINF = (ll) 1e18;
);
;
inline ll gcd(ll a, ll b) {ll r; while (b) {r = a % b; a = b; b = r;} return a;}
inline ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
inline ll fpow(ll n, ll k, ; ) {) r = r * n % p; n = n * n % p;} return r;}
template< : ;}
template< : ;}
inline ll isqrt(ll k) {ll r = sqrt(k) + ; while (r * r > k) r--; return r;}
inline ll icbrt(ll k) {ll r = cbrt(k) + ; while (r * r * r > k) r--; return r;}
inline void addmod(int& a, int val, int p = MOD) {if ((a = (a + val)) >= p) a -= p;}
inline ) a += p;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}
inline , p);}
inline : x > +EPS;}
inline int sign(ld x, ld y) {return sign(x - y);}
#define db(x) cerr << #x << " = " << (x) << " ";
#define endln cerr << "\n";
<< ;
int n;
int a[maxn];
int f[maxn];
];
int mn[maxn];
int mx[maxn];
][maxn << ];
void updmn(int st[], int p, int val) {
; ) p >>= , st[p] = min(st[p << ], st[p << | ]);
}
int querymn(int st[], int l, int r) {
int res = INF;
; l < r; l >>= , r >>= ) {
) chkmin(res, st[l++]);
) chkmin(res, st[--r]);
}
return res;
}
void updmx(int st[], int p, int val) {
; ) p >>= , st[p] = max(st[p << ], st[p << | ]);
}
int querymx(int st[], int l, int r) {
int res = -INF;
; l < r; l >>= , r >>= ) {
) chkmax(res, st[l++]);
) chkmax(res, st[--r]);
}
return res;
}
void solve() {
cin >> n; n = << n;
FOR(i, , n) mn[i] = INF, mx[i] = -INF;
FOR(i, , n) {
cin >> a[i];
chkmin(a[i], a[i] ^ n - );
chkmin(mn[a[i]], i);
chkmax(mx[a[i]], i);
}
) {
cout << << "\n";
return;
}
FOR(i, , n) {
updmn(st[], i, mn[a[i]]);
updmx(st[], i, mx[a[i]]);
}
;
FOR(i, , n) {
int k = a[i];
if (mx[k] == i) {
f[i] = -;
], mn[k], mx[k]);
if (j >= mn[k]) {
f[i] = j;
}
else {
f[i] = f[mx[a[j]]];
}
&& querymx(st[], f[i], i) <= i) {
;
& ;
dp[i][par] = ;
if (f[i]) {
dp[i][par] += dp[f[i] - ][];
dp[i][par ^ ] += dp[f[i] - ][];
}
res -= dp[i][];
}
}
}
res += () / ;
cout << res << "\n";
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(), cin.tie();
) {
assert(freopen(argv[], "r", stdin));
}
) {
assert(freopen(argv[], "wb", stdout));
}
solve();
;
}//标程....
但是我只会写写被数据卡到飞起的hash......怎么办?一个hash不行就2个嘛......
hash : : //代码极丑.......毕竟是蒟蒻.......体谅下
//hash 有毒, 双hash才可过 ;
#include <cstdio>
#include <map>
using namespace std;
typedef long long ll;
<< ;
map<pair<ll, ll>, ];
; return c = 1ll * c * 15315ll % 0x7fffffff; }
], pos[N + ], f1[N + ], f2[N + ], r1[N + ], r2[N + ];
int main() {
scanf( << n; ll ans = 1ll * c * (c + ) / 2ll;
; i <= c; i ++) scanf("%d", a + i), pos[a[i]] = i;
; i <= c / ; i ++) r1[pos[i]] = r1[pos[i ^ (c-)]] = rand(), r2[pos[i]] = r2[pos[i ^ (c-)]] = rand();
; i <= c; i ++) f1[i] = f1[i-] ^ r1[i], f2[i] = f2[i-] ^ r2[i];
; i <= c; i ++) ans -= p[i % ][make_pair(f1[i], f2[i])], p[i % ][make_pair(f1[i], f2[i])] ++;
printf(n == ? " : "%lld\n", ans);
}
证明如下 ::
设 k = (1 << M) - 1; pos[] 表示值为 x 的数的位置;
首先考虑区间长度为奇数的时候, 设区间的xor值为 A, 则将其中某个数换走后的xor值为 A ^a[i], 那么要使区间 xor值 为 k, 则需要换一个 a[j] = k ^ A ^a[i] (这不是废话么......), 设 g = k ^ A(这是个定值)
假设该区间不合法, 则有 pos[g ^ a[1]] ∈ [l, r], pos[g ^ a[2]] ∈ [l, r]......,因为 a[i] != a[j], 所以g ^ a[i] != g ^a[j](又是废话),
所以g ^ a[1] ^ g ^ a[2] ^ .... g ^ a[n] = a[1] ^ a[2] ^ ...^a[n], 所以g ^ a[1] ^ a[2] ^ ... ^a[n] == a[1] ^ a[2] ^ ... ^ a[n], 所以当前情况 g = 0。。。又 g = k ^ A,所以 k == A (与假设矛盾)。 所以当区间长度为奇数时一定是
合法区间。
好, 现在开始考虑区间长度为偶数时 先讨论 区间长度 / 2 为奇数时
同样,如果为不合法区间 (假设) ,则有 a[ i ] ^ g = a[ j ], pos[ a[ i ] ] ∈ [l, r], pos[ a[ j ] ] ∈ [l, r]; 又因为 g = a[ i ] ^ a[ j ]; 所以最后区间的 xor 值一定为 g (自己想一想)。
所以 A = a[i] ^ a[j]。 g = K ^ A = a[i] ^a[j]; k ^ a[i] ^ a[j] = a[i] ^a[j]。(好像在哪里见过。。。。)
现在讨论区间长度为偶数且区间长度 / 2 为偶数的情况
同上,区间的 xor 值 A 为 0(不想解释了,反正上面看懂了也很简单), 则有 g = K(g = k ^ A), 所以 a[ i ] ^ a[ j ] = k (对于任意a[i]都只有一个a[j]与之异或等于K,不想证明了), 所以到这里我们就把所有不合法区间的条件求出了。
总结下,现在我们知道了区间的总数为 (k + 2) * (k + 1) / 2,区间长度为4的倍数且其中的数都是配♂对的数的区间不合法,减去,双hash乱搞ok(一个hash让我Wa到怀疑人生);
证明完毕。。。(我语文很差,可能有的地方表达有误。谁看懂标程的可以说下否?)

COCI2017-2018#3 Dojave || 洛谷P4443的更多相关文章
- 2018.07.01洛谷P2617 Dynamic Rankings(带修主席树)
P2617 Dynamic Rankings 题目描述 给定一个含有n个数的序列a[1],a[2],a[3]--a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i ...
- 2018.07.17 洛谷P1368 工艺(最小表示法)
传送门 好的一道最小表示法的裸板,感觉跑起来贼快(写博客时评测速度洛谷第二),这里简单讲讲最小表示法的实现. 首先我们将数组复制一遍接到原数组队尾,然后维护左右指针分别表示两个即将进行比较的字符串的头 ...
- 2018.12.30 洛谷P4238 【模板】多项式求逆
传送门 多项式求逆模板题. 简单讲讲? 多项式求逆 定义: 对于一个多项式A(x)A(x)A(x),如果存在一个多项式B(x)B(x)B(x),满足B(x)B(x)B(x)的次数小于等于A(x)A(x ...
- 2018.11.09 洛谷P1110 [ZJOI2007]报表统计(multiset)
传送门 sb题. 直接用两个multisetmultisetmultiset维护相邻两个数的差值和所有数的前驱后继. 插入一个数的时候更新一下就行了. 代码: #include<bits/std ...
- 2018.11.06 洛谷P1099 树网的核(最短路+枚举)
传送门 之前看李煜东的书一直感觉是道神题. 然后发现这题数据范围只有300?300?300? 直接上floydfloydfloyd然后暴力就完了啊. 代码: #include<bits/stdc ...
- 2018.11.06 洛谷P1941 飞扬的小鸟(背包)
传送门 上升看成完全背包. 下降看成01背包. 注意边界转移就行了. 代码: #include<bits/stdc++.h> using namespace std; inline int ...
- 2018.11.04 洛谷P2679 子串(线性dp)
传送门 为什么前几年的noipnoipnoip总是出这种送分题啊? 这个直接线性dpdpdp不就完了吗? f[i][j][k][0/1]f[i][j][k][0/1]f[i][j][k][0/1]表示 ...
- 2018.11.04 洛谷P1081 开车旅行(倍增)
传送门 思路简单码量超凡? 感觉看完题大家应该都知道是倍增sbsbsb题了吧. 首先预处理出从每个点出发如果是AAA走到哪个点,如果是BBB走到哪个点. 然后利用刚刚预处理出的信息再预处理从每个点出发 ...
- 2018.11.02 洛谷P2661 信息传递(拓扑排序+搜索)
传送门 按照题意模拟就行了. 先拓扑排序去掉不在环上面的点. 剩下的都是简单环了. 于是都dfsdfsdfs一遍求出最短的环就行. 代码: #include<bits/stdc++.h> ...
随机推荐
- 基于jquery 的插件,让IE支持placeholder属性
开发一个项目的时候为了美观和用户体验用到了input标签的placeholder属性,但是这个属性是html5中的,所以低版本的IE浏览器不支持.于是在百度找了一些解决方法,找了好几个都不是那么完美, ...
- c语言学习笔记 if语句的条件判断
可能经常会看到错误的if语句示范,比如这样的: if(a=6) { printf("hello"); } if语句块执行的条件是if条件的运算结果不是0则执行if语句块. a=6这 ...
- R语言的并行运算(CPU多核)
通常R语言运行都是在CPU单个核上的单线程程序.有时我们会有需求对一个向量里的元素应用相同的函数,最终再将结果合并,并行计算可以大幅节约时间. 为了支持R的并行运算, parallel包已经被纳入了R ...
- Luogu 4069 [SDOI2016]游戏
BZOJ 4515 树链剖分 + 李超线段树 要求支持区间插入一条线段,然后查询一个区间内的最小值.可以使用李超线段树解决,因为要维护一个区间内的最小值,所以每一个结点再维护一个$res$表示这个区间 ...
- JMS-消息中间件的应用01-基本概念-来自慕课学习-新手学习
什么是JMS? Java消息服务(Java Message Service),即JMS,是一个java平台中关于面向消息中间件的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信. ...
- 原生ajax访问服务器所展现的现象
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>ajax ...
- AES加密 AESCrypt 类
/// <summary> /// AES加密 /// </summary> public sealed class AESCrypt { /// <summary> ...
- 使用Boost库(1)
如何说服你的公司.组织使用Boost库 one of the most highly regarded and expertly designed C++ library projects in th ...
- 「HNOI2013」切糕
题目链接 戳我 \(Solution\) 对于这道题,我们首先来看看没有\(D\)这个约束的该如何做. 我们考虑构造最小割模型. 其实直接贪心就好了,选出每条路径上的最小值就好了(路径就是将每层的同一 ...
- [SinGuLaRiTy] 树链问题
[SinGuLaRiTy-1035] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 关于树链 树链是什么?这个乍一看似乎很陌生的词汇表达的其 ...