cf449D. Jzzhu and Numbers(容斥原理 高維字首和)
題意
給出\(n\)個數,問任意選幾個數,它們\(\&\)起來等於\(0\)的方案數
Sol
正解居然是容斥原理Orz,然而本蒟蒻完全想不到。。
考慮每一種方案
答案=任意一種方案 - 至少有\(1\)位為\(1\)的方案 + 至少有兩位為\(1\)的方案 - 至少有三位為\(1\)的方案
至少有\(i\)位為\(1\)的方案可以dp算,設\(f[x]\)表示滿足\(f[x] = a_i \& x = x\)的\(a_i\)的個數
最終答案$ = (-1)^{bit(i)} f[i]$
\(f\)陣列可以通過高維字首和預處理
#include<bits/stdc++.h> #define Pair pair<int, int> #define MP make_pair #define fi first #define se second using namespace std; const int MAXN = 3e6 + 10, mod = 1e9 + 7, B = 20; inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int N, a[MAXN], bit[65537], f[MAXN]; int add(int &x, int y) { if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y); } int mul(int x, int y) { return 1ll * x * y % mod; } int fp(int a, int p) { int base = 1; while(p) { if(p & 1) base = mul(base, a); a = mul(a, a); p >>= 1; } return base; } int get1(int x) { // return __builtin_popcount(x); return bit[x & 65535] + bit[x >> 16]; } int main() { for(int i = 1; i <= 65536; i++) bit[i] = bit[i >> 1] + (i & 1); N = read(); for(int i = 1; i <= N; i++) a[i] = read(), f[a[i]]++; int Lim = (1 << B) - 1, ans = 0; for(int i = 0; i <= 20; i++) for(int sta = 0; sta <= Lim; sta++) if(!(sta & (1 << i))) add(f[sta], f[sta | (1 << i)]); for(int sta = 0; sta <= Lim; sta++) { int k = (get1(sta) & 1) ? -1 : 1; add(ans, mul(k, fp(2, f[sta]))); } cout << ans; return 0; }
相關推薦
cf449D. Jzzhu and Numbers(容斥原理 高維字首和)
題意 題目連結 給出\(n\)個數,問任意選幾個數,它們\(\&\)起來等於\(0\)的方案數 Sol 正解居然是容斥原理Orz,然而本蒟蒻完全想不到。。 考慮每一種方案 答案=任意一種方案 - 至少有\(1\)位為\(1\)的方案 + 至少有兩位為\(1\)的方案 - 至少有三位為\(1\)的方案
Codeforces Round #257 (Div. 1) D - Jzzhu and Numbers 容斥原理 + SOS dp
name size num space \n force ret namespace lse D - Jzzhu and Numbers 這個容斥沒想出來。。。 我好菜啊。。 f[ S ] 表示若幹個數 & 的值 & S == S得 方案數, 然後用這個
codeforces 449 D Jzzhu and Numbers(容斥+dp)
這題真的爆炸難懂...待補。 程式碼: #include <bits/stdc++.h> using namespace std; const int mod=1e9+7; const
CF449D Jzzhu and Numbers (狀壓DP+容斥)
進行 狀態 位與 不能 gif using com 答案 {} 題目大意: 給出一個長度為n的序列,構造出一個序列使得它們的位與和為0,求方案數 也就是從序列裏面選出一個非空子集使這些數按位與起來為0. 看了好久才明白題解在幹嘛,我們先要表示出兩兩組合位與和為0的所有情況
bzoj 2986 Non-Squarefree Numbers 容斥原理+數學
代碼 bre define com amp check href break www. 題面 題目傳送門 解法 顯然可以二分答案 計算的時候用容斥原理即可 用莫比烏斯函數實現這個過程即可 代碼 #include <bits/stdc++.h> #define L
Jzzhu and Numbers CodeForces - 449D (高維前綴和,容斥)
\n algorithm codeforce sign div str += highlight 超時 大意: 給定集合a, 求a的按位與和等於0的非空子集數. 首先由容斥可以得到 $ans = \sum \limits_{0\le x <2^{20}}
Devu and Flowers lucas定理+容斥原理
原理 容斥原理 title pac cond rst like with lld Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box con
Codeforces 451E Devu and Flowers【容斥原理+盧卡斯定理】
d+ 題意 while markdown post mark 色相 esp printf 題意:每個箱子裏有\( f[i] \)種顏色相同的花,現在要取出\( s \)朵花,問一共有多少種顏色組合 首先枚舉\( 2^n \)種不滿足條件的情況,對於一個不被滿足的盒子,我們至
Codeforces 451 E. Devu and Flowers(組合數學,數論,容斥原理)
傳送門 解題思路: 假如只有 s 束花束並且不考慮 f ,那麼根據隔板法的可重複的情況時,這裡的答案就是 假如說只有一個 f 受到限制,其不合法時一定是取了超過 f 的花束 那麼根據組合數,我們仍然可以算出其不合法的解共有: 最後,由於根據容斥,減兩遍的東西要加回來,那麼含有偶數個 f 的項
Codeforces483B. Friends and Presents(二分+容斥原理)
題目連結:傳送門 題目: B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input standard input output standard out
Smith Numbers(尤拉函式,容斥原理)
While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,noticed that the telephone number of his brother-in-law
UVa 11481 Arrange the Numbers (組合數學+容斥原理)
UVa 11481 Arrange the Numbers 題目大意: 可以將序列1,2,3,...n任意重排,但重排後的前m(m≤n)個位置恰好有k(k≤m)個不變,求方案數除以1000000
POJ 1091 容斥原理
.org 質因子 blank dfs tar cin href strong 元組 鏈接: http://poj.org/problem?id=1091 題意: 給你兩個正整數n,m,讓你求長度為n+1的滿足條件的一個等式:a[1]*x1+a[2]*x2+a[3]*x
容斥原理
clu images class 又是 對象 title href 推理 計算 容斥原理(Inclusion–exclusion principle),是指在計數時,必須註意無一重復,無一遺漏,為了使重疊部分不被重復計算,人們研究出一種新的計數方法。這種方法的基
POJ 2773 容斥原理
for log cto tor ans 個數 ret num void 鏈接: http://www.cnblogs.com/MashiroSky/p/5913989.html 題意: 給出兩個數m,k,要求求出從1開始與m互質的第k個數。 題解: 二分一個答案m
{容斥原理}
.com lld hide mes problem fine efi lose -a 題目鏈接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1284 比較基礎練一下。 1 #include
POJ3904 Sky Code【容斥原理】
define 原理 tail n-1 pop blog soft ace tdi 題目鏈接: http://poj.org/problem?id=3904 題目大意: 給你N個整數。從這N個數中選擇4個數,使得這四個數的公約數為1。求滿足條件的 四元組個數。
洛谷P1450 [HAOI2008]硬幣購物 動態規劃 + 容斥原理
string -1 line sum mes 開始 clas 完全背包 預處理 洛谷P1450 [HAOI2008]硬幣購物 動態規劃 + 容斥原理 1、首先我們去掉限制 假設 能夠取 無數次 也就是說一開始把他當做完全背包來考慮 離線DP 預處理 復雜度 4*v
hdu 1695 GCD(歐拉函數+容斥原理)
spi fin clu init mod long long tac push_back gcd http://acm.hdu.edu.cn/showproblem.php?pid=1695 非常經典的題。同一時候感覺也非常難。 在區間[a,b]和[c,d]內分
洛谷1002 容斥原理+dfs OR DP
amp define its name sign pri last += include //By SiriusRen #include <bits/stdc++.h> using namespace std; #define int long lon