东大OJ-1040-Count-快速幂方法求解斐波那契-
来源:互联网 发布:购买域名送空间 编辑:程序博客网 时间:2024/05/16 05:22
Many ACM team name may be very funny,such as "Complier_Error","VVVVV".Oh,wait for a minute here.
Is it "W W"+"V",or "W"+"V V V",or something others we can treat as?There are several ways we can treat this name "VVVVV" (5 'V's),as V V can be treat as a W.
For 5 'V's,our have 8 ways.They are:
V V V V V
V W W
W W V
V W V V
W V W
W V V V
V V W V
V V V W
The problem here is that for n 'V's,how many ways do we have to treat it?Because the answer may be too large, you should output the answer module by p.(If n is 0,then we have just one way.)
输入
There are multiple test cases. The first line of the input contains an integerM, meaning the number of the test cases.
For each test cases, there aretwo integers nand pin a single line.
You can assume that0<=n<=2100000000,0<p<=2009.
输出
For each test case, output the answer with case number in a single line.
样例输入
25 54 7
样例输出
3
#include<iostream>using namespace std;struct m{int a[2][2]; };m mul(m a, m b,int p){m c;int i, j,k;for (i = 0; i < 2;i++)for (j = 0; j < 2; j++){c.a[i][j] = 0;for (k = 0; k < 2; k++)c.a[i][j] +=( (a.a[i][k] %p)* (b.a[j][k]%p))%p;c.a[i][j] %= p;}return c;}m go(m a, int n,int p){if (n == 1)return a;m b = go(a, n / 2, p);m c = mul(b, b, p);if (n % 2 == 1)c = mul(c, a, p);return c;}int main(){//freopen("in.txt", "r", stdin);int t;cin >> t;m a = { 0, 1, 1, 1 };while (t-- > 0){int n, p;cin >> n >> p;if (n == 0){ cout << 1 << endl; continue; }cout << go(a, n, p).a[1][1]<<endl;}return 0;}
- 东大OJ-1040-Count-快速幂方法求解斐波那契-
- 【矩阵快速幂】 斐波那契数列求解。
- 从sicily Fibonacci 问题出发解决矩阵快速幂求解斐波那契问题
- 三种方法求解Fibonacci(斐波那契)数列
- 3种方法求解斐波那契数列
- 3种方法求解斐波那契数列
- 三种方法求解Fibonacci(斐波那契)数列
- 3种方法求解斐波那契数列
- 求解斐波那契数列的几种方法
- 斐波那契数列的几种求解方法
- 求解斐波那契数列的动态规划方法
- 动态规划的方法求解斐波那契数列
- 3种方法求解斐波那契数列
- 3种方法求解斐波那契数列
- 菜鸟上路 杭电OJ 1005 从 斐波那契到 快速幂
- NEUOJ 1040 Count (快速幂求第 n 个斐波那契数)
- 矩阵快速幂求解任意初始值f1,f2及a,b的斐波那契数列
- 矩阵快速幂算法的原理与实践——“使用MATLAB求解大型斐波那契数"
- awk数组
- hdu2063过山车(最大匹配)
- 找工作的几点指导思想
- BDB 封装
- vc++telnet远程登录
- 东大OJ-1040-Count-快速幂方法求解斐波那契-
- 我的逻辑回归认知
- UVA - 10071 Back to High School Physics
- TCP/IP协议浅析
- const类型的成员函数内部如何改变成员变量
- UVa1210 Sum of Consecutive Prime Numbers
- HDU 1180 诡异的楼梯
- vc++实现http下载的通用类
- 各大网站的页面初始化css代码