首页 > 其他 > 详细

So Easy! HDU - 4565

时间:2021-06-09 12:55:59      阅读:33      评论:0      收藏:0      [点我收藏+]

原题链接
考察:矩阵快速幂
思路:
??将有理项和无理项分开,可以发现两者的系数都有规律.参考了这位大佬的博客,这波我没想出来().对\(\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;
}

So Easy! HDU - 4565

原文:https://www.cnblogs.com/newblg/p/14865573.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!