转载: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. 使用js对select动态添加和删除OPTION

    <select id="ddlResourceType" onchange="getvalue(this)"> </select> 动态 ...

  2. 移动端兼容 - faskclick.js

    fasckclick为解决移动端300ms延迟而生 github地址为:https://github.com/ftlabs/fastclick 使用方法: 1. 原生使用(window.onload或 ...

  3. 算法笔记_188:历届试题 危险系数(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 问题描述 抗日战争时期,冀中平原的地道战曾发挥重要作用. 地道的多个站点间有通道连接,形成了庞大的网络.但也有隐患,当敌人发现了某个站点后,其它站点 ...

  4. STS项目html文件中文乱码解决

    解决方案: windows -- perferences -- encoding,设置成utf-8 步骤一:Content Types 步骤二:Workspace 步骤三:JSP Files

  5. 发现linux shell中$0,$?,$!等的特殊用法

    记录下linux shell下的特殊用法及参数的说明 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代 ...

  6. Divisibility by Eight

    把当前数删除几位然后能够整除与8 那么可得知大于3位数的推断能否整除于八的条件是(n%1000)%8==0 能够得出我们的结论:仅仅须要枚举后三位后两位后一位就可以知道是否可整除于8 #include ...

  7. 24、java操作xml方法

    XML解析方式 1. SAX解析方式 SAX(simple API for XML)是一种XML解析的替代方法.相比于DOM,SAX是一种速度更快,更有效的方法.它逐行扫描文档,一边扫描一边解析.而且 ...

  8. phpCAS library

    The phpCAS library provides a simple API for authenticating users against a CAS server. phpCAS is co ...

  9. Web Service基础——规范及三要素

    1. Java中的Web Service规范 Java 中共有三种WebService 规范,分别是JAX-WS(JAX-RPC).JAX-RS.JAXM&SAAJ(废弃). 1.1 JAX- ...

  10. PHP-表达式

    最精确的定义一个表达式的方式就是"任何有值的东西" $a = 5; 1 > 2;等