So Easy!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 813    Accepted Submission(s): 226

Problem Description
A sequence S
n is defined as:

Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate S
n.

You, a top coder, say: So easy! 

 
Input
There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 2
15, (a-1)
2< b < a
2, 0 < b, n < 2
31.The input will finish with the end of file.
 
Output
For each the case, output an integer S
n.
 
Sample Input
2 3 1 2013
2 3 2 2013
2 2 1 2013
 
Sample Output
4
14
4

详解在这: http://blog.csdn.net/ljd4305/article/details/8987823

另外Cn=(a+sqrt(b))^n+(a-sqrt(b))^n =>Cn*(a+sqrt(b) + a-sqrt(b))=(a+sqrt(b))^(n+1)+(a-sqrt(b))^(n+1)+(a*a-b)*(a+sqrt(n))^(n-1)+(a*a-b)*(a-sqrt(b))^(n-1)=C(n+1) + (a*a-b)C(n-1) =>C(n+1)=2a*Cn+(b-a^2)*C(n-1)

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std; const int MAX=10;
__int64 array[2][2],sum[2][2]; void MatrixMult(__int64 a[2][2],__int64 b[2][2],__int64 mod){
__int64 c[2][2]={0};
for(int i=0;i<2;++i){
for(int j=0;j<2;++j){
for(int k=0;k<2;++k){
c[i][j]+=a[i][k]*b[k][j];
}
}
}
for(int i=0;i<2;++i){
for(int j=0;j<2;++j)a[i][j]=c[i][j]%mod;
}
} __int64 Matrix(__int64 a,__int64 b,__int64 k,__int64 mod){
array[0][0]=a%mod,array[0][1]=(b%mod+mod)%mod;
array[1][0]=1,array[1][1]=0;
sum[0][0]=sum[1][1]=1;
sum[0][1]=sum[1][0]=0;
while(k){
if(k&1)MatrixMult(sum,array,mod);
MatrixMult(array,array,mod);
k>>=1;
}
return (a*sum[0][0]+2*sum[0][1])%mod;
} int main(){
__int64 a,b,n,m;
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&m)!=EOF){
if(n == 0)printf("I64d\n",1%m);
else if(n == 1)printf("%I64d\n",2*a%m);
else printf("%I64d\n",Matrix(2*a,b-a*a,n-1,m));
}
return 0;
}

hdu4565之矩阵快速幂的更多相关文章

  1. HDU4565(SummerTrainingDay05-C 矩阵快速幂)

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. HDU2256&&HDU4565:给一个式子的求第n项的矩阵快速幂

    HDU2256 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256 题意:求(sqrt(2)+sqrt(3))^2n%1024是多少. 这个题算是h ...

  3. hdu4565 So Easy! 矩阵快速幂

    A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example ...

  4. hdu4565 So Easy!(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题解:(a+√b)^n=xn+yn*√b,(a-√b)^n=xn-yn*√b, (a+√b)^n ...

  5. 矩阵快速幂 HDU 4565 So Easy!(简单?才怪!)

    题目链接 题意: 思路: 直接拿别人的图,自己写太麻烦了~ 然后就可以用矩阵快速幂套模板求递推式啦~ 另外: 这题想不到或者不会矩阵快速幂,根本没法做,还是2013年长沙邀请赛水题,也是2008年Go ...

  6. 51nod 算法马拉松18 B 非010串 矩阵快速幂

    非010串 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 如果一个01字符串满足不存在010这样的子串,那么称它为非010串. 求长度为n的非010串的个数.(对1e9+7取模) ...

  7. 51nod 1113 矩阵快速幂

    题目链接:51nod 1113 矩阵快速幂 模板题,学习下. #include<cstdio> #include<cmath> #include<cstring> ...

  8. 【66测试20161115】【树】【DP_LIS】【SPFA】【同余最短路】【递推】【矩阵快速幂】

    还有3天,今天考试又崩了.状态还没有调整过来... 第一题:小L的二叉树 勤奋又善于思考的小L接触了信息学竞赛,开始的学习十分顺利.但是,小L对数据结构的掌握实在十分渣渣.所以,小L当时卡在了二叉树. ...

  9. HDU5950(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 题意:f(n) = f(n-1) + 2*f(n-2) + n^4,f(1) = a , f(2 ...

随机推荐

  1. 转:java 帐号激活与忘记密码 实例

    原文链接:http://endual.iteye.com/blog/1613679 一.帐户激活   在 很多时候,在某些网站注册一个用户之后,网站会给这个用户注册时填写的email地址发送一封帐户激 ...

  2. classList详解,让你的js方便地操作DOM类

    在此之前,jQuery的hasClass.addClass.removeClass我们已经再熟悉不过了,然而我们并不会在每一个项目中都会去使用 jQuery或者Zepto,譬如在移动端的网页中,考虑到 ...

  3. c#之字符串函数

    1.常用的字符串函数 Compare 比较字符串的内容,考虑文化背景(场所),确定某些字符是否相等 int Compare(string str1,string str2) int Compare(s ...

  4. linux基础(2)

    Linux基础题 作业一:1) 新建用户natasha,uid为1000,gid为555,备注信息为“master”useradd natashagroupmod -g 555 natashauser ...

  5. 卡片选项页面 JTabbedPane 的使用

    package first; import javax.swing.*; import java.awt.*; import java.awt.event.*; class TtpDemo exten ...

  6. Django-models class Meta:元类

    Django模型之Meta选项详解 Model 元数据就是 "不是一个字段的任何数据" -- 比如排序选项, admin 选项等等.   Django模型类的Meta是一个内部类, ...

  7. android ARM 汇编学习 —— hello world

    android ARM 汇编学习—— 在 android 设备上编译c/cpp代码并用objdump/readelf等工具分析 adb putty 连上手机,用busybox vi 写一个 hello ...

  8. arm处理器中a5 a8 a9,v6 v7,arm7 arm9 arm11都是依据什么来分类的【转】

    转自:http://blog.csdn.net/maochengtao/article/details/9951131ARM处理器发展这么多年,有很多架构,很多不同的内核 架构有armv1 v2 v3 ...

  9. 【 Ngnix 】配置路径转发至后端Apache多台虚拟主机

    一.安装apache并开启端口 [root@server ~]# netstat -ntplu | grep httpd tcp /httpd tcp /httpd 二.nginx配置 locatio ...

  10. ORM-班级信息系统

    ORM版学员管理系统 班级表 表结构 class Class(models.Model): id = models.AutoField(primary_key=True) # 主键 cname = m ...