LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)

题目链接:LightOJ 1070 Algebraic Problem

题意:已知a+b和ab的值求a^n+b^n。结果模2^64。

思路:

1.找递推式

得到递推式之后就是矩阵快速幂了

注意:模2^64,定义成unsigned long long 类型,因为无符号类型超过最大范围的数与该数%最大范围 的效果是一样的。

AC代码:

#include<stdio.h>
#include<string.h>
#define LL unsigned long long

struct Matrix {
	LL m[10][10];
};
LL n;
Matrix geti(LL n) {
	LL i;
	Matrix b;
	memset(b.m,0,sizeof b.m);
	for(i=0;i<2;i++)
		b.m[i][i]=1;
	return b;
}

Matrix matrixmul(Matrix a,Matrix b) {
	LL i,j,k;
	Matrix c;
	for(i=0;i<2;i++) {
		for(j=0;j<2;j++) {
			c.m[i][j]=0.0;
			for(k=0;k<2;k++) {
				c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
			}
		}
	}
	return c;
}

Matrix quickpow(Matrix a,LL p) {
	Matrix m=a;
	Matrix b=geti(n);
	while(p) {
		if(p%2)
			b=matrixmul(b,m);
		p/=2;
		m=matrixmul(m,m);
	}
	return b;
}

int main() {
	LL t;
	LL p,q;
	int cas=1;
	Matrix pp,init,ans;
	//printf("%llu\n",kmod);
	scanf("%llu",&t);
	while(t--) {
		scanf("%llu %llu %llu",&p,&q,&n);
		memset(pp.m,0,sizeof pp);
		pp.m[0][0]=p;
		pp.m[0][1]=1;
		pp.m[1][0]=-q;
		pp.m[1][1]=0;

		init.m[0][0]=p;
		init.m[0][1]=2;
		init.m[1][0]=0;
		init.m[1][1]=0;

		printf("Case %d: ",cas++);
		if(n==0){
			printf("%llu\n",init.m[0][1]);
		}
		else if(n==1){
			printf("%llu\n",init.m[0][0]);
		}
		else {
			ans=quickpow(pp,n-1);
			ans=matrixmul(init,ans);
			printf("%llu\n",ans.m[0][0]);
		}
	}
	return 0;
}
/*

 */

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 05:33:25

LightOJ 1070 Algebraic Problem (推导+矩阵快速幂)的相关文章

LightOJ 1070 - Algebraic Problem 推导+矩阵快速幂

http://www.lightoj.com/volume_showproblem.php?problem=1070 思路:\({(a+b)}^n =(a+b){(a+b)}^{n-1} \) \((ab)C_{n}^{r}a^{n-r}b{r} = C_{n+2}^{r}a^{n-r+2}b{r} - a^{n+2} - b^{n+2} \) 综上\( f(n) = (a+b)f(n-1)-(ab)f(n-2) \) /** @Date : 2016-12-19-19.53 * @Author

LOJ 1070 - Algebraic Problem(矩阵快速幂啊)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1070 Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessarily have to be real numbers. Input Input starts with an integer T (≤ 10000), denoting the number o

LightOJ 1070 Algebraic Problem (推导+矩阵高速幂)

题目链接:problem=1070">LightOJ 1070 Algebraic Problem 题意:已知a+b和ab的值求a^n+b^n.结果模2^64. 思路: 1.找递推式 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" > 得到递推式之后就是矩阵高速幂了 注意:模2^64,定

LightOJ 1070 - Algebraic Problem 矩阵快速幂

题链:http://lightoj.com/volume_showproblem.php?problem=1070 1070 - Algebraic Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Given the value of a+b and ab you will have to find the value of an+bn. a and b not necessar

dutacm.club Water Problem(矩阵快速幂)

Water Problem Time Limit:3000/1000 MS (Java/Others)   Memory Limit:163840/131072 KB (Java/Others)Total Submissions:1228   Accepted:121 [Submit][Status][Discuss] Description 函数 f:Z+→Z .已知 f(1),f(2) 的值,且对于任意 x>1,有 f(x+1)=f(x)+f(x?1)+sin(πx2) . 求 f(n) 的

A Simple Math Problem(矩阵快速幂)(寒假闭关第一题,有点曲折啊)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 155 Accepted Submission(s): 110   Problem Description Lele now is thinking about a simple function f(x). If x < 10 f(x) = x.If

[ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了一发没过 上网看了一下才知道是快速幂 而且特征方程的推导简直精妙 尤其是共轭相抵消的构造 真的是太看能力了 (下图转自某大神博客) 特征方程是C^2=-2*a*C+(a*a-b) 然后用快速幂求解 临时学了下矩阵快速幂 从这道题能看出来 弄ACM真的要数学好 这不是学校认知的高数 线代 概率分数 而

HDU——4291A Short problem(矩阵快速幂+循环节)

A Short problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2461    Accepted Submission(s): 864 Problem Description According to a research, VIM users tend to have shorter fingers, compared

HDU 1757 A Simple Math Problem (矩阵快速幂)

[题目链接]:click here~~ [题目大意]: If x < 10 f(x) = x. If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + -- + a9 * f(x-10); 问f(k)%m的值. [思路]:矩阵快速幂,具体思路看代码吧,注意一些细节. 代码: #include<bits/stdc++.h> using namespace std; typedef long long LL; const