Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 177761    Accepted Submission(s):
44124

Problem Description
A number sequence is defined as follows:

f(1) =
1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

Given A, B, and
n, you are to calculate the value of f(n).

 
Input
The input consists of multiple test cases. Each test
case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1
<= n <= 100,000,000). Three zeros signal the end of input and this test
case is not to be processed.
 
Output
For each test case, print the value of f(n) on a single
line.
 
Sample Input
1 1 3
1 2 10
0 0 0
 
Sample Output
2
5
 
Author
CHEN, Shunbao
 
Source
 
Recommend
 
JGShining   |   We have carefully selected several
similar problems for you:  1008 1004 1021 1019 1002 
 
 
这道题需要推一个类似于斐波那契矩阵的矩阵。
比较好推

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<iostream>
  4. using namespace std;
  5. const int MAXN=;
  6. inline void read(int &n){char c='+';bool flag=;n=;
  7. while(c<''||c>'') c=='-'?flag=,c=getchar():c=getchar();
  8. while(c>=''&&c<='') n=n*+c-,c=getchar();flag==?n=-n:n=n;}
  9. struct matrix
  10. {
  11. int m[][];matrix(){memset(m,,sizeof(m));}
  12. };
  13. matrix ma;
  14. int limit=;
  15. const int mod=;
  16. matrix mul(matrix a,matrix b)
  17. {
  18. matrix c;
  19. for(int k=;k<limit;k++)
  20. for(int i=;i<limit;i++)
  21. for(int j=;j<limit;j++)
  22. c.m[i][j]=(c.m[i][j]+(a.m[i][k]*b.m[k][j]))%mod;
  23. return c;
  24. }
  25. matrix fast_martix_pow(matrix ma,int p)
  26. {
  27. matrix bg;
  28. bg.m[][]=;bg.m[][]=;
  29. bg.m[][]=;bg.m[][]=;
  30. /*for(int i=0;i<limit;i++)
  31. {
  32. for(int j=0;j<limit;j++)
  33. cout<<bg.m[i][j]<<" ";
  34. cout<<endl;
  35. }*/
  36.  
  37. while(p)
  38. {
  39. if(p&) bg=mul(bg,ma);
  40. ma=mul(ma,ma);
  41. p>>=;
  42. }
  43. return bg;
  44. }
  45. int main()
  46. {
  47. int a,b,n;
  48. while(scanf("%d%d%d",&a,&b,&n)&&(a!=&&b!=&&n!=))
  49. {
  50. ma.m[][]=a;ma.m[][]=b;
  51. ma.m[][]=;ma.m[][]=;
  52. if(n<)
  53. {
  54. printf("1\n");
  55. continue;
  56. }
  57. matrix ans=fast_martix_pow(ma,n-);
  58. printf("%d\n",(ans.m[][]+ans.m[][])%mod);
  59. }
  60. return ;
  61. }

HDU 1005 Number Sequence(矩阵)的更多相关文章

  1. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  2. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  3. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

  4. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  5. HDU 1005 Number Sequence(数论)

    HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...

  6. HDU 1005 Number Sequence【斐波那契数列/循环节找规律/矩阵快速幂/求(A * f(n - 1) + B * f(n - 2)) mod 7】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. HDU - 1005 Number Sequence (矩阵快速幂)

    A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mo ...

  8. HDU 1005 Number Sequence【多解,暴力打表,鸽巢原理】

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. CentOS7 部署SVN服务器

    服务器端:svnserver 安装主要步骤 yum install subversion rpm -ql subversion mkdir /application/svndata mkdir /ap ...

  2. 【Codeforces】512C Fox and Dinner

    [解析]欧拉筛法,奇偶分析.建二分图,网络流 [Analysis] http://blog.csdn.net/qq574857122/article/details/43453087. 所谓的连通块就 ...

  3. android中的AlertDialog具体概述

    android的AlertDialog具体解释 AlertDialog的构造方法所有是Protected的.所以不能直接通过new一个AlertDialog来创建出一个AlertDialog. 要创建 ...

  4. Hibernate是怎么工作的——Hibernate的工作流程

    举个简单的样例说明: 1.Base.java package cn.flymouse.hibernate; import java.util.Date; import org.hibernate.Qu ...

  5. node07---post请求、表单提交、文件上传

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 自定义标签 Unable to find setter method for attribute

    变量的首字母不能大写 http://blog.csdn.net/looksun/article/details/7690601

  7. thinkphp5项目--个人博客(四)

    thinkphp5项目--个人博客(四) 项目地址 fry404006308/personalBlog: personalBloghttps://github.com/fry404006308/per ...

  8. Mysql优化理论知识

    参考文章 http://blog.51cto.com/lizhenliang/2095526 ()硬件优化 如果有条件一定要SSD固态硬盘代替SAS机械硬盘,将RAID级别调整为RAID1+,相对于R ...

  9. Windows安装PHP MongoDB扩展

    本文将讲述一下在Wamp环境下安装MongoDB扩展的过程,大家可以略作参考 Linux 版本的可以参考之前发布的Linux安装PHP MongoDB扩展 安装环境 系统环境:Windows 7 64 ...

  10. BZOJ 3600 替罪羊树+线段树

    思路: 当然是抄的黄学长的题解啦 //By SiriusRen #include <cstdio> #include <algorithm> using namespace s ...