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
 
挑战程序设计竞赛P268,关键是求初始矩阵:
an+1 = a*an+b*bn;
bn+1 = an+a*bn;
a[0][0],a[0][1]分别是公式1里面的系数,a和b;
a[1][0],a[1][1]分别是公式2里面的系数,1和a;
然后就是矩阵快速幂了:
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<cmath>
#define M(a,b) memset(a,b,sizeof(a))
typedef long long LL; using namespace std; long long a,b,n,m; struct matrix
{
LL mat[][];
void init()
{
mat[][] = a;
mat[][] = b;
mat[][] = ;
mat[][] = a;
}
}; matrix mamul(matrix aa,matrix bb)
{
matrix c;
for(int i = ;i<;i++)
{
for(int j = ;j<;j++)
{
c.mat[i][j] = ;
for(int k = ;k<;k++)
c.mat[i][j]+=(aa.mat[i][k]*bb.mat[k][j]);
c.mat[i][j]%=m;
}
}
return c;
} matrix mul(matrix s, int k)
{
matrix ans;
ans.init();
while(k>=)
{
if(k&)
ans = mamul(ans,s);
k = k>>;
s = mamul(s,s);
}
return ans;
} int main()
{
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&m)==)
{
matrix ans;
ans.init();
ans = mul(ans,n-);
printf("%I64d\n",(ans.mat[][]*+m)%m);
}
return ;
}

2013长沙邀请赛A So Easy!(矩阵快速幂,共轭)的更多相关文章

  1. 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 ...

  2. HDU 4565 So Easy! 矩阵快速幂

    题意: 求\(S_n=\left \lceil (a+\sqrt{b})^n \right \rceil mod \, m\)的值. 分析: 设\((a+\sqrt{b})^n=A_n+B_n \sq ...

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

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

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

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

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

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

  6. 2013长春网赛1009 hdu 4767 Bell(矩阵快速幂+中国剩余定理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4767 题意:求集合{1, 2, 3, ..., n}有多少种划分情况bell[n],最后结果bell[ ...

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

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

  8. hdu4565 So Easy!(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题解:(a+√b)^n=xn+yn*√b,(a-√b)^n=xn-yn*√b, (a+√b)^n ...

  9. 徐州邀请赛 江苏 icpc I. T-shirt 矩阵快速幂

    题目 题目描述 JSZKC is going to spend his vacation! His vacation has N days. Each day, he can choose a T-s ...

随机推荐

  1. cobbler重装、web、定制化

    cobbler重装 根据此文已自动化安装centos 7的版本http://www.cnblogs.com/shhnwangjian/p/5858900.html 在cobbler-test主机上重装 ...

  2. [VSTS] 从零开始 Team Foundation Server 2010 安装配置详细图文教程

    http://www.cnblogs.com/WilsonWu/archive/2011/11/24/2261674.html 近期公司要配TFS用于新项目的管理,公司也将逐步迁移至VSTS平台,前期 ...

  3. 【原】js检测移动端横竖屏

    摘要:上周做了一个小项目,但是要放到我们的app上,然而需要横竖屏使用不同的样式.横屏一套,竖屏一套.调用了手机APP那里的api,可是他们那里ios和安卓返回的不一样. 各种头疼.于是用了css3的 ...

  4. SimpleDateFormat 12小时制以及24小时制的写法

    有些代码按了复制键没有效果,但是其实已经复制到剪贴板上面了,大家请注意哦! 我的文章有时会稍有修改,转载请注明出处哦! 原文地址:SimpleDateFormat 12小时制以及24小时制的写法 去代 ...

  5. windows7-SQLyog 安装图解

    双击: 双击已下载的SQLyog Enterprise 安装文件,点击“next”,选择“I accept...”,勾选安装组件,选择安装目录,等待安装完成. 协议:选择我接受 选择操作   选择路径 ...

  6. sphinx :undefined reference to `libiconv' 报错解决办法

    sphinx :undefined reference to `libiconv' 报错解决办法   2013-11-30 21:45:39 安装sphinx时不停报错...郁闷在make时报错,错误 ...

  7. ./yy.sh -d bash 执行脚本时所加的参数

    -e filename 如果 filename存在,则为真 -d filename 如果 filename为目录,则为真 -f filename 如果 filename为常规文件,则为真 -L fil ...

  8. win7开防火墙,允许ping通

  9. linux设置和删除环境变量

    删除环境变量: unset -v PKG_CONFIG_PATH 添加环境变量: export PKG_CONFIG_PATH="你需要设置的路径", 例如: export LAN ...

  10. ecshop 订单-》订单状态 2

    // 判断订单状态 public function get_order_status($os,$ps,$ss){ $arr = array('已取消','待付款','待发货','待收货','确认收货' ...