[关闭]
@DingCao-HJJ 2015-09-21T20:41:47.000000Z 字数 2140 阅读 1131

sicily_1863 Elegant fibonacci numbers again

sicily


题目

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB 

Description

Fibonacci numbers are nice simple integers. All of you are familiar with it, aren’t you?
The Fibonacci sequence <F[n]> are defined by the recurrence:
F[0]=0;
F[1]=1;
F[n]=F[n-1]+F[n-2], for n>1
You know that the value of F[n] increases rapidly when the n becomes larger. So for each test case,output the value F[n] mod m will be ok. 

Input

The first line of the input is a positive integer.It is the number of the test cases followed. Each test case contains two integers n (0<=n<2^32) and m (0<m<10000). There may be one or several spaces between the two integers. 

Output

The output of the program should consist of one line of output for each test case.The output of each test case only contains one integer which equals to the value F[n] mod m. No any redundant spaces are needed. 

Sample Input

2
1 1000
2 100

Sample Output
1
1

思路

使用矩阵乘法来快速求出对应的斐波那契数列项。

代码

  1. // Copyright (c) 2014 Junjie_Huang@SYSU(SNO:13331087). All Rights Reserved.
  2. // 1863.c: http://soj.me/1863
  3. #include<stdio.h>
  4. #include<string.h>
  5. void matrix_copy(long long matrix_2[2][2], long long matrix_tmp[2][2]) {
  6. int i, j;
  7. for (i = 0; i < 2; i++) {
  8. for (j = 0; j < 2; j++) {
  9. matrix_2[i][j] = matrix_tmp[i][j];
  10. }
  11. }
  12. }
  13. void matrix_multiplication(long long matrix_1[2][2],
  14. long long matrix_2[2][2],
  15. long long m) {
  16. long long matrix_tmp[2][2] = { 0 };
  17. matrix_tmp[0][0] = (matrix_1[0][0] * matrix_2[0][0]
  18. + matrix_1[0][1] * matrix_2[1][0]) % m;
  19. matrix_tmp[1][0] = (matrix_1[1][0] * matrix_2[0][0]
  20. + matrix_1[1][1] * matrix_2[1][0]) % m;
  21. matrix_tmp[0][1] = (matrix_1[0][0] * matrix_2[0][1]
  22. + matrix_1[0][1] * matrix_2[1][1]) % m;
  23. matrix_tmp[1][1] = (matrix_1[1][0] * matrix_2[0][1]
  24. + matrix_1[1][1] * matrix_2[1][1]) % m;
  25. matrix_copy(matrix_2, matrix_tmp);
  26. }
  27. void matrix_pow(long long matrix[2][2],
  28. long long matrix_tmp[2][2],
  29. long long n, long long m) {
  30. if (n == 0) {
  31. matrix_tmp[0][0] = 0;
  32. return;
  33. } else if (n == 1) {
  34. return;
  35. }
  36. n -= 2;
  37. while (n) {
  38. if (n & 1) matrix_multiplication(matrix, matrix_tmp, m);
  39. matrix_multiplication(matrix, matrix, m);
  40. n >>= 1;
  41. }
  42. return;
  43. }
  44. int main() {
  45. int T;
  46. long long n, m;
  47. long long matrix_0[2][2] = { { 1, 1 }, { 1, 0 } },
  48. matrix_result[2][2] = { { 1, 1 }, { 1, 0 } },
  49. matrix_tmp[2][2] = { { 1, 1 }, { 1, 0 } };
  50. scanf("%d", &T);
  51. while (T--) {
  52. matrix_copy(matrix_tmp, matrix_0);
  53. matrix_copy(matrix_result, matrix_0);
  54. scanf("%lld %lld", &n, &m);
  55. matrix_pow(matrix_tmp, matrix_result, n, m);
  56. printf("%d\n", matrix_result[0][0]);
  57. }
  58. return 0;
  59. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注