您的位置:首页 > 其它

HDU 6172 Array Challenge (打表找规律+矩阵快速幂 17多校第十场第2题)

2017-08-25 23:21 716 查看

题目链接

HDU6172

题意



分析

不得不佩服AC这道题的人脑洞,为什么你们这么熟练。。。打表找规律无所不能。

因为我们无法处理开方的取模问题,因此我们不能一步步求。我们令fn=⌊an−−√⌋,则打表看fn的前几项,f2=31 ,f3=197 ,f4=1255

发现fn符合类似于hn的递推式,fn=4fn−1+17fn−2−12fn−3,是个三阶线性递推式,用矩阵快速幂解决即可。

⎛⎝⎜fnfn−1fn−2⎞⎠⎟=⎛⎝⎜4101701−1200⎞⎠⎟n−4⎛⎝⎜f4f3f2⎞⎠⎟

由于转移矩阵出现了负数,取模的时候要注意!

代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <string>
#define ls (rt<<1)
#define rs (rt<<1|1)
#define bit(x) (1<<(x))
using namespace std;
typedef long long LL;
const double pi=4*atan(1.0);
const int INF=0x3f3f3f3f;
const double eps=1e-6;
const int MAXN=5;
const int MOD=1000000007;
struct Mat
{
LL mat[MAXN+1][MAXN+1];
}p,q,x;
Mat operator *(Mat a,Mat b)
{
Mat c;
memset(c.mat,0,sizeof(c.mat));
for (int k=1;k<=MAXN;k++)
{
for (int i=1;i<=MAXN;i++)
{
if (a.mat[i][k]==0) continue;
for (int j=1;j<=MAXN;j++)
{
if (b.mat[k][j]==0) continue;
c.mat[i][j]+=(((a.mat[i][k]*b.mat[k][j])%MOD+MOD)%MOD);///防止出现负数
c.mat[i][j]%=MOD;
}
}
}
return c;
}
Mat operator ^(Mat a,LL k)
{
Mat c;
int i,j;
for (i=1;i<=MAXN;i++)
for (j=1;j<=MAXN;j++)
c.mat[i][j]=(i==j);
for (;k;k>>=1)
{
if (k&1) c=c*a;
a=a*a;
}
return c;
}
void Init()
{
p.mat[1][1]=4;p.mat[1][2]=17;p.mat[1][3]=(-12);
p.mat[2][1]=1;p.mat[2][2]=0;p.mat[2][3]=0;
p.mat[3][1]=0;p.mat[3][2]=1;p.mat[3][3]=0;
q.mat[1][1]=1255;q.mat[2][1]=197;q.mat[3][1]=31;
}
int main()
{
int T;
LL n;
//freopen("in.txt","r",stdin);
Init();
scanf("%d",&T);
while (T--)
{
scanf("%lld",&n);
if (n==2) printf("31\n");
else if (n==3) printf("197\n");
else
{
x=(p^(n-4))*q;
printf("%lld\n",x.mat[1][1]);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息