矩阵快速幂---BestCoder Round#8 1002
当要求递推数列的第n项且n很大时,怎么快速求得第n项呢?
可以用矩阵快速幂来加速计算。
我们可以用矩阵来表示数列递推公式
比如fibonacci数列 可以表示为 [f(n) f(n-1)] = [f(n-1) f(n-2)] [ 1 1 ]
[ 1 0 ]
设A = [ 1 1 ]
[ 1 0 ]
[f(n) f(n-1)] = [f(n-2) f(n-3)]*A*A
[f(n) f(n-1)] = [f(2) f(1)]*A^(n-2)
矩阵满足结合律,所以先计算A^(n-2),这个可以用一般快速二分幂的思想来计算。
BestCoder Round#8 1002
当n为奇数时,f(n) = 2 * f(n-1) + 1
当n为偶数时,f(n) = 2 * f(n-1)
将偶数项独立出来形成单独的一个数列 b(2*n) = 2 * b(2*n-1) + 1 = 4 * (2*n-2) + 2
即b(n) = 4 * b(n-1) + 2
当n为偶数时,计算b(n/2)即可
当n为奇数时,计算b(n/2) * 2 + 1即可
因为n很大,可以用矩阵快速幂来加速
递推矩阵为 [b(n) 2] = [b(n-1] 2] * [ 4 0 ]
[ 1 1 ]
#include <stdio.h>
#include <string.h>
typedef long long LL;
struct Matrix
{
LL matrix[][];
};
int n,m;
Matrix operator *(const Matrix &lhs, const Matrix &rhs)
{
Matrix res;
memset(res.matrix, ,sizeof(res.matrix));
int i,j,k;
for(k=; k<; ++k)
for(i=; i<; ++i)
{
if(lhs.matrix[i][k] == ) continue;
for(j=; j<; ++j)
{
if(rhs.matrix[k][j] == ) continue;
res.matrix[i][j] = (res.matrix[i][j] + lhs.matrix[i][k] * rhs.matrix[k][j]) % m;
}
}
return res;
}
Matrix operator ^(Matrix a, int k)
{
Matrix res;
int i,j;
for(i=; i<; ++i)
for(j=; j<; ++j)
res.matrix[i][j] = (i == j);
while(k)
{
if(k & )
res = res * a;
a = a * a;
k>>=;
}
return res;
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
Matrix a;
a.matrix[][] = ;
a.matrix[][] = ;
a.matrix[][] = a.matrix[][] = ;
int k = n / ;
a = a ^ k;
LL ans =( * a.matrix[][]) % m;
if(n & == )
ans = (ans * + ) % m;
printf("%lld\n",ans); }
return ;
}
矩阵快速幂---BestCoder Round#8 1002的更多相关文章
- 线段树+矩阵快速幂 Codeforces Round #373 (Div. 2) E
http://codeforces.com/contest/719/problem/E 题目大意:给你一串数组a,a[i]表示第i个斐波那契数列,有如下操作 ①对[l,r]区间+一个val ②求出[l ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- hdu 5607 BestCoder Round #68 (矩阵快速幂)
graph Accepts: 9 Submissions: 61 Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 ...
- BestCoder Round #29——A--GTY's math problem(快速幂(对数法))、B--GTY's birthday gift(矩阵快速幂)
GTY's math problem Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- hdoj5667 BestCoder Round #80 【费马小定理(膜拜)+矩阵快速幂+快速幂】
#include<cstdio> #include<string> #include<iostream> #include<vector> #inclu ...
- Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems(动态规划+矩阵快速幂)
Problem Educational Codeforces Round 60 (Rated for Div. 2) - D. Magic Gems Time Limit: 3000 mSec P ...
- Educational Codeforces Round 13 D. Iterated Linear Function (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/678/D 简单的矩阵快速幂模版题 矩阵是这样的: #include <bits/stdc++.h&g ...
- Codeforces Round #257 (Div. 2) B. Jzzhu and Sequences (矩阵快速幂)
题目链接:http://codeforces.com/problemset/problem/450/B 题意很好懂,矩阵快速幂模版题. /* | 1, -1 | | fn | | 1, 0 | | f ...
- Codeforces Round #536 (Div. 2) F 矩阵快速幂 + bsgs(新坑) + exgcd(新坑) + 欧拉降幂
https://codeforces.com/contest/1106/problem/F 题意 数列公式为\(f_i=(f^{b_1}_{i-1}*f^{b_2}_{i-2}*...*f^{b_k} ...
随机推荐
- Swift - 协议(protocol)
1,Swift中协议类似于别的语言里的接口,协议里只做方法的声明,包括方法名.返回值.参数等信息,而没有具体的方法实现. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- setStyleSheet来设定窗口部件的样式
使用setStyleSheet来设置图形界面的外观:QT Style Sheets是一个很有利的工具,允许定制窗口的外观,此外还可以用子类QStyle来完成,他的语法很大比重来源于html的CSS,但 ...
- 八皇后问题详细分析与解答(递归法解答,c#语言描述)
八皇后问题,是一个古老而著名的问题,是回溯算法的典型例题.该问题是十九世纪著名的数学家高斯1850年提出:在8X8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或 ...
- urllib2的异常处理
异常处理 作为爬虫的抓取过程基本就那么多内容了,后面再将一些正则表达式的东西简单介绍一下基本就完事了,下面先说说异常处理的方法.先介绍一下抓取过程中的主要异常,如URLError和HTTPError. ...
- IBinder在进程之间传递一个对象的形式(一)
主张 什么时候service通常被称为远程时的,用到aidl来定一个接口供service和client来使用,这个事实上就是使用Binder机制的IPC通信.当client bind service成 ...
- hdu2112 HDU Today
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112 题目分类:SPFA算法+map容器 错误点:红色标记部分 代码: #include<bit ...
- 深刻:截获windows的消息并分析实例(DefWindowProc),以WM_NCHITTEST举例(Windows下每一个鼠标消息都是由 WM_NCHITTEST 消息产生的,这个消息的参数包含了鼠标位置的信息)
1,回调函数工作机制 回调函数由操作系统自动调用,回调函数的返回值当然也是返回给操作系统了. 2,截获操作系统发出的消息,截获到后,将另外一个消息返回给操作系统,已达到欺骗操作系统的目的. 下面还是以 ...
- projecteuler---->problem=9----Special Pythagorean triplet
title: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2 For e ...
- gradle多模块开发(转)
参考文档:gradle的官方userguide.pdf文档的chapter 55和chapter 56.gradle的多模块或项目开发一定不会比maven差,在我看来!大的项目分成多个模块来开发是常事 ...
- C++操作符operator的另一种用法
http://blog.csdn.net/memewry/article/details/7833314 参考地址 今天在程序员面试宝典上看到这样一道题目: A C++ developer want ...