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
------------------------------------------------------------------------------------------------------------------
  1. #include<stdio.h>
  2. #include<string.h>
  3. #define SIZE 2
  4. long long int ** multiple(long long int a[][SIZE], long long int b[][SIZE]);
  5. int main()
  6. {
  7. int A, B;
  8. long long int n;
  9. long long int factor[SIZE][SIZE];
  10. long long int ans[SIZE][SIZE];
  11. while (scanf("%d%d%lld", &A, &B, &n) && (A||B||n))
  12. {
  13. memset(factor, 0, 2 * 2 * sizeof(int));
  14. memset(ans, 0, 2 * 2 * sizeof(int));
  15. factor[0][0] = A;
  16. factor[0][1] = B;
  17. factor[1][0] = 1;
  18. factor[1][1] = 0;
  19. //矩阵快速幂
  20. for (int i = 0; i < SIZE; i++)
  21. ans[i][i] = 1;
  22. n = n - 2;
  23. while (n > 0)
  24. {
  25. if (n & 1)
  26. memcpy(ans, multiple(ans, factor), 2 * 2 * sizeof(long long int));
  27. memcpy(factor, multiple(factor, factor), 2 * 2 * sizeof(long long int));
  28. n >>= 1;
  29. }
  30. printf("%lld\n", (ans[0][0] + ans[0][1]) % 7);
  31. }
  32. return 0;
  33. }
  34.  
  35. long long int ** multiple(long long int a[][SIZE], long long int b[][SIZE])
  36. {
  37. int i, j, k;
  38. long long int c[SIZE][SIZE];
  39. memset(c, 0, 2 * 2 * sizeof(long long int));
  40. for(i = 0; i < SIZE; i++)
  41. for(j = 0; j < SIZE; j++)
  42. for (k = 0; k < SIZE; k++)
  43. {
  44. c[i][j] += a[i][k] * b[k][j];
  45. c[i][j] = c[i][j] % 7;
  46. }
  47. return c;
  48. }

参考文章:

https://blog.csdn.net/codeswarrior/article/details/81258928
https://www.cnblogs.com/cmmdc/p/6936196.html

ACM1005:Number Sequence的更多相关文章

  1. 1005:Number Sequence(hdu,数学规律题)

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

  2. 递推:Number Sequence(mod找规律)

    解题心得: 1.对于数据很大,很可怕,不可能用常规手段算出最后的值在进行mod的时候,可以思考找规律. 2.找规律时不必用手算(我傻,用手算了好久).直接先找前100项进行mod打一个表出来,直接看就 ...

  3. HDU 1711:Number Sequence(KMP)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. POJ 1019:Number Sequence 二分查找

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36013   Accepted: 10409 ...

  5. HDU 1711:Number Sequence(KMP模板,求位置)

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. hdu5014:number sequence对称思想

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5014 题目大意:给定数组 a[]={0,1,2......n} 求一个数组b[] 元素也为0.... ...

  7. HDU1711:Number Sequence

    Problem Description Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], .... ...

  8. hdu1005 Number Sequence(寻找循环节)

    主题链接: pid=1005">huangjing 题意: 就是给了一个公式,然后求出第n项是多少... 思路: 题目中n的范围实在是太大,所以肯定直接递推肯定会超时,所以想到的是暴力 ...

  9. [AX]AX2012 Number sequence framework :(三)再谈Number sequence

    AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal nu ...

随机推荐

  1. Linux基础和常用命令

    经常使用的命令: #查看端口被占用情况 netstat -tunlp|grep #查看java进程 ps -ef|grep java #压缩前端工程 rar a -ep1 ./update/win32 ...

  2. redis下的持久化保存

    rdb.h   rdb.c  --->  完成数据保存到临时文件,再利用rename保存到指定文件的过程: 如果需要写一个数据持久化保存的功能时,可以参考这部分代码: //rdb API int ...

  3. Chapter 2 Secondary Sorting:Detailed Example

    2.1 Introduction MapReduce framework sorts input to reducers by key, but values of reducers are arbi ...

  4. 深入了解Node模块原理

    深入了解Node模块原理 当我们编写JavaScript代码时,我们可以申明全局变量: var s = 'global'; 在浏览器中,大量使用全局变量可不好.如果你在a.js中使用了全局变量s,那么 ...

  5. python读取xml文件报错ValueError: multi-byte encodings are not supported

    1.在使用python对xml文件进行读取时,提示ValueError: multi-byte encodings are not supported 很多贴子上说把xml的编码格式改为,就可以正常执 ...

  6. 把对象缓存到HttpRuntime.Cache里,你能安全地使用它吗?

    每每勤勤恳恳,思来想去,趁还有激情,先把它记录下来... 定义一个Stu的类: public class Stu { public string Name { get; set; } public i ...

  7. 1053. [HAOI2007]反素数ant【DFS+结论】

    Description 对于任何正整数x,其约数的个数记作g(x).例如g(1)=1.g(6)=4.如果某个正整数x满足:g(x)>g(i) 0<i<x ,则称x为反质数.例如,整数 ...

  8. windows C++ new/delete内存大小

    转载自:https://blog.csdn.net/will_hsbsch/article/details/21124055 windows 上,但使用C++语言new一块内存,用指针P指向这块内存, ...

  9. 【问题】 cookie 不保存特殊字符 解决办法

    遇到的问题: 在做项目,用geolocation 获取经纬度,格式如(23.1133,113.2552) ,想保存到cookie中备用.但读取cookie出来之后发现逗号变成了  %2c. 找到的原因 ...

  10. Lua库-string库

    string.len(s) string.rep(s,n) string.lower(s) string.upper(s) string.sub(s,i);//截取s第i个开始的后缀 string.s ...