首页 > 其他分享 >CF449D Jzzhu and Numbers

CF449D Jzzhu and Numbers

时间:2023-04-18 19:34:20浏览次数:54  
标签:ch 超集 CF449D Jzzhu 序列 getchar Numbers

CF449D Jzzhu and Numbers

黄金定律:给定序列求答案,但答案与序列顺序无关的题目,要么考虑把序列转权值序列,要么对序列排序。

二进制题按大小排序看起来就没啥用,那就转成权值序列。即,设 \(c(i)\) 表示 \(i\) 在 \(a\) 中的出现次数。同时设 \(V\) 为 \(a\) 的值域。

然后直接算发现不是很好算。考虑容斥计算。

设 \(g(x)\) 为 \(a\) 中为 \(x\) 超集的元素个数,即 \(g(x) = \sum_{i \supseteq x} c(i)\)。sosdp 板子解决。

设 \(f(x)\) 为 \(a\) 中,满足这个条件的子序列个数:该子序列中所有数 & 起来的值为 \(x\) 的超集。

我们发现,这样的子序列中,里面每个数必须都是 \(x\) 的超集,否则 & 起来之后 \(x\) 中的 1 位势必会有丢失,无法是 \(x\) 的超集。

换句话说,这个子序列必须从 \(g(x)\) 个数里选。

然后考虑怎么选。发现只要保证每个数都是 \(x\) 的超集,那么 & 起来之后 \(x\) 中的 1 位肯定不会丢,一定是 \(x\) 的超集。

所以选法是任意的,只要非空即可。在 \(g(x)\) 中任意选非零个数,选法自然是 \(2^{g(x)} - 1\)。

所以,\(f(x) = 2^{g(x)} - 1\)。

答案要求的是 & 起来为 \(0\) 的元素个数。

我们将满足这个条件的子序列称作满足第 \(i\) 个属性:该子序列中所有数 & 起来的值的二进制第 \(i\) 位为 \(1\)。

我们发现,比如同时满足 \(i_1\),\(i_2\),\(i_3\) 三个属性的子序列数量就对应了 \(f(2^{i_1} \operatorname{or} 2^{i_2} \operatorname{or} 2^{i_3})\)。

那我们要统计的就是不满足任意属性的子序列数量。

根据容斥原理可以得到答案为 \(\sum_{x = 0}^{V} f(x) \times -1^{\operatorname{popcount}(x)}\)。

时间复杂度 \(\Theta(n + V \log V)\)。

/*
 * @Author: crab-in-the-northeast 
 * @Date: 2023-04-18 19:02:58 
 * @Last Modified by: crab-in-the-northeast
 * @Last Modified time: 2023-04-18 19:23:43
 */
#include <bits/stdc++.h>
#define int long long
inline int read() {
    int x = 0;
    bool f = true;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        if (ch == '-')
            f = false;
    for (; isdigit(ch); ch = getchar())
        x = (x << 1) + (x << 3) + ch - '0';
    return f ? x : (~(x - 1));
}

const int mod = (int)1e9 + 7;
const int lg = 20;
int g[(1 << lg) + 5], pw2[(1 << lg) + 5];

signed main() {
    int n = read();
    for (int i = 1; i <= n; ++i)
        ++g[read()];
    pw2[0] = 1;
    for (int i = 1; i < (1 << lg); ++i)
        pw2[i] = pw2[i - 1] * 2 % mod;
    for (int j = 0; j < lg; ++j) {
        for (int i = (1 << lg) - 1; ~i; --i) {
            if ((i & (1 << j)) == 0) {
                int lst = i ^ (1 << j);
                (g[i] += g[lst]) %= mod;
            }
        }
    }

    int ans = 0;
    for (int x = 0; x < (1 << lg); ++x) {
        (ans += (pw2[g[x]] - 1) * (__builtin_parityll(x) ? -1 : 1) + mod) %= mod;
    }
    printf("%lld\n", ans);
    return 0;
}

标签:ch,超集,CF449D,Jzzhu,序列,getchar,Numbers
From: https://www.cnblogs.com/crab-in-the-northeast/p/cf449d.html

相关文章

  • Apple iWork(Pages、Numbers、Keynote)13.0 - 文档、电子表格、演示文稿
    请访问原文链接:https://sysin.org/blog/apple-iwork-13/,查看最新版。原创作品,转载请保留出处。作者主页:sysin.org苹果今天将其专为iOS和macOS设备设计的iWork应用套件更新为版本12(sysin),引入了许多新功能和改进功能。文档、电子表格、演示文稿,尽可集思广益。Pages......
  • 两数相加-Add Two Numbers-中等
    两数相加AddTwoNumbers[M]题目:https://leetcode.cn/problems/add-two-numbers/description/?favorite=2cktkvj讲解https://www.youtube.com/watch?v=wgFPrzTjm7s&ab_channel=NeetCodepublicListNodeaddTwoNumbers(ListNodel1,ListNodel2){ListNoded......
  • Codeforces Round #257 (Div. 1)B题Jzzhu and Cities(spfa+slf优化)
    题目地址:http://codeforces.com/contest/450/problem/D这题有重边,需要去重。。sad。当时居然没看见。。这题只要引入一个最短路条数,然后再遍历火车线,如果最短路与火车线长度相等,此时如果最短路条数是1的话,那说明这个最短路就是火车线,不能去掉,如果最短路条数大于1条,说明除了这条火车......
  • UVa 350 Pseudo-Random Numbers (伪随机数的循环长度)
    350-Pseudo-RandomNumbersTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=100&page=show_problem&problem=286Computersnormallycannotgeneratereallyrandomnumbers,butfrequentlyar......
  • UVa 443 / POJ 2247 Humble Numbers (4因子-丑数&STL灵活运用)
    443-HumbleNumbersTimelimit:3.000secondshttp://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=384http://poj.org/problem?id=2247Anumberwhoseonlyprimefactorsare2,3,5or7isc......
  • Semi-prime H-numbers UVA - 11105
     所有形如4n+1(n为非负整数)的数叫H数。定义1是唯一的单位H数,H素数是指本身不是1,且不能写成两个不是1的H数的乘积。H-半素数是指能写成两个H素数的乘积的H数(这两个数可以相同也可以不同)。 例如,25是H-半素数,但125不是。输入一个H数h(h≤1000001),输出1~h之间有多少个H-半素数。......
  • Sum of Consecutive Prime Numbers UVA - 121
     #include<iostream>#include<cstring>#include<cmath>#include<algorithm>usingnamespacestd;constintM=1e4+33;intb[M],c[M],tot;ints[M];voidinit(inttop){memset(b,1,sizeofb);b[1]=0;inti,j;......
  • 2019牛客暑期多校训练营(第四场) K numbers
    链接:https://ac.nowcoder.com/acm/contest/884/K?&headNav=acm&headNav=acm来源:牛客网 题目描述300iqlovesnumberswhoaremultipleof300.Onedayhegotastringconsistedofnumbers.Hewantstoknowhowmanysubstringsinthestringaremultiplesof300whe......
  • React数字滚动组件 numbers-scroll
    数字滚动组件,也可以叫数字轮播组件,这个名字一听就是非常普通常见的组件,第一反应就是想找找网上大佬的东西顶礼膜拜一下,这一搜,还真是没找到趁手的╮(╯▽╰)╭。最近接了大......
  • 1009. K-based Numbers
    1009.K-basedNumbershttps://acm.timus.ru/problem.aspx?space=1&num=1009 思路典型dp问题对于n位k位底的数,求不存在连续0出现的数目。设f(i)为i位,最后一位为......