CF1557C 题解
xianggua154 · · 题解
模拟赛考了这个,考场最后 20 分钟推出结论,于是发题解。
考场上首先看到的是子任务数据范围中的
于是就有了代码里的特判,我也不知道能不能删。反正写着吧。
以下是题解。
题面:In Luogu,In CF。
题意:有
条件:
-
-
a_1 \& a_2 \& a_3 \& ... \& a_n \ge a_1 \oplus a_2 \oplus a_3 \oplus ... \oplus a_n
满足
这里令:
由题得
注意到如果要使
| 要求 | ||
|---|---|---|
由此得出结论:
所以,我们只需要要求
所以我们讨论
-
当
n 为奇数时,每一位会有2^{n-1}+1 种可能^{[1]} ,根据乘法原理,答案为(2^{n-1}+1)^k 。 -
当
n 为偶数时,我们分两种情况讨论。枚举每一个
i ,将所有情况 2 都算一遍,所有情况方案数之和即为答案。 -
由于快速幂,时间复杂度为
#include<bits/stdc++.h>
#define f(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
#define int long long
const int mod=1000000007;
int t,n,k;
// 快速幂
int fpow(int x,int y){
if(y<0)return 0;
int s=1;
while(y){
if(y&1)s=s*x%mod;
x=x*x%mod,y>>=1;
}
return s;
}
signed main(){
cin>>t;
while(t--){
cin>>n>>k;
if(k==0){
cout<<"1\n";
}
else if(k==1){
cout<<(fpow(2,n-1)+(n&1))%mod<<'\n';
}
else if(n&1){
cout<<fpow(fpow(2,n-1)+1,k)<<'\n';
}
else{
int a=fpow(2,n-1)-1;
int b=fpow(2,n);
int ans=fpow(a,k);
f(i,1,k)
ans=(ans+fpow(a,k-i)*fpow(b,i-1)%mod)%mod;
cout<<ans<<'\n';
}
}
return 0;
}
注: