So Easy!

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

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
  这道题还是有点门路的,需要一些巧法。
  由于(a-1)2< b < a2,可以得出a-1<sqrt(b)<a,0<a-sqrt(b)<1。
  这时构建E(n)=(a+sqrt(b))^n+(a-sqrt(b))^n,发现E(n)是整数,而且(a-sqrt(b))^n小于1,那么(a+sqrt(b))^n向上取整就是E(n)。
  通过推导可以得出E(0)=2,E(1)=2*a,E(n)=2*a*E(n-1)-(a*a-b)*E(n-2),用矩阵乘法快速求出即可。
 #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
using namespace std;
const int maxn=;
int a,b,n,m;
struct Array{
int a[maxn],L;
void Init(int x){L=x;memset(a,,sizeof(a));}
int *operator[](int x){return &a[(x-)*L];}
};
struct Matrix{
int R,C;
Array mat;
void Init(int r,int c){mat.Init(c);R=r;C=c;}
int *operator[](int x){return mat[x];}
friend Matrix operator*(Matrix a,Matrix b){
Matrix c;c.Init(a.R,b.C);
for(int i=;i<=a.R;i++)
for(int j=;j<=b.C;j++)
for(int k=;k<=a.C;k++)
(c[i][j]+=1ll*a[i][k]*b[k][j]%m)%=m;
return c;
}
friend Matrix operator^(Matrix a,int k){
Matrix c;c.Init(a.R,a.C);
for(int i=;i<=a.R;i++)c[i][i]=;
while(k){if(k&)c=c*a;k>>=;a=a*a;}
return c;
}
}A,B; int main(){
while(scanf("%d%d%d%d",&a,&b,&n,&m)!=EOF){
A.Init(,);
A[][]=*a%m;A[][]=(b-1ll*a*a%m+m)%m;
A[][]=;A[][]=; B.Init(,);
B[][]=*a%m;
B[][]=;
A=A^(n-);B=A*B;
printf("%d\n",B[][]);
}
return ;
}

数学(矩阵乘法):HDU 4565 So Easy!的更多相关文章

  1. HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简

    http://acm.hdu.edu.cn/showproblem.php?pid=4565 首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数). 因为 ...

  2. HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...

  3. 【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)

    [解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下来就是矩阵的快速幂了. 神图: 给个大神的链接:构造类斐波那契数列的矩阵快速幂 ...

  4. HDU 4565 So Easy(矩阵解公式)

    So Easy [题目链接]So Easy [题目类型]矩阵解公式 &题解: 感觉这种类型的题都是一个套路,这题和hdu 2256就几乎是一样的. 所以最后2Xn就是答案 [时间复杂度]\(O ...

  5. hdu 4565 So Easy! (共轭构造+矩阵快速幂)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...

  6. HDU 4565 So Easy!(矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题意: 题意: #include <iostream>#include <cs ...

  7. HDU 4565 So Easy!(公式化简+矩阵)

    转载:http://www.klogk.com/posts/hdu4565/ 这里写的非常好,看看就知道了啊. 题意很easy.a,b,n都是正整数.求 Sn=⌈(a+b√)n⌉%m,(a−1)2&l ...

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

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

  9. hdu 4565 So Easy!(矩阵+快速幂)

    题目大意:就是给出a,b,n,m:让你求s(n); 解题思路:因为n很可能很大,所以一步一步的乘肯定会超时,我建议看代码之前,先看一下快速幂和矩阵快速幂,这样看起来就比较容易,这里我直接贴别人的推导, ...

随机推荐

  1. web前端开发随手笔记 - 持续更新

    本文仅为个人常用代码整理,供自己日常查阅 html 浏览器内核 <!--[if IE]><![endif]--> <!--[if IE 6]><![endif ...

  2. C#日期转换类

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te ...

  3. Java中JIN机制及System.loadLibrary() 的执行过程

    Android平台Native开发与JNI机制详解 http://mysuperbaby.iteye.com/blog/915425 个人认为下面这篇转载的文章写的很清晰很不错. 注意Android平 ...

  4. 内网映射到公网工具 --- ngrok

    ngrok可以将内网映射到公网上,这样就可以在公网上访问你的网络服务. 该工具通常在进行app开发和微信开发时比较有用,这样就可避免在公网服务器上单独部署项目,通过映射,直接连接本地服务即可进行开发. ...

  5. css media

    /* media */ /* 横屏 */ @media screen and (orientation:landscape){ } /* 竖屏 */ @media screen and (orient ...

  6. java把InputStram 转换为String

    public static String readStream(InputStream in) throws Exception{ //定义一个内存输出流 ByteArrayOutputStream ...

  7. SGU 162.Pyramids

    时间限制:0.25s 空间限制:6M; 题意: 按照AB, AC, AD, BC, BD, CD.给出一个空间四面体的6条边长.求出它的体积. Solution: 欧拉四面体公式:           ...

  8. print 函数的进一步理解

    没有括号的时候,pritn是列表操作符,会把其后列表里所有东西都数出来. 但是假如print后面紧跟着左括号,它就是一个函数调用,只会将括号内的东西输出来. “假如它看起来像函数调用,它就是一个函数调 ...

  9. ubuntu 解压,压缩

    .rar解压:rar x FileName.rar压缩:rar a FileName.rar DirName

  10. Eclipse启动Tomcat报错,系统缺少本地apr库

    Eclipse启动Tomcat报错,系统缺少本地apr库. Tomcat中service.xml中的设置情况. 默认情况是HTTP协议的值:protocol="HTTP/1.1" ...