So Easy! HDU - 4565

A sequence S n is defined as:

Sn=⌈(a+b√)n⌉%mSn=⌈(a+b)n⌉%m

S_n = \lceil(a+\sqrt{b})^n\rceil\%m

Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate S n.
  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 < 2 15, (a-1) 2< b < a 2, 0 < b, n < 2 31.The input will finish with the end of file.
Output
  For each the case, output an integer S n.
Sample Input

2 3 1 2013
2 3 2 2013
2 2 1 2013

Sample Output

4
14
4

题意:

求上面的公式

分析:

直接看这篇文章吧,照着这个题写的讲解

斐波那契数列通项公式推导及其类似构造共轭公式反求递推式

得到递推式

fn=2a⋅fn−1+(b−a2)⋅fn−2fn=2a⋅fn−1+(b−a2)⋅fn−2

f_n = 2a\cdot f_{n-1} + (b-a^2)\cdot f_{n-2}

打表发现这个递推式取模好像没有循环节因此只能老老实实用矩阵快速幂

(fnfn−1)=(2a1b−aa0)⋅(fn−1fn−2)(1)(1)(fnfn−1)=(2ab−aa10)⋅(fn−1fn−2)

\begin{equation}\left(\begin{matrix}f_n\\f_{n-1}\\\end{matrix}\right)=\left(\begin{matrix}2a & b-a^a \\1 & 0\\\end{matrix}\right)\cdot\left(\begin{matrix}f_{n-1}\\f_{n-2}\end{matrix}\right)\end{equation}

(fnfn−1)=(2a1b−aa0)n−1⋅(f1f0)(2)(2)(fnfn−1)=(2ab−aa10)n−1⋅(f1f0)

\begin{equation}\left(\begin{matrix}f_n\\f_{n-1}\\\end{matrix}\right)=\left(\begin{matrix}2a & b-a^a \\1 & 0\\\end{matrix}\right)^{n-1}\cdot\left(\begin{matrix}f_{1}\\f_{0}\end{matrix}\right)\end{equation}

code:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+10;
int a,b,n,m;
struct Matrix{int mm[3][3];
};
Matrix mul(Matrix aa,Matrix bb){Matrix ans;for(int i = 0; i < 2; i++){for(int j = 0; j < 2; j++){ans.mm[i][j] = 0;for(int k = 0; k < 2; k++){ans.mm[i][j] = (ans.mm[i][j] + aa.mm[i][k] * bb.mm[k][j] % m + m) % m;}}}return ans;
}
Matrix q_pow(Matrix aa,int bb){Matrix ans;memset(ans.mm,0,sizeof(ans.mm));for(int i = 0; i < 2; i++){ans.mm[i][i] = 1;}while(bb){if(bb & 1)ans = mul(ans,aa);bb >>= 1;aa = mul(aa,aa);}return ans;
}
int main(){while(~scanf("%d%d%d%d",&a,&b,&n,&m)){Matrix ans;ans.mm[0][0] = 2 * a % m;ans.mm[0][1] = b - a * a;ans.mm[1][0] = 1;ans.mm[1][1] = 0;ans = q_pow(ans,n-1);printf("%d\n",(ans.mm[0][0] * 2 * a % m + ans.mm[0][1] * 2 % m + m) % m);}return 0;
}

同类型题目Best Solver HDU - 5451
这道题取模有循环节,处理起来就相对简单些了

So Easy! HDU - 4565(构造共轭+矩阵快速幂)相关推荐

  1. 湘潭赛Easy wuxing(递推+矩阵快速幂or DP)

    湘潭赛Easy wuxing(递推+矩阵快速幂or DP) 十分感谢老师的思路! 题目描述 "五行"是中国传统哲学思想,它认为认为大自然的现象由"木.火.土.金.水&qu ...

  2. HDU - 5015 233 Matrix(矩阵快速幂)

    题目链接:点击查看 题目大意:初始化:第一行依次为233,2333,23333....第一列依次为a0,a1,a2....(题目中会给出),再给出递推公式:,求矩阵中第n行m列的数字是多少 题目分析: ...

  3. hdu 2842 Chinese Rings 矩阵快速幂

    分析: 后面的环能不能取下来与前面的环有关,前面的环不被后面的环所影响.所以先取最后面的环 设状态F(n)表示n个环全部取下来的最少步数 先取第n个环,就得使1~n-2个环属于被取下来的状态,第n-1 ...

  4. HDU - 4990 Reading comprehension(矩阵快速幂,水题)

    题目链接:点击查看 题目大意:给出一段程序,进行优化后提交 题目分析:其实就是找规律,大水题一个,偶尔也是需要做做水题找找自信(逃) 先将题目中的程序拿下来,跑上100项,然后拿到oeis里找一下规律 ...

  5. Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

    题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但 ...

  6. HDU 2256Problem of Precision(矩阵快速幂)

    题意 求$(\sqrt{2} + \sqrt{3})^{2n} \pmod {1024}$ $n \leqslant 10^9$ Sol 看到题解的第一感受:这玩意儿也能矩阵快速幂??? 是的,它能q ...

  7. HDU (1575)Tr A ---矩阵快速幂

    Tr A Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每 ...

  8. 考研路茫茫――单词情结 HDU - 2243(ac自动机 + 矩阵快速幂)

    考研路茫茫--单词情结 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. 又见斐波那契数列(矩阵构造+矩阵快速幂)

    //补题~~~ 链接:https://ac.nowcoder.com/acm/problem/15666 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其 ...

  10. 矩阵快速幂的学习(系统的学习)

    学习博客:https://www.cnblogs.com/cmmdc/p/6936196.html https://www.cnblogs.com/yan-boy/archive/2012/11/29 ...

最新文章

  1. 基于TensorRT车辆实时推理优化
  2. qt4如何读oracle,Qt4编程的控制MPlayer
  3. java.lang.UnsatisfiedLinkError:no jhdf5 in java.library.path问题的解决
  4. 现代化医学信息管理c语言,山西医科大学_院校信息库_阳光高考
  5. iphone屏幕上的圆圈怎么设置_iphone亮度条不变屏幕变暗怎么回事【解决方法】
  6. C和指针之实现strlen函数
  7. 洛谷P2480:古代猪文(中国剩余定理)(欧拉定理)
  8. HTTP协议的请求协议(个人笔记看不懂的地方可以和我交流)
  9. 25. JavaScript PopupAlert
  10. python 重新执行循环中出错的那一次
  11. Jenkins下的Pipeline流水线入门篇
  12. php speex转码为mp3,ffmpeg speex转换为mp3或者aac
  13. 前端开发SEO搜索引擎优化方案
  14. post 防篡改_如何防止http请求数据被篡改
  15. python中gzip库用法详解(压缩和解压缩)
  16. html卡片式轮播图带字,卡片式轮播
  17. Centos 7 怎么都连不上手机阿阿阿阿Android Studio 怎么都检测不到真机啊还有关于git本地提交就缺少文件啊啊啊啊
  18. 字符串 Z 字形变换(Java)
  19. 全国计算机等级考试二级cpp试题,2017年全国计算机二级C++考试试题附答案
  20. pascal语言的版本

热门文章

  1. Arduino ide配置esp32硬件支持(配置esp32的arduino开发环境)
  2. Sensor 数据整理
  3. 实例分割:R-CNN、Fast R-CNN、Faster R-CNN、Mask R-CNN
  4. jupyter更改默认浏览器的方法
  5. html手机端富文本,移动端富文本踩坑
  6. ETF基金定投策略回测分析
  7. iso计算机术语简单解释,计算机网络知识(上)
  8. 大学生数学竞赛试题荟萃 (更新至2017年10月28日)
  9. 南澳大学计算机科学专业学费,澳洲南澳大学生活费
  10. Java自定义类的属性、方法结合数组简单使用