POJ-3070 Fibonacci【矩阵二分幂】

news/2024/7/4 1:43:38 标签: matrix, class, c
cle class="tags" href="/tags/CLASS.html" title=class>class="baidu_pl">
cle_content" class="tags" href="/tags/CLASS.html" title=class>class="article_content clearfix">
content_views" class="tags" href="/tags/CLASS.html" title=class>class="htmledit_views">

题目链接:http://poj.org/problem?id=3070

解题思路:

矩阵二分幂࿰c;模板题~~~~~水过


代码如下:

<code class="tags" href="/tags/CLASS.html" title=class>class="language-cpp">#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<climits>
using namespace std;

#define CLR(arr, what) memset(arr, what, sizeof(arr))
typedef long long LL;
const LL Size = 2;
const LL MOD = 10000; //取余

class="tags" href="/tags/CLASS.html" title=class>class Matrix67
{
public:
	Matrix67(const LL ff1, const LL ff2, const LL aa, const LL bb, const LL cc, const LL nn); //构造函数
	void copymat(LL mata[Size][Size], LL matb[Size][Size]); //矩阵复制:后->前
	void binary(LL n); //矩阵二分幂:次数
	void multiply(LL mata[Size][Size], LL matb[Size][Size]); //矩阵连乘
	void put(LL mata[Size][Size]); //调试~
	LL result(); //打印结果

private:
	LL f1, f2, a, b, c, n;
	LL mat[Size][Size], temp[Size][Size], mid[Size][Size]; //二分幂、递推、单位矩阵
};

void Matrix67::put(LL mata[Size][Size])
{
	cout<<endl;
	for(int i = 0; i < Size; ++i)
	{
		for(int j = 0; j < Size; ++j)
			cout<<mata[i][j];
		cout<<endl;
	}
	cout<<endl;
}

Matrix67::Matrix67(const LL ff1, const LL ff2, const LL aa, const LL bb, const LL cc, const LL nn)
{
	CLR(mat, 0); CLR(temp, 0); CLR(mid, 0);
	f1 = ff1, f2 = ff2, a = aa, b = bb, c = cc, n = nn;
	mat[0][1] = 1, mat[1][0] = 1, mat[1][1] = 1; //初始化单位矩阵
	temp[0][0] = ff1, temp[0][1] = ff2; //初始化递推矩阵
	copymat(mid, mat);
}

void Matrix67::copymat(LL mata[Size][Size], LL matb[Size][Size])
{
	for(int i = 0; i < Size; ++i)
		for(int j = 0; j < Size; ++j)
			mata[i][j] = matb[i][j];
}

void Matrix67::binary(LL n)
{
	if(n == 1)
		return ;
	binary(n >> 1);
	multiply(mat, mat);
	if(n & 1) //奇次幂
		multiply(mat, mid);
}

void Matrix67::multiply(LL mata[Size][Size], LL matb[Size][Size])
{
	LL sum, tempmat[Size][Size];
	for(int i = 0; i < Size; ++i)
	{
		for(int j = 0; j < Size; ++j)
		{
			sum = 0;
			for(int k = 0; k < Size; ++k)
				sum = (sum + mata[i][k] * matb[k][j]) % MOD;
			tempmat[i][j] = (sum + MOD) % MOD;
		}
	}
	copymat(mata, tempmat);
}

LL Matrix67::result()
{
	if(n == 0)
		return temp[0][0];
	if(n > 1)
	{
		binary(n - 1);
		multiply(temp, mat);
	}
	return (temp[0][1] % MOD + MOD) % MOD;
}

int main()
{
	LL a, b, n;
	while(scanf("%lld", &n) && n != -1)
	{
		Matrix67 m(0, 1, 1, 1, 0, n);
		printf("%lld\n",m.result() % MOD);
	}
	return 0;
}code>


cle>

http://www.niftyadmin.cn/n/1516651.html

相关文章

java单引号_Java十四天零基础入门-Java字面量

不闲聊&#xff01;&#xff01;&#xff01;不扯淡&#xff01;&#xff01;&#xff01;小UP只分享Java相关的资源干货本章节目标&#xff1a;理解变量本质是什么&#xff0c;在开发中有什么用&#xff1f;变量三要素是什么&#xff1f;怎么声明变量&#xff1f;怎么给变量赋…

WINCE开机自动运行指定程序

WINCE开始默认是运行explorer.exe&#xff0c;是在shell.reg中设置的 [HKEY_LOCAL_MACHINE/init]"Launch50""explorer.exe""Depend50"hex:14,00, 1e,00 因此只要在platform.reg或者project.reg中做类似的更改就可以实现开机自动运行指定AP的功…

wince datagrid数据居中设置_年终总结PPT,一定少不了数据图表,教你四步做出高大上的图表...

平时做 PPT 时&#xff0c;经常会用到各种图表&#xff0c;来辅助表现内容的逻辑关系。听说很多人都是用形状一个个画出来的&#xff0c;我每次看到&#xff0c;都又羡慕又害怕。最常见的就是下边这样的逻辑图表&#xff1a;如果是你来做&#xff0c;中间这个逻辑图怎么在PPT里…

大数是否为素数【费马小定理+Carmichael数判断】

作为判断一个大数是否为素数的模板。 代码如下&#xff1a; #include<stdio.h> #include<algorithm> #include<iostream> using namespace std;long long multi(long long a,long long b,long long mod) {long long temp a,sum 0;while(b){if(b&1) sum…

NYOJ-479 Coprimes【欧拉函数】

题目链接&#xff1a;http://acm.nyist.net/JudgeOnline/problem.php?pid479 解题思路&#xff1a; 欧拉函数打表即可。 n的欧拉函数计算方法如下&#xff1a; 先将n素分解&#xff0c;之后取全部素因子。则n的欧拉函数就是n*(1-/1p1)*(1-1/p2)*..... 这样&#xff0c;当…

注解动态设置参数_举个栗子!Tableau 技巧(146):设置 动态参数 自动更新参数值列表...

我们知道&#xff0c;在 Tableau 2019.4 及以下版本中&#xff0c;参数的值在设置完成后&#xff0c;因为无法自动更新&#xff0c;所以需要不断地去手工维护包含参数的工作簿&#xff0c;直接影响了数据分析的效率。最近发布的 Tableau2020.1版本&#xff0c;新增的动态参数功…

NYOJ-333 mdd的烦恼【欧拉函数】

题目链接&#xff1a;http://acm.nyist.net/JudgeOnline/problem.php?pid333 解题思路&#xff1a; 欧拉函数应用&#xff0c;但是这个题和上个不一样&#xff0c;不能打表算&#xff0c;因为n的范围为整形&#xff0c;数组无法存下&#xff0c;所以只能用最原始的素分解来写…

NYOJ-468 Fibonacci数列(三)【大素数判断】

题目链接&#xff1a;http://acm.nyist.net/JudgeOnline/problem.php?pid468 解题思路&#xff1a; 这道题考察的是大素数的判断&#xff0c;但是有一个知识点。可以在黑书的221页找到。 就是斐波那契数列从第5项开始&#xff0c;如果它的项数为素数&#xff0c;那么它就是斐波…