So Easy! HDU - 4565

原题链接
考察:矩阵快速幂
思路:
  将有理项和无理项分开,可以发现两者的系数都有规律.参考了这位大佬的博客,这波我没想出来().对\(\sqrt b\)的系数取模是不合理的,所以只能另寻他路.

Code

#include <iostream> 
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int N = 2;
int x,y,n,m;
void mul(int f[],int a[][N])
{
	int res[N] = {0};
	for(int i=0;i<N;i++)
	  for(int j=0;j<N;j++)
	   res[i] = (res[i]+(LL)f[j]*a[j][i])%m;
	memcpy(f,res,sizeof res);
}
void mul(int a[][N])
{
	int res[N][N] = {0};
	for(int i=0;i<N;i++)
	  for(int j=0;j<N;j++)
	    for(int k=0;k<N;k++)
	     res[i][j] = (res[i][j]+(LL)a[i][k]*a[k][j])%m;
	memcpy(a,res,sizeof res);
}
int main()
{
//	freopen("in.txt","r",stdin);
	while(scanf("%d%d%d%d",&x,&y,&n,&m)!=EOF)
	{
		int f[N] = {1,0};
		int a[N][N] = {
		   {x,1},
		   {y,x} 
		};
		while(n)
		{
			if(n&1) mul(f,a);
			mul(a);
			n>>=1;
		}
//		LL ans = ceil(f[0]+f[1]*sqrt(y));
		printf("%lld\n",f[0]*2ll%m);
	}
	return 0;
}

posted @ 2021-06-09 09:34  acmloser  阅读(27)  评论(0编辑  收藏  举报