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. 【bzoj1911-[Apio2010]特别行动队】斜率优化

    [题目描述] 有n个数,分成连续的若干段,每段的分数为a*x^2+b*x+c(a,b,c是给出的常数),其中x为该段的各个数的和.求如何分才能使得各个段的分数的总和最大. [输入格式]  第1行:1个 ...

  2. 滑杆(JSlider)和进度指示条(JProgressBar) 的使用

    package first; import javax.swing.*; import javax.swing.border.TitledBorder; import java.awt.*; impo ...

  3. bzoj 2659 几何

    首先考虑(0, 0)到(p, q)这条直线. y = q / p * x. sum{k = 0 to (p - 1) / 2} [q / p * k] 就是直线下方的点数.sum{k = 0 to ( ...

  4. C#中文乱码转换

    string text="中文";string keyword;byte[] buffer= Encoding.UTF8.GetBytes(text);keyword=Encodi ...

  5. JavaScript阻止修改对象的三种方式

    JavaScript中,我们希望别人无法修改我们创建的对象.比如,代码库的作者很可能想锁定核心库的某些部分来保证它们不被意外地修改.ES5中引入了三种锁定修改的级别:防止扩展preventExtens ...

  6. Java 原子性引用 AtomicReference

    http://www.jianshu.com/p/882d0e2c3ea6 实现 原子操作 使用场景: 一个线程使用student对象,另一个线程负责定时读表,更新这个对象.那么就可以用AtomicR ...

  7. Liquibase 快速开始

    Step 1 :创建Changelog文件,所有的数据库变动都会保存在Changelog文件中 <?xml version="1.0" encoding="UTF- ...

  8. Eclipse,myeclipse安装 配置Maven

    本文转自http://www.cnblogs.com/timeng/archive/2013/05/07/maven_install.html myeclipse自带了maven插件,但是和原生插件还 ...

  9. docker的存储结构,和以前有了很大不同

    在网上学习这一块知识点时,有一个URL讲得很详细, docker 镜像与容器存储目录结构精讲 http://blog.csdn.net/wanglei_storage/article/details/ ...

  10. head first (一):策略模式

    head first 看了一遍,被面试时问我工厂模式,我发现我还是不记得,所以还是认认真真的看一遍吧,上一遍看的太快了,什么都没有记住.这一遍我会写下一些东西帮助自己记忆和理解,例子我也会用C#自己写 ...