地址:https://nanti.jisuanke.com/t/31453
题意:题意:n个数字排成一圈,每个数字范围[0, 2^k-1],问有多少种不同的序列满足对于所有相邻的两个数字,它们同或值不能为0,其中第一个数字和最后一个数字也算相邻。(0
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const int N = 1e6 + 5;
LL pow_mod(LL n,LL m)
{
LL sum = 1;
LL x = n;
while(m)
{
if(m & 1) sum = sum * x % mod;
m >>= 1;
x = x * x % mod;
}
return sum;
}
LL me[N];
LL dfs(int n,int k)
{
if(n == 1){
return me[k];
}
if(n == 2){
return me[k] * (me[k] - 1) % mod;
}
return (me[k] * (me[k] - 2) % mod * pow_mod(me[k] - 1,n - 2) % mod + dfs(n - 2,k)) % mod;
}
int main()
{
me[0] = 1;
for(int i = 1;i <= 1000000;++i)
{
me[i] = me[i - 1] * 2LL % mod;
}
int t;
scanf("%d",&t);
while(t--)
{
int n,k;
scanf("%d %d",&n,&k);
//递推式求解:a[n] = 2^k*(2^k - 1)^(n - 2) * (2^k - 2) + a[n - 2];
printf("%lld\n",dfs(n,k));
}
return 0;
}