题目链接:HDU 3658
题目大意:题目的意思是在52个英文字母里面选择m个字母组成一个字符串,满足以下两个条件。第一是相邻的两个字符的ASCLL码的绝对值小于等于32,第二至少要有一对的字符的绝对值为32
分析:
挺好想的. 我们只要把第一次求出的所有的个数减去“相邻的两个字符的ASCLL码的绝对值小于等于31”的即可.
套个矩阵快速幂板子完事, 就是答案相减的时候注意下可能会是负的.
以下是代码:
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define Fi first
#define Se second
#define ll long long
#define inf 0x3f3f3f3f
#define lowbit(x) (x&-x)
#define PLL pair<ll,ll>
#define PII pair<int,int>
#define l_inf 0x3f3f3f3f3f3f3f
#define mmin(a,b,c) min(a,min(b,c))
#define mmax(a,b,c) max(a,max(b,c))
#define debug(a) cout<<#a<<"="<<a<<endl;
#define Sc_P(x) scanf("%d%d",&x.Fi,&x.Se)
#define Sc_PL(x) scanf("%lld%lld",&x.Fi,&x.Se)
#define debug2(a,b) cout<<#a<<"="<<a<<" "<<#b<<"="<<b<<endl;
using namespace std;
const int N=3e5+10;
const int maxn=55;
const ll Mod=1e9+7;
ll tmp[maxn][maxn];
void multi(ll a[][maxn],ll b[][maxn],int n)//n是矩阵大小
{
int i,j,k;
memset(tmp,0,sizeof tmp);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
for(k=1;k<=n;k++)
{
tmp[i][j]+=a[i][k]*b[k][j];
tmp[i][j]%=Mod;
}
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
a[i][j]=tmp[i][j];
}
ll res[maxn][maxn];
void Pow(ll a[][maxn],ll m,int n)// a 是初始矩阵 res是答案数组 m是幂,n是矩阵大小
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
res[i][j]=(i==j);
while(m)
{
if(m&1) multi(res,a,n); //res=res*a;
multi(a,a,n); //a=a*a
m>>=1;
}
}
void Show(int n,ll res[][maxn])
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
cout<<res[i][j]<<" ";
}
cout<<endl;
}
}
ll a[maxn][maxn];
ll solve(int x,ll n)
{
memset(a,0,sizeof a);
for(int i=1;i<=52;i++)
{
for(int j=-x;j<=x;j++)
{
int k=i+j;
if(k<1||k>52)continue;
a[i][k]=1;
}
}
Pow(a,n,52);
ll ans=0;
for(int i=1;i<=52;i++)
{
for(int j=1;j<=52;j++)
{
ans+=res[i][j];
ans%=Mod;
}
}
return ans;
}
int main()
{
int T;scanf("%d",&T);
ll n;
while(T--)
{
scanf("%lld",&n);
ll ans1=solve(26,n-1);
ll ans2=solve(25,n-1);
ll ans=(ans1-ans2+Mod)%Mod;
printf("%lld\n",ans);
}
return 0;
}