注意题目的一个关键条件(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. 2013夏,iDempiere来了 - v1.0c Installers (Devina LTS Release) 2013-06-27

    怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ iDempiere来了 - v1.0c Installers (Devina LTS R ...

  2. android:imeOptions属性

    imeOptions软键盘setOnEditorActionListener  默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软键盘 设置 android:imeOptions= ...

  3. OpenSSL生成证书详解 如何使用OpenSSL生成自签证书 转载

    原文:http://my.oschina.net/fajar/blog/425478 使用OpenSSL生成自签证书(亲测) 一,前言 读过我博客的小伙伴儿都知道,我一般在前言里面会提到为什么写这篇博 ...

  4. php漏洞修复 禁用函数

    别人利用此PHP函数可以对系统进行相关操作 1.打开php.ini找到 ; http://php.net/disable-functions 2.修改添加内容如下 disable_functions ...

  5. 任务调度Cron表达式及Quartz代码详解

    在线Cron表达式生成器 http://cron.qqe2.com/ cron表达式详解 http://www.cnblogs.com/linjiqin/archive/2013/07/08/3178 ...

  6. 735. Replace With Greatest From Right【medium】

    Given an array of integers, replace every element with the next greatest element (greatest element o ...

  7. struts-config message-resources配置问题总结

    问题:我的app无法读取配置好的ApplicationResources.properties中的内容 解答:文件目录为 /webapp /WEB-INF /classes ApplicationRe ...

  8. yii save model return id null

    /** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' ...

  9. C++Primer学习笔记《2》

    数组是一种复合类型,由类型名+数组名+维度组成. 数组定义中的类型能够是C++基本内置类型.也能够是类类型的.数组元素的类型能够是除了引用类型以外的其它不论什么类型.没有全部的元素都是引用的数组. 数 ...

  10. JQ 时间插件

    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/ueditor/u ...