转载:http://www.klogk.com/posts/hdu4565/

这里写的非常好,看看就知道了啊。

题意很easy。a,b,n都是正整数。求

Sn=⌈(a+b√)n⌉%m,(a−1)2<b<a2

这个题目也是2008年Google Codejam Round 1A的C题

做法事实上很easy。记(a+b√)n为An,配项

Cn=An+Bn=(a+b√)n+(a−b√)n

两项恰好共轭,所以Cn是整数。

又依据限制条件

(a−1)2<b<a2⇒0<a−b√<1⇒0<(a−b√)n<1⇒Bn<1

也就是说Cn=⌈An⌉

Sn=(Cn)%m

求Cn的方法是递推。
对Cn乘以(a+b√)+(a−b√)

Cn[(a+b√)+(a−b√)]=[(a+b√)n+(a−b√)n][(a+b√)+(a−b√)]=(a+b√)n+1+(a−b√)n+1+(a+b√)n(a−b√)+(a−b√)n(a+b√)=Cn+1+(a2−b)(a+b√)n−1+(a2−b)(a−b√)n−1=Cn+1+(a2−b)Cn−1

于是

Cn+1=2aCn−(a2−b)Cn−1

把这个递推式写成矩阵形式

[Cn+1Cn]=[2a1−(a2−b)0][CnCn−1]

于是就能够用矩阵高速幂来做了

[Cn+1Cn]=[2a1−(a2−b)0]n[C1C0]

So Easy!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 2286    Accepted Submission(s): 710

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
 

<span style="font-size:18px;">#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x) ///#define mod 10007 const int maxn = 210; using namespace std; struct matrix
{
LL f[3][3];
};
LL mod; matrix mul(matrix a, matrix b, int n)
{
matrix c;
memset(c.f, 0, sizeof(c.f));
for(int k = 0;k < n; k++)
{
for(int i = 0; i < n;i++)
{
if(!a.f[i][k]) continue;
for(int j = 0; j < n; j++)
{
if(!b.f[k][j]) continue;
c.f[i][j]=(c.f[i][j]+a.f[i][k]*b.f[k][j]+mod)%mod;
}
}
}
return c;
}
matrix pow_mod(matrix a, LL b, int n)
{
matrix s;
memset(s.f, 0 , sizeof(s.f));
for(int i = 0; i < n; i++) s.f[i][i] = 1LL;
while(b)
{
if(b&1) s = mul(s, a, n);
a = mul(a, a, n);
b >>= 1;
}
return s;
} int main()
{
LL a, b, n;
while(~scanf("%I64d %I64d %I64d %I64d",&a, &b, &n, &mod))
{
if(n == 1)
{
printf("%I64d\n",2*a%mod);
continue;
}
matrix c;
memset(c.f, 0 , sizeof(c.f));
c.f[0][0] = 2LL*a;
c.f[0][1] = b-a*a;
c.f[1][0] = 1LL;
matrix d = pow_mod(c, n-1, 2);
printf("%I64d\n",((d.f[0][0]*2*a+d.f[0][1]*2)%mod+mod)%mod);
}
return 0;
}</span>

HDU 4565 So Easy!(公式化简+矩阵)的更多相关文章

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

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

  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! 数学 + 矩阵 + 整体思路化简

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

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

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

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

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

  6. HDU 4565 So Easy!(矩阵)

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

  7. 数学(矩阵乘法):HDU 4565 So Easy!

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

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

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

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

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

随机推荐

  1. URLRewriter.dll的使用

    http://www.cnblogs.com/Jaylong/archive/2011/10/17/url.html 微软URLRewriter.dll的url重写的简单使用 先添加引用URLRewr ...

  2. Emmet初探2

    关于Emmet Emmet插件的前身是Zen coding,可以大幅度提高前端开发效率的一个工具,也有人说类似于jade(高性能的模板引擎,它深受 Haml 影响,它是用 JavaScript 实现的 ...

  3. jquery-序列化表单

      createTime--2016年9月25日08:54:48参考链接:http://www.w3school.com.cn/tags/html_ref_urlencode.htmljQuery的s ...

  4. 〖Linux〗Android NDK调用已编译好的C/C++动态连接库(so文件)

    一.背景:假定已有应用程序zigbeeclient.cpp,内容如下: ... extern "C" { int getresult(int argc, char **argv); ...

  5. 全面提升WordPress前台和后台的 打开速度的方案

    装好WordPress之后,准备想访问自己的网站,或是登入后台的时候,却发现,这个速度不敢恭维,即使是本地话的程序,也是慢慢的.操作起来也要挺久.那下面我们来解决一下这个问题,提升WordPress的 ...

  6. 使用openssl进行证书格式转换

    各类证书由于存储的内容不同(如是否包含公钥/私钥是否加密存储/单一证书或多证书等).采用编 码不同(DER/BASE64).标准不同(如PEM/PKCS),所以尽管X.509标准规定了证书内容规范,但 ...

  7. m2a-vm超频的方法

    m2a-vm超频的方法 此帖对"华硕超频俱乐部"的评论 要超频的话,bios要进行相应的设置, 1.jumperfree选项下    cpu multiplier  改cpu的倍频 ...

  8. PHP生成缩略图、加水印

    <?php class ThumbWaterImages{ /** * 生成缩略图/加水印 * classname ThumbWaterImages * datetime:2015-1-15 * ...

  9. Spring配置文件头信息

    代码如下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http:// ...

  10. 初学HTML一些基本控件语句

    <html> <head> <title> 这是网页的标题</title> </head> <body> <h2>& ...