题意:问funny被调用了多少次,结果ModP,P不一定为质数。
 
首先很容易发现递推公式fn=fn-1*fn-2;写出前几项a,b,a*b,a*b^2,a^2*b^3,a^3*b^5;易发现a,b的指数为斐波那契数列。但是当N大一点时,斐波那契数列便变得非常大。那么此时得用欧拉定理降幂。
特别要主要使用条件,x>phi(c)。
接着用矩阵快速幂求斐波那契数列,再快速幂求答案即可。
 
  1. #include<cstdio>
  2. #include<cstring>
  3. using namespace std;
  4. typedef long long ll;
  5. const int maxn=1e6+;
  6. int euler[maxn];
  7.  
  8. ll a,b,m,n;
  9.  
  10. int phi()
  11. {
  12. for(int i=;i<maxn;i++) euler[i]=i;
  13. for(int i=;i<maxn;i++)
  14. {
  15. if(euler[i]==i)
  16. {
  17. for(int j=i;j<maxn;j+=i)
  18. {
  19. euler[j]=euler[j]/i*(i-);
  20. }
  21. }
  22. }
  23. }
  24.  
  25. struct Matrix
  26. {
  27. ll a[][];
  28. Matrix(){memset(a,,sizeof(a));}
  29. Matrix operator* (const Matrix &p)
  30. {
  31. Matrix res;
  32. for(int i=;i<;i++)
  33. {
  34. for(int j=;j<;j++)
  35. {
  36. for(int k=;k<;k++)
  37. {
  38. res.a[i][j]+=a[i][k]*p.a[k][j];
  39. if(res.a[i][j]>euler[m])//特别要注意这个条件
  40. {
  41. res.a[i][j]=res.a[i][j]%euler[m]+euler[m];
  42. }
  43. }
  44. }
  45. }
  46. return res;
  47. }
  48. }ans,base;
  49.  
  50. Matrix quick_pow(Matrix base,ll n)
  51. {
  52. Matrix res;
  53. for(int i=;i<;i++)
  54. {
  55. res.a[i][i]=;
  56. }
  57. while(n)
  58. {
  59. if(n&) res=res*base;
  60. base=base*base;
  61. n>>=;
  62. }
  63. return res;
  64. }
  65.  
  66. ll pow(ll a,ll n)
  67. {
  68. ll ans=;
  69. while(n)
  70. {
  71. if(n&) ans=ans*a%m;
  72. a=a*a%m;
  73. n>>=;
  74. }
  75. return ans;
  76. }
  77.  
  78. void Matrix_init()
  79. {
  80. ans.a[][]=;
  81. ans.a[][]=;
  82. ans.a[][]=;
  83. ans.a[][]=;
  84. base.a[][]=;
  85. base.a[][]=;
  86. base.a[][]=;
  87. base.a[][]=;
  88. }
  89.  
  90. int main()
  91. {
  92. int t,cas=;
  93. ll ansa,ansb,faca,facb;
  94. phi();
  95. scanf("%d",&t);
  96. while(t--)
  97. {
  98. cas++;
  99. scanf("%lld%lld%lld%lld",&a,&b,&m,&n);
  100. printf("Case #%d: ",cas);
  101. if(n==) printf("%lld\n",a%m);
  102. else if(n==) printf("%lld\n",b%m);
  103. else if(n==) printf("%lld\n",a*b%m);
  104. else if(m==) printf("0\n");
  105. else
  106. {
  107. Matrix_init();
  108. ans=ans*quick_pow(base,n-);
  109. faca=ans.a[][];
  110. facb=ans.a[][];
  111. ansa=pow(a,faca)%m;
  112. ansb=pow(b,facb)%m;
  113. printf("%lld\n",ansa*ansb%m);
  114. }
  115. }
  116. return ;
  117. }

HDU 3221 Brute-force Algorithm的更多相关文章

  1. HDU 6215 Brute Force Sorting(模拟链表 思维)

    Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  2. HDU 6215 Brute Force Sorting(链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去.然后继续去 ...

  3. hdu 6215 -- Brute Force Sorting(双向链表+队列)

    题目链接 Problem Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's st ...

  4. hdu 6215 Brute Force Sorting(模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题解:类似双链表的模拟. #include <iostream> #include ...

  5. HDU 6215 Brute Force Sorting 模拟双端链表

    一层一层删 链表模拟 最开始写的是一个一个删的 WA #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) mem ...

  6. HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting

    Brute Force Sorting Time Limit: 1 Sec  Memory Limit: 128 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...

  7. HDU 4971 A simple brute force problem.

    A simple brute force problem. Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged o ...

  8. SRM 582 Div II Level Three: ColorTheCells, Brute Force 算法

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=12581 Burte Force 算法,求解了所有了情况,注意  ...

  9. Test SRM Level Three: LargestCircle, Brute Force

    题目来源:http://community.topcoder.com/stat?c=problem_statement&pm=3005&rd=5858 思路: 如果直接用Brute F ...

  10. hdu6215 Brute Force Sorting

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215 题目: Brute Force Sorting Time Limit: 1000/100 ...

随机推荐

  1. 使用bootstrap 弹出效果演示

    前言:对于Web开发人员,弹出框和提示框的使用肯定不会陌生,比如常见的表格新增和编辑功能,一般常见的主要有两种处理方式:行内编辑和弹出框编辑.在增加用户体验方面,弹出框和提示框起着重要的作用,如果你的 ...

  2. Delphi以及三方控件的源代码规模

    这些项目大多数使用C++或者C编写,使用SourceCounter-3.5.33.73工具来统计源代码数量,本来是这里下载的: https://code.google.com/p/boomworks/ ...

  3. C# 日期格式转【转】

    使用:DateTime.ToString的方法(String, IFormatProvider)转换格式 using System; using System.Globalization; Strin ...

  4. bash里面的一些符号说明

    $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行的命令的结束代码(返回值) $- 使用Set命令设定的Flag一览 $* 所有参 ...

  5. async 和 await小结

    三大返回值: 返回类型 - Task<TResult> 返回类型 - Task 返回类型 - void 当你添加 async 关键字后,需要返回一个将用于后续操作的对象,请使用 Task& ...

  6. Python学习之路【目录】

    本系列博文包含 Python基础.前端开发.Web框架.缓存以及队列等,希望可以给正在学习编程的童鞋提供一点帮助!!! 目录: Python学习[第一篇]python简介 Python学习[第二篇]p ...

  7. PHP访问带密码的Redis

    1. 设置Redis密码,以提供远程登陆打开redis.conf配置文件,找到requirepass,然后修改如下: requirepass yourpassword yourpassword就是re ...

  8. telnet输入乱码的解决

    1.Win+R --- 运行窗口  输入cmd回车 2.输入telnet 主机 端口 3.连接主机发现无法输入 4.这里什么也不要输入,按下 ctrl+] 键 5.按下回车键,然后会弹出新的窗口,就可 ...

  9. vim - Removing duplicate lines

    http://vim.wikia.com/wiki/Uniq_-_Removing_duplicate_lines :sort u

  10. C#写入日志信息到文件中

    为了在服务器上运行程序及时的跟踪出错的地方,可以在必要的地方加入写日志的程序. string folder = string.Format(@"D:\\{0}\\{1}", Dat ...