转载: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. C# Console 运行之后最小化到状态栏

    static void Main(string[] args) { new ConsoleCtrl(); Console.Read(); } class ConsoleCtrl { [DllImpor ...

  2. 【Maven】IKAnalyzer 在Maven Repository不存在

    1.在mvnrepository里面找IKAnalyzer,这个中文分词包,一直没有找到,找到github,发现是一个国人写的. http://mvnrepository.com/search?q=I ...

  3. 算法笔记_182:历届试题 核桃的数量(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 小张是软件项目经理,他带领3个开发组.工期紧,今天都在加班呢.为鼓舞士气,小张打算给每个组发一袋核桃(据传言能补脑).他的要求是: 1. ...

  4. C和C++静态检查规范

  5. ERROR 1227 (42000): Access denied; you need (at least one of) the SUPER privilege(s) for this operation

    开启super权限: 1. update user set Super_priv=‘Y’ where User=‘root’2. flush privileges

  6. 解决win10鼠标晃动问题

    删除HKEY_CLASSES_ROOTDirectoryBackgroundshellexContextMenuHandlers 下面,除了new以外的文件夹 重启,Ok

  7. 最新美行地图Z13升级攻略

    原文地址:http://bbs.gpsuu.com/read.php?tid-231134.html  2013年11月16日订车,4S答应送导航,却没有提送什么导航.12月24日提车,DVD导航一体 ...

  8. 【asp.net Core 2.0 初步探索】

    首先下载 对应的SDK 和runtime https://www.microsoft.com/net/core#linuxubuntu           ---------当前为 1.1 稳定版本 ...

  9. OpenStack 网络:Neutron 初探

    OpenStack Neutron 网络模型 OpenStack nova-network 独立成为单独的组件 Neutron 后,形象的网络模型的多平面网络.混合平面私有网络.如图 3,图 4,图 ...

  10. 安装到LG手机出错

    [2013-07-10 07:44:31 - txrjsms] ERROR: Application requires API version 11. Device API version is 8 ...