题目链接http://codeforces.com/gym/101848/problem/D
题意:
给出n,k,p,问在集合{ 0 0 0, 1 1 1, 2 2 2, 3 3 3,… 2 n − 1 2^{n}-1 2n−1}中,存在多少个子集,使得集合内所有元素的异或恰好为k,答案对p取模
题解:
一共有
2
2
n
2^{2^{n}}
22n个子集,每一位是1或0的概率是
1
2
\frac{1}{2}
21,所以答案就是
2
2
n
×
1
2
n
2^{2^{n}}\times\frac{1}{2^{n}}
22n×2n1。
注意:如果k是0,需要减1,因为空集会被计算。
欧拉降幂:
a
b
=
a
b
%
p
h
i
(
c
)
+
p
h
i
(
c
)
%
c
a^{b}=a^{b\%phi(c)+phi(c)}\%c
ab=ab%phi(c)+phi(c)%c
注意:如果p取2需要特判一下。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=2e5+7;
const ll inf=1e9+7;
ll n,k,p;
ll qpow(ll a,ll b,ll c){
ll res=1;
while(b){
if(b&1) res=res*a%c;
a=a*a%c;
b>>=1;
}
return res;
}
int main(){
scanf("%lld%lld%lld",&n,&k,&p);
if(p==2){
printf("%d\n",k==0?1:0);
return 0;
}
ll tmp=qpow(2,n,p-1);
ll ans=qpow(2,tmp+p-1,p)*qpow(qpow(2,n,p),p-2,p)%p;
if(k==0) ans=(ans-1+p)%p;
printf("%lld\n",ans);
return 0;
}