注意题目的一个关键条件(a-1)2< b < a2 , 于是可以知道    0 < a-√b < 1 ,所以 (a-√b)^n < 1 . 然后 (a+ √b)^n+(a-√b)^n 的值为整数且正好是 (a+√b)^n的向上取整.

然后就可以得到递推式 f[n+1]=2*a*f[n]-(a*a-b)*f[n-1] .

然后构造矩阵。 幂乘即可.

                So Easy!

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

Problem Description
  A sequence Sn 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 Sn.
  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 < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.
 
Output
  For each the case, output an integer Sn.
 
Sample Input
2 3 1 2013
2 3 2 2013
2 2 1 2013
 
Sample Output
4
14
4
 
Source
 
Recommend
zhoujiaqi2010
 
 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff typedef __int64 LL; LL a,b,n,m;
LL g[][];
LL MOD; void cal(LL s[][],LL t[][])
{
LL tmp[][];
memset(tmp,,sizeof(tmp));
for(int k=;k<;k++)
for(int i=;i<;i++)
for(int j=;j<;j++)
{
tmp[i][j]=(tmp[i][j]+s[i][k]*t[k][j])%MOD;
}
for(int i=;i<;i++)
for(int j=;j<;j++)
s[i][j]=tmp[i][j];
} int main()
{
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","r",stdin);
//freopen("C:\\Users\\Administrator\\Desktop\\in.txt","w",stdout);
while(scanf("%d%d%d%d",&a,&b,&n,&m)!=EOF)
{
MOD=m;
g[][]=*a; g[][]=;
g[][]=b-a*a; g[][]=; n--;
LL sum[][];
for(int i=;i<;i++)
for(int j=;j<;j++)
if(i==j) sum[i][j]=;
else sum[i][j]=;
while(n)
{
if( (n&)!=)
cal(sum,g);
cal(g,g);
n>>=;
}
LL ans=((*a*sum[][]+*sum[][])%MOD+MOD)%MOD;
cout<<ans<<endl;
}
return ;
}

hdu4565(矩阵快速幂+经典的数学处理)的更多相关文章

  1. 51nod 1113 矩阵快速幂( 矩阵快速幂经典模板 )

    1113 矩阵快速幂 链接:传送门 思路:经典矩阵快速幂,模板题,经典矩阵快速幂模板. /******************************************************* ...

  2. hdu4565矩阵快速幂

    这题太坑了...刚开始以为可以用|a+sqrt(b)  1|水过...结果tle,还一直想明明我logn的做法怎么可能tle.. |    0           1| 实在无奈看的题解 (a+sqr ...

  3. [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

    从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...

  4. 【做题】SRM701 Div1 Hard - FibonacciStringSum——数学和式&矩阵快速幂

    原文链接 https://www.cnblogs.com/cly-none/p/SRM701Div1C.html 题意:定义"Fibonacci string"为没有连续1的01串 ...

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

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

  6. luogu3263/bzoj4002 有意义的字符串 (数学+矩阵快速幂)

    首先我们发现$\frac{b+\sqrt{d}}{2}$这个形式好像一元二次方程的求根公式啊(???反正我发现不了) 然后我们又想到虽然这个东西不好求但是$(\frac{b-\sqrt{d}}{2}) ...

  7. 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 ...

  8. BZOJ2326 HNOI2011数学作业(矩阵快速幂)

    考虑暴力,那么有f(n)=(f(n-1)*10digit+n)%m.注意到每次转移是类似的,考虑矩阵快速幂.首先对于位数不同的数字分开处理,显然这只有log种.然后就得到了f(n)=a·f(n-1)+ ...

  9. 2018.09.26 bzoj5221: [Lydsy2017省队十连测]偏题(数学推导+矩阵快速幂)

    传送门 由于没有考虑n<=1的情况T了很久啊. 这题很有意思啊. 考试的时候根本不会,骗了30分走人. 实际上变一个形就可以了. 推导过程有点繁杂. 直接粘题解上的请谅解. 不得不说这个推导很妙 ...

随机推荐

  1. Spark-Dependency

    1.Spark中採用依赖关系(Dependency)表示rdd之间的生成关系.Spark可利用Dependency计算出失效的RDD.在每一个RDD中都存在一个依赖关系的列表 private var ...

  2. oracle事务块示例

    begin Insert into T_SYS_PAGEOPER (FOPERID,FPAGEID) values (152,22); Insert into T_SYS_PAGEOPER (FOPE ...

  3. res与res-auto的区别

    Solution: Upgrade to latest SDK & ADT version (fixed was released since r17) and usehttp://schem ...

  4. 判断Server Manager里面的Role是否已经安排

    用InstallState来判断 function Check-DataDeduplication{ Import-Module "ServerManager" -ErrorAct ...

  5. C#使用技巧之调用JS脚本(转)

    .创建个Winform项目. .在From1上增加一个文本框一个按钮. .在解决方案中创建一个test.js文件. test.js代码如下: function sayHello(str) { retu ...

  6. wpf 帧动画

    帧动画实现很简单 <ImageBrush x:Key="speed_s" Stretch="Fill" ImageSource="/images ...

  7. AngularJS ——ngResource、RESTful APIs 使用

    这篇文章里,用以下两个情景用例来解释: 保存/持久化 新的数据对象 更新存在的数据对象 代码片段包含了AngularJs代码和Spring MVC代码,以能够让你简单快速的上手. 想要$resourc ...

  8. 003实现字符串反转reverse

    这个简单就直接代码了 //写代码翻转一个C风格的字符串. (C风格的意思是"abcd"须要用5个字符来表示,包括末尾的 结束字符) #include <stdio.h> ...

  9. 点滴积累【C#】---C#实现上传照片到物理路径,并且将地址保存到数据库,

    效果: 思路: 首先,获取图片物理地址,然后进行判断将图片保存到文件夹下,再将图片的信息保存到数据库. 数据库: create table image1 ( ID ,) primary key, Im ...

  10. PHPCMS V9数据库表结构分析

    PHPCMS V9可以轻松承载百万级的访问数据,最大的功臣就是PHPCMS良好的数据库结构,在数据库的设计方面,一定是下足了功夫.   一般网站的信息量离这个级别相差甚远,但是了解学习一下PHPCMS ...