Given two integer \(n\) and \(m\), among all the sequence containing n
non-negative integers less than \(2^m\), you need to count the number of such
sequences \(A\) that there exists a
non-empty subsequence of \(A\) in which
the bitwise AND of the integers is \(1\).
Since the answer may be very large, output it modulo a positive
integer \(q\).
Input
The only line contains three integers \(n,m,q\). (\(1 \le
n,m \le 5000, 1 \le q \le 10^9\))
Output
Output a line containing an integer, denoting the answer.
intksm(int x,int y){ int ans = 1; while (y) { if (y & 1) (ans *= x) %= mo; y >>= 1; (x *= x) %= mo; } return ans; }
signedmain(){ cin>>n>>m>>mo; pre(); if (m == 1) { int ans = ksm(2,n) - 1; if (ans < 0) ans += mo; cout<<ans<<'\n'; return0; } int ans = 0; f(i,0,n) { int tmp = ksm(ksm(2,i) - 1,m - 1); if (tmp < 0) tmp += mo; int oth = ksm(ksm(2,n - i),m - 1); int cnt = ((tmp * oth) % mo * C[n][i]) % mo; (ans += cnt) %= mo; } if (ans < 0) ans += mo; cout<<ans<<'\n'; return0; }
B
Given two integer \(n\) and \(m\), among all the sequence containing n
non-negative integers less than \(2^m\), you need to count the number of such
sequences \(A\) that there exists
at least two different non-empty subsequence of \(A\) in which the bitwise AND of the
integers is \(1\).
Since the answer may be very large, output it modulo a positive
integer \(q\).
Input
The only line contains three integers \(n,m,q\). (\(1 \le
n,m \le 5000, 1 \le q \le 10^9\))
Output
Output a line containing an integer, denoting the answer.
voidsolve1(){ pre(); int ans = 0; f(i,0,n) { int v = f[i] - 1; if (v < 0) v += mo; int tmp = (ksm(f[i], m - 1) - ksm(v,m - 1)) % mo; if (tmp < 0) tmp += mo; int oth = ksm(f[n - i],m - 1); int cnt = ((tmp * oth) % mo * C[n][i]) % mo; ans += cnt; } ans = (ksm(f[n],m) - ans) % mo; if (ans < 0) ans += mo; ans1 = ans; } signedmain(){ // freopen("in.in","r",stdin); // freopen("1.out","w",stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m>>mo; if (mo == 1) { cout<<"0\n"; return0; } solve1(); if (m == 1) { intfinal = f[n] - n - 1; final %= mo; if (final < 0) final += mo; cout<<final<<'\n'; return0; } f(i,0,n) { // i: the number of how many entry with the least significant bit being 1 if (i > m - 1) break; int sum = 0; int p = f[i] - 1; f(j,0,i) { //dp[i][j] = ksm(2^i - 1 - j, m - 1) int tmp = (dp[i][j] * C[i][j]) % mo; if (j & 1) tmp = (~tmp + 1); sum += tmp; } sum %= mo; // if (sum < 0) sum += mo; int oth = ksm(f[n - i],m - 1); int loc = ((sum * oth) % mo * C[n][i]) % mo; ans2 += loc; } intfinal = (ans1 - ans2) % mo; if (final < 0) final += mo; cout<<final<<'\n'; return0; }
/* int sum = 0; f(j,0,i - 1) { int tmp = ksm(n - j,m - 1) * C[i][j]; if (j & 1) tmp = -tmp; tmp %= mo; (sum += tmp) %= mo; } */