1. #include<iostream>
  2. #include<cstdio>
  3. #include<queue>
  4. #include<algorithm>
  5. #include<cmath>
  6. #include<ctime>
  7. #include<set>
  8. #include<map>
  9. #include<stack>
  10. #include<cstring>
  11. #define inf 2147483647
  12. #define ls rt<<1
  13. #define rs rt<<1|1
  14. #define lson ls,nl,mid,l,r
  15. #define rson rs,mid+1,nr,l,r
  16. #define N 100010
  17. #define For(i,a,b) for(long long i=a;i<=b;i++)
  18. #define p(a) putchar(a)
  19. #define g() getchar()
  20.  
  21. using namespace std;
  22. long long aa,bb,n,mod,tt;
  23. double t;
  24. struct matrix{
  25. long long a[];
  26. long long b[][];
  27. matrix operator *(const matrix&c)const{
  28. matrix r;
  29. For(i,,)
  30. For(j,,){
  31. r.b[i][j]=;
  32. For(k,,)
  33. r.b[i][j]=(r.b[i][j]+b[i][k]*c.b[k][j]%mod)%mod;
  34. }
  35. return r;
  36. }
  37. }a;
  38.  
  39. void in(long long &x){
  40. long long y=;
  41. char c=g();x=;
  42. while(c<''||c>''){
  43. if(c=='-')y=-;
  44. c=g();
  45. }
  46. while(c<=''&&c>=''){
  47. x=(x<<)+(x<<)+c-'';c=g();
  48. }
  49. x*=y;
  50. }
  51. void o(long long x){
  52. if(x<){
  53. p('-');
  54. x=-x;
  55. }
  56. if(x>)o(x/);
  57. p(x%+'');
  58. }
  59.  
  60. matrix ksm(matrix a,long long b){
  61. matrix r=a;
  62. b--;
  63. if(b==) return r;
  64. while(b%==){
  65. a=a*a;
  66. b>>=;
  67. }
  68. while(b>){
  69. if(b%==)
  70. r=r*a;
  71. a=a*a;
  72. b>>=;
  73. }
  74. return r;
  75. }
  76.  
  77. int main(){
  78. while(cin>>aa>>bb>>n>>mod){
  79. if(n==){
  80. o((long long)%mod);p('\n');
  81. continue;
  82. }
  83. a.a[]=;
  84. a.a[]=;
  85. a.b[][]=aa%mod;
  86. a.b[][]=bb%mod;
  87. a.b[][]=;
  88. a.b[][]=aa%mod;
  89. matrix r,ans;
  90. r=ksm(a,n);
  91. For(i,,){
  92. ans.a[i]=;
  93. For(k,,)
  94. ans.a[i]=(ans.a[i]+a.a[k]*r.b[i][k]%mod)%mod;
  95. }
  96. tt=;
  97. tt=(tt+ans.a[])%mod;
  98. o((*tt%mod+mod)%mod);p('\n');
  99. }
  100. return ;
  101. }

hdu4565的更多相关文章

  1. 【HDU4565】So Easy!

    [HDU4565]So Easy! 题面 要你求 \[ \lceil (a+\sqrt b)^n \rceil \% m \] 其中\(0<a,m<2^{15},(a-1)^2<b& ...

  2. HDU2256&&HDU4565:给一个式子的求第n项的矩阵快速幂

    HDU2256 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256 题意:求(sqrt(2)+sqrt(3))^2n%1024是多少. 这个题算是h ...

  3. HDU4565 So Easy! —— 共轭构造、二阶递推数列、矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-4565 So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  4. HDU4565 So Easy!

    /* HDU4565 So Easy! http://acm.hdu.edu.cn/showproblem.php?pid=4565 数论 快速幂 矩阵快速幂 题意:求[(a+sqrt(b))^n ] ...

  5. (转载)HDU4565

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 这个博客讲的比较好:http://blog.csdn.net/ljd4305/article/d ...

  6. HDU4565 && 2013年长沙邀请赛A题

    部分转自http://blog.csdn.net/crazy______/article/details/9021169 #include<cstdio> using namespace ...

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

  8. HDU4565(SummerTrainingDay05-C 矩阵快速幂)

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

  9. hdu4565矩阵快速幂

    这题太坑了...刚开始以为可以用|a+sqrt(b)  1|水过...结果tle,还一直想明明我logn的做法怎么可能tle.. |    0           1| 实在无奈看的题解 (a+sqr ...

  10. hdu4565(矩阵快速幂+经典的数学处理)

    注意题目的一个关键条件(a-1)2< b < a2 , 于是可以知道    0 < a-√b < 1 ,所以 (a-√b)^n < 1 . 然后 (a+ √b)^n+(a ...

随机推荐

  1. java_List接口

    /** * java.util.list接口 extends Collection接口 * 1.有序的集合 * 2.有索引 * 3.元素可以重复 * * List中带索引的方法: *add:添加 * ...

  2. C#窗体代码相关笔记

    获取ComboBox下拉列表的所有选项值 ArrayList al = new ArrayList(); foreach (string item in this.comboBox2.Items) { ...

  3. Java开发系列-注解

    概述 在JDK5之后提供了一个新特性,和类.接口同级.定义时使用的关键字是@interface.注解主要有三个作用,分别是编译检查.替代配置文件.定义注解(元注解).分析代码(用到反射).注解的本质就 ...

  4. uoj349 即时战略

    题意:这是一道交互题.交互库中有一棵树.一开始只有1节点已知.需要在T次询问内使得n个节点都已知.一次询问explore(x,y),返回从x到y路径上第一个点,并将返回点标记为已知. 数据有区分. 标 ...

  5. javaweb的几种开发模式

    1.MVC模式基础 1.1.MVC模式简介 MVC是一种架构型模式,它本身并不引入新的功能,只是用来指导我们改善应用程序的架构,使得应用的模型和视图相分离,从而达到更好的开发和维护效率.在MVC模式中 ...

  6. tty who 命令

    #tty : 查看当前终端对应的终端的设备文件 #who : 查看当前系统登录的所有用户及其信息

  7. PAT甲级——A1068 Find More Coins

    Eva loves to collect coins from all over the universe, including some other planets like Mars. One d ...

  8. eclipse memory analyzer对系统内存溢出堆文件解析0(转)

    前言 在平时工作过程中,有时会遇到OutOfMemoryError,我们知道遇到Error一般表明程序存在着严重问题,可能是灾难性的.所以找出是什么原因造成OutOfMemoryError非常重要.现 ...

  9. c#设置文件及文件夹的属性

    c#中通过FileAttributes枚举来设置文件或文件夹的属性. FileAttributes 枚举 成员名称 说明 Archive 文件的存档状态.应用程序使用此属性为文件加上备份或移除标记. ...

  10. EF中的DbContext类

    使用先前的数据上下文,可以通过使用LINQ查询,按字母顺序检索出所有专辑,代码如下