2019 Nowcoder Multi-University Training Contest 1 H-XOR

由于每个元素贡献是线性的,那么等价于求每个元素出现在多少个异或和为$0$的子集内。因为是任意元素可以去异或,那么自然想到线性基。
先对整个集合A求一遍线性基,设为$R$,假设$R$中元素个数为$r$,那么任取一个不在$R$内的元素,$R$中肯定存在一种取法能和这个元素异或和为$0$。
同理,取定一个不在$R$内的元素,再随便取另外任意个不在$R$内的元素,$R$内仍然存在一种取法使得这个异或和为$0$。那么每个不在$R$内的元素包含在$2^{n - r - 1}$个集合内(其他不在$R$内的元素可以任取)。所以所有不在$R$内的元素对答案的贡献就是$\left(n - r + 1\right) \times 2^{n - r + 1}$。
考虑在$R$内的元素,最多只有$62$个元素。它们对答案的贡献其实就跟上面的一样。
对不在线性基内的元素求一遍线性基,设为$other$,这样其实就缩成了$2$个包含$62$个元素的集合。枚举$R$的每个元素$a_i$,把其他$123$个元素加入一个线性基中,设这个线性基为$temp$,元素个数为$r‘$,最后看$a_i$能否加入线性基中,如果不行就说明$temp$能让它异或和为$0$。同理可得对答案的贡献为$2^{n - r‘ - 1}$。
其实到这我只check了$a_i$能否加入$temp$,并没有check另外$n - r‘ - 1$个元素能否加入$temp$,但是其实在之前已经check过了,对于在$R$内和$other$的其他元素显然,因为我暴力枚举了它们去加入$temp$。对于其他元素其实都已经在得到$R$和$other$的时候尝试着让它们加入$R$和$other$,那么它们其实也是不能加入$temp$的。

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int sz = 70;
const int N = 1e5 + 10;
const ll MOD = 1e9 + 7;
bool vis[N];
vector<ll> G;
ll a[N], bin[N];

struct XO {
    ll p[sz];
    void init() {
        memset(p, 0, sizeof(p));
    }
    bool ins(ll x) {
        for (int i = 62; ~i; i--)
            if (x >> i & 1) {
                if (!p[i]) { p[i] = x; return true; }
                x ^= p[i];
            }
        return false;
    }
} R, other, temp;

int main() {
    int n;
    bin[0] = 1;
    for (int i = 1; i < N; i++) bin[i] = bin[i - 1] * 2 % MOD;
    while (~scanf("%d", &n)) {
        R.init(); other.init();
        G.clear();
        int r = 0;
        for (int i = 1; i <= n; i++) {
            scanf("%lld", &a[i]);
            vis[i] = 0;
            if (R.ins(a[i])) {
                r++;
                vis[i] = 1;
                G.push_back(a[i]);
            }
        }
        if (r == n) {
            puts("0");
            continue;
        }
        ll ans = (n - r) * bin[n - r - 1] % MOD;
        for (int i = 1; i <= n; i++) {
            if (!vis[i]) other.ins(a[i]);
        }
        for (auto x: G) {
            int tol = 0;
            temp.init();
            for (auto y: G) {
                if (x == y) continue;
                if (temp.ins(y)) tol++;
            }
            for (int i = 0; i <= 62; i++) {
                if (temp.ins(other.p[i])) tol++;
            }
            if (!temp.ins(x)) ans = (ans + bin[n - tol - 1]) % MOD;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/Mrzdtz220/p/11673051.html

时间: 2024-10-07 23:47:59

2019 Nowcoder Multi-University Training Contest 1 H-XOR的相关文章

2016 Multi-University Training Contest 1 H.Shell Necklace

Shell Necklace Time Limit: 16000/8000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 534    Accepted Submission(s): 227 Problem Description Perhaps the sea‘s definition of a shell is the pearl. However, in my view,

HDU校赛 | 2019 Multi-University Training Contest 3

2019 Multi-University Training Contest 3 http://acm.hdu.edu.cn/contests/contest_show.php?cid=850 1004. Distribution of books 考虑二分答案,设当前二分出来的是\(x\). 设\(f_i\)表示前\(i\)个能分成最多的段数,使得每一段和都\(\leqslant x\). 转移显然,枚举一个\(j\),若\(s_i-s_j\leqslant x\)则转移,\(s_i\)表示前

[最短路,最大流最小割定理] 2019 Multi-University Training Contest 1 Path

题目:http://acm.hdu.edu.cn/showproblem.php?pid=6582 Path Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 3747    Accepted Submission(s): 1075 Problem Description Years later, Jerry fell in love

HDU 6049 - Sdjpx Is Happy | 2017 Multi-University Training Contest 2

思路来源于 FXXL - - 一个比较奇怪的地方就是第三步可以不做,也就是ans至少为1,听说场内有提问的,然后 admin 说可以不做- - (wa的我心烦) /* HDU 6049 - Sdjpx Is Happy [ 枚举,剪枝 ] | 2017 Multi-University Training Contest 2 题意: 长度为N的排列 N <= 3000 排序分三个步骤: 1.原数组分为不相交的K段 2.每段都独立排序 3.选择其中两段swap 问按步骤能成功排序的K能取到的最大是多

2015 Multi-University Training Contest 10 hdu 5406 CRB and Apple

CRB and Apple Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 421    Accepted Submission(s): 131 Problem Description In Codeland there are many apple trees.One day CRB and his girlfriend decide

HDU4888 Redraw Beautiful Drawings(2014 Multi-University Training Contest 3)

Redraw Beautiful Drawings Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Alice and Bob are playing together. Alice is crazy about art and she has visited many museums around the world. She has

【2014 Multi-University Training Contest 3 1002】/【HDU 4888】 Redraw Beautiful Drawings

不容易啊,终于可以补第二个题了!! 顺便说一句:模版写残了就不要怪出题人啊 ~ (这残废模版研究了好长时间才找出错) 题目大意: 有一个n*m的矩阵,每一个格子里都将有一个数.给你每一行数字之和和每一列数字之和.求每一个位置能填0~k之间的哪个数.如果有多种可能输出"Not Unique",如果没有解输出"Impossible",如果一组解则将其输出. 解题思路: 最大流: 不可能的条件:是行之和和列之和不想等或者建图后的最大流与他们不想等. 多组的条件是:在最大流

2017 Multi-University Training Contest - Team 9 1005&amp;&amp;HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】

FFF at Valentine Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1060    Accepted Submission(s): 506 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any oth

2017 Multi-University Training Contest - Team 1 1002&amp;&amp;HDU 6034 Balala Power!【字符串,贪心+排序】

Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2668    Accepted Submission(s): 562 Problem Description Sample Input 1 a 2 aa bb 3 a ba abc Sample Output Case #1: 25 Case #2: 132

2015 Multi-University Training Contest 2 hdu 5303 Delicious Apples

Delicious Apples Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 1057    Accepted Submission(s): 354 Problem Description There are n apple trees planted along a cyclic road, which is L metres