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:

  1. V V V V V

  2. V W W

  3. W W V

  4. V W V V

  5. W V W

  6. W V V V

  7. V V W V

  8. 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 integer
M, meaning the number of the test cases.

For each test cases, there are
two integers n
and p
in a single line.

You can assume that
0<=n<=2100000000,
0<p<=2009.

输出

For each test case, output the answer with case number in a single line.

样例输入

2
5 5
4 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-快速幂方法求解斐波那契-的更多相关文章

  1. 使用并行的方法计算斐波那契数列 (Fibonacci)

    更新:我的同事Terry告诉我有一种矩阵运算的方式计算斐波那契数列,更适于并行.他还提供了利用TBB的parallel_reduce模板计算斐波那契数列的代码(在TBB示例代码的基础上修改得来,比原始 ...

  2. 斐波那契数列-java编程:三种方法实现斐波那契数列

    题目要求:编写程序在控制台输出斐波那契数列前20项,每输出5个数换行 斐波那契数列指的是这样一个数列:1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, … 这个数列 ...

  3. 两种JS方法实现斐波那契数列

    第一种方法:递归 function fibonacci(n){ if (n==0){ return 0; }else if (n==1){ return 1; } return fibonacci(n ...

  4. C# 4种方法计算斐波那契数列 Fibonacci

    F1: 迭代法 最慢,复杂度最高 F2: 直接法 F3: 矩阵法 参考<算法之道(The Way of Algorithm)>第38页-魔鬼序列:斐波那契序列 F4: 通项公式法 由于公式 ...

  5. HDU 2855 斐波那契+矩阵快速幂

    http://acm.hdu.edu.cn/showproblem.php?pid=2855 化简这个公式,多写出几组就会发现规律 d[n]=F[2*n] 后面的任务就是矩阵快速幂拍一个斐波那契模板出 ...

  6. HDU----(4549)M斐波那契数列(小费马引理+快速矩阵幂)

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  7. HDU 4549 M斐波那契数列(矩阵快速幂)

    题目链接:M斐波那契数列 题意:$F[0]=a,F[1]=b,F[n]=F[n-1]*F[n-2]$.给定$a,b,n$,求$F[n]$. 题解:暴力打表后发现$ F[n]=a^{fib(n-1)} ...

  8. HDU 4549 M斐波那契数列(矩阵快速幂+费马小定理)

    M斐波那契数列 Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submi ...

  9. CodeForces 227E Anniversary (斐波那契的高妙性质+矩阵快速幂)

    There are less than 60 years left till the 900-th birthday anniversary of a famous Italian mathemati ...

随机推荐

  1. Windows 安装ELK

    在Windows服务器上安装ELK logstash在windows平台下不能监控磁盘文件,用nxlog代替,监控文件并把内容发送到logstash 部署环境 Os :Windows 7 logsta ...

  2. android RelativeLayout 动态设置高度

    定义: private RelativeLayout mrlay; 调高度: mrlay = (RelativeLayout) findViewById(R.id.rlay_1); android.v ...

  3. Linux 解压缩命令

    gzip 文件 压缩文件 gzip -d 文件 解压文件 zcat  查看以结尾为zip 文件 bzip2 压缩文件 bzip2 -d 文件 解压文件 bzcat 查看以结尾为zip2 文件 zip ...

  4. 记一次惨痛的线上bug

    讲述背景,刚入职新公司2个月的时候,接手一个红包系统.资历尚浅,对业务也不是很熟悉.公司开发新的平台,需要使用红包功能来进行推广,按照产品的需求,进行开发...然而,问题就出在这里,红包接口比较陈旧, ...

  5. Object-C中self和super的区别

    self与super的区别 原文CSDN evilotus 有所整理 **** 在ObjC中的类实现中经常看到这两个关键字"self"和"super",以以前o ...

  6. 创建MyOffice项目

    创建查看评分窗体(FrmLOOK),添加定义成员数组,将员工数据绑定到FrmLOOK窗体的ListView控件上 public ListViewItem lv; private void Form1_ ...

  7. BZOJ1085: [SCOI2005]骑士精神 [迭代加深搜索 IDA*]

    1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1800  Solved: 984[Submit][Statu ...

  8. Jmeter :多个jmeter如何参数化获取不同的CSV文件

    如何为不同的jmeter engine建立不同的csv文件: 1.如果需要多个jmeter engine 去读取不同的csv文件,可以使用一个变量去区分 ${__P(InstanceID)},  in ...

  9. jmeter-HTTP COOKIE Manager

    http://wangsheng14591.blog.163.com/blog/static/327797102012829101351887/

  10. Python自动补全

    转自:http://blog.linuxeye.com/324.html Python自动补全有vim编辑下和python交互模式下,下面分别介绍如何在这2种情况下实现Tab键自动补全. 一.vim ...