【ACM-ICPC 2018 徐州賽區網路預賽 A. Hard to prepare】 DP
阿新 • • 發佈:2018-12-10
題目連結 https://nanti.jisuanke.com/t/31453 題意 一個圓環,每個位置可以選擇2^k中任意一個數,要求相鄰位置異或不等於 做法 考慮遞推時的做法,如果我們把這道題轉換為求一條鏈上的方案數,就很容易遞推,但是由於環有一個限定就是開頭結尾可能完全相反,導致方案數會改變,於是我們就多開一個數組存這種方案進行轉移即可。 具體為 : 程式碼
#include<stdio.h>
#include<iostream>
using namespace std;
typedef long long ll;
const int Mod = 1e9+7;
const int maxn = 1e6+10;
ll a[maxn],b[maxn],c[maxn];
ll pow_(ll x,int y)
{
ll ans=1;
while(y)
{
if(y&1) ans=(ans*x)%Mod;
x=(x*x)%Mod;
y>>=1;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,k;
scanf("%d%d",&n,&k);
a[1]=pow_(2,k);
b[1]=0;
c[1]=0;
for(int i=2;i<=n;i++)
{
a[i]=(a[i-1]+c[i-1])%Mod;
b[i]=(b[i-1]+c[i-1])%Mod;
c[i]=((a[i-1]+b[i-1])*(pow_(2,k)-2+Mod)%Mod+c[i-1]*((pow_(2,k)-3+Mod)%Mod)%Mod)%Mod;
}
ll ans=0;
ans=(a[n]+c[n])%Mod;
printf("%lld\n",ans);
}
return 0;
}