hdu 3306 Another kind of Fibonacci(矩阵高速幂)
Another kind of Fibonacci
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1691 Accepted Submission(s): 660
Each test case will contain three integers , N, X , Y .
N : 2<= N <= 231 – 1
X : 2<= X <= 231– 1
Y : 2<= Y <= 231 – 1
2 1 1
3 2 3
6
196

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int mod=10007;
struct matrix
{
long long ma[5][5];
};
matrix multi(matrix x,matrix y)//矩阵相乘
{
matrix ans;
memset(ans.ma,0,sizeof(ans.ma));
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(x.ma[i][j])//稀疏矩阵优化
for(int k=1;k<=4;k++)
{
ans.ma[i][k]=(ans.ma[i][k]+(x.ma[i][j]*y.ma[j][k])%mod)%mod;
}
}
}
return ans;
}
matrix pow(matrix a,long long m)
{
matrix ans;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=4;j++)
{
if(i==j)
ans.ma[i][j]=1;
else
ans.ma[i][j]=0;
}
}
while(m)
{
if(m&1)
ans=multi(ans,a);
a=multi(a,a);
m=m>>1;
}
return ans;
}
int main()
{
long long x,y,n;
while(~scanf("%I64d%I64d%I64d",&n,&x,&y))
{
matrix a,b;
memset(a.ma,0,sizeof(a.ma));
memset(b.ma,0,sizeof(b.ma));
a.ma[1][1]=1;
a.ma[1][2]=1;
a.ma[2][2]=(x*x)%mod;
a.ma[2][3]=(y*y)%mod;
a.ma[2][4]=(2*x*y)%mod;
a.ma[3][2]=1;
a.ma[4][2]=x;
a.ma[4][4]=y;
b.ma[1][1]=1;
b.ma[2][1]=1;
b.ma[3][1]=1;
b.ma[4][1]=1;
a=pow(a,n);
a=multi(a,b);
printf("%I64d\n",a.ma[1][1]);
}
return 0;
}
hdu 3306 Another kind of Fibonacci(矩阵高速幂)的更多相关文章
- hdu 3306 Another kind of Fibonacci 矩阵快速幂
参考了某大佬的 我们可以根据(s[n-2], a[n-1]^2, a[n-1]*a[n-2], a[n-2]^2) * A = (s[n-1], a[n]^2, a[n]*a[n-1], a[n-1] ...
- HDU1588-Gauss Fibonacci(矩阵高速幂+等比数列二分求和)
题目链接 题意:g(x) = k * x + b.f(x) 为Fibonacci数列.求f(g(x)),从x = 1到n的数字之和sum.并对m取模. 思路: 设A = |(1, 1),(1, 0) ...
- HDU 4965 Fast Matrix Calculation(矩阵高速幂)
HDU 4965 Fast Matrix Calculation 题目链接 矩阵相乘为AxBxAxB...乘nn次.能够变成Ax(BxAxBxA...)xB,中间乘n n - 1次,这样中间的矩阵一个 ...
- HDU 5411 CRB and puzzle (Dp + 矩阵高速幂)
CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) T ...
- HDU 3306 Another kind of Fibonacci(矩阵+ll超时必须用int&输入必须取模&M必须是int类型)
Another kind of Fibonacci [题目链接]Another kind of Fibonacci [题目类型]矩阵+ll超时必须用int&输入必须取模&M必须是int ...
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
- HDU 2254 奥运(矩阵高速幂+二分等比序列求和)
HDU 2254 奥运(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 2254 奥运 题意: 中问题不解释. 分析: 依据floyd的算法,矩阵的k次方表示这个矩阵走了k步. 所以k ...
- hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
http://acm.hdu.edu.cn/showproblem.php?pid=3221 一晚上搞出来这么一道题..Mark. 给出这么一个程序.问funny函数调用了多少次. 我们定义数组为所求 ...
- HDU 1575 Tr A(矩阵高速幂)
题目地址:HDU 1575 矩阵高速幂裸题. 初学矩阵高速幂.曾经学过高速幂.今天一看矩阵高速幂,原来其原理是一样的,这就好办多了.都是利用二分的思想不断的乘.仅仅只是把数字变成了矩阵而已. 代码例如 ...
随机推荐
- VB 宏+mysql解决EXCEL表格实现自动化处理
1.表格模板自动建立源码 Sub opp()Dim myPath$, myFile$, AK As WorkbookApplication.ScreenUpdating = FalsemyPath = ...
- 【例题 8-2 UVA-1605】Building for UN
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 两层 然后n*n就够了 第一层类似 aaa.. bbb.. ccc.. ... 第二次则变成 abc.... abc.... abc ...
- [React & Testing] Simulate Event testing
Here we want to test a toggle button component, when the button was click, state should change, styl ...
- x264代码剖析(三):主函数main()、解析函数parse()与编码函数encode()
x264代码剖析(三):主函数main().解析函数parse()与编码函数encode() x264的入口函数为main().main()函数首先调用parse()解析输入的參数,然后调用encod ...
- linux 命令之 apt-get
apt-get 是一个下载安装软件包的简单命令行接口 使用方法: apt-get [OPTIONS] [COMMANDS] [PACKAGE_NAMES] OPTIONS: -h 帮助信息 -q 输出 ...
- PDF.Js的使用—javascript中前端显示pdf文件
PDF.Js的使用—javascript中前端显示pdf文件 写于2018/12/6 起因是一个图片展示页面需要展示pdf格式的文件,所以查了半天决定使用pdf.js,我也不求有多了解它,能实现我想要 ...
- WebService学习总结(2)——WebService是什么?
一.WebService是什么? 1. 基于Web的服务:服务器端整出一些资源让客户端应用访问(获取数据) 2. 一个跨语言.跨平台的规范(抽象) 3. 多个跨平台.跨语言的应用间通信整合的方案(实际 ...
- PatentTips - Method, apparatus and system for instructing a virtual device from a virtual machine
BACKGROUND OF THE INVENTION A virtual machine (VM) may be or include a framework or environment crea ...
- 洛谷 P2368 EXCEEDED WARNING B
P2368 EXCEEDED WARNING B 题目背景 SGU 107 题目描述 求有多少个平方后末尾为987654321的n位数 输入输出格式 输入格式: 整数n 输出格式: 答案,即[b]“平 ...
- Linux 从core信息中找到TLS信息
背景 我们在查core问题时,有时候须要查看某个TLS变量的值.可是GDB没有提供直接的命令,或者我不知道.这篇文字的目的.就是想办法从core文件里找出某个线程存放TLS变量的内容. 依据 Linu ...