Given two integers nnn and mmm, among all the sequences containing nnn non-negative integers less than 2m2^m2m, you need to count the number of such sequences AAA that there exists a non-empty subsequence of AAA in which the bitwise AND of the integers is 111.
Note that a non-empty subsequence of a sequence AAA is a non-empty sequence that can be obtained by deleting zero or more elements from AAA and arranging the remaining elements in their original order.
Since the answer may be very large, output it modulo a positive integer qqq.
The bitwise AND of non-negative integers AAA and BBB, A AND BA\ \texttt{AND}\ BA AND B is defined as follows:
- When A AND BA\ \texttt{AND}\ BA AND B is written in base two, the digit in the 2d2^d2d's place (d≥0d \ge 0d≥0) is 111 if those of AAA and BBB are both 111, and 000 otherwise.
For example, we have 4 AND 64\ \texttt{AND}\ 64 AND 6 = 444 (in base two: 100 AND 110100\ \texttt{AND}\ 110100 AND 110 = 100100100).
Generally, the bitwise AND of kkk non-negative integers p1,p2,…,pkp_1, p_2, \ldots, p_kp1,p2,…,pk is defined as
(…((p1 AND p2) AND p3) AND … AND pk)(\ldots((p_1\ \texttt{AND}\ p_2)\ \texttt{AND}\ p_3)\ \texttt{AND}\ \ldots\ \texttt{AND}\ p_k)(…((p1 AND p2) AND p3) AND … AND pk)
and we can prove that this value does not depend on the order of p1,p2,…,pkp_1, p_2, \ldots, p_kp1,p2,…,pk.
输入描述:
The only line contains three integers nnn (1≤n≤5 0001 \le n \le 5\,0001≤n≤5000), mmm (1≤m≤5 0001 \le m \le 5\,0001≤m≤5000) and qqq (1≤q≤1091 \le q \le 10^91≤q≤109).
输出描述:
Output a line containing an integer, denoting the answer.
输入
2 3 998244353
输出
17
示例2
输入
5000 5000 998244353
输出
2274146
题目意思求是长为n,每个数小于2^m的数组,存在子序列(不需要连续)按位与之后为1,这样的数组有多少个。
因为按位与之后要为1所以子序列中二进制最低位应该为1,其他每一位上至少有一个数在这一位有一个0才能使按位与之后这一位上是0,也就是说如果这一位全是1就不行。
所以,设k为按位与为1的子序列的长度,在n个数中选k个位置。对于每一位来说,k个数可以组成2^k种排列,但要去掉全为1的可能所以要减一,除了最后一位还有m-1位,每一位的可能数量相乘就可以得到长度位k的子序列的数量,剩下的位数要确保加上之后位于不为1,所以全放偶数(最后一位是0),因此可以得到下式。
代码如下
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=5010;
LL c[N][N];
LL n, m, q;
LL ksm(LL a, LL b, LL mod){
LL res = 1;
while(b){
if(b & 1){
res = (res * a) % mod;
}
a = (a * a) % mod;
b >>= 1;
}
return res;
}
int main()
{
cin >> n >> m >> q;
for(int i = 0;i < N;i ++){
for(int j = 0;j <= i;j ++){
if(!j) {
c[i][j] = 1;
}else {
c[i][j] = (c[i-1][j-1] + c[i-1][j]) % q;
}
}
}
LL ans = 0;
for(int i=1;i<=n;i++){
LL num = c[n][i] * ksm((ksm(2, i, q) - 1 + q) % q, m-1, q) % q * ksm(2, (n - i) * (m - 1), q) % q;
ans = (ans % q + num % q) % q;
}
cout << ans << '\n';
return 0;
}