记录编号 321353 评测结果 AAAAAAAAAA
题目名称 eins 最终得分 100
用户昵称 Gravatarsxysxy 是否通过 通过
代码语言 C++ 运行时间 3.749 s
提交时间 2016-10-13 18:00:09 内存使用 0.23 MiB
显示代码纯文本
  1. #include <cstring>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstdarg>
  5. #include <cctype>
  6. using namespace std;
  7.  
  8. long long MOD;
  9. struct matrix
  10. {
  11. long long x[2][2];
  12. matrix mul(matrix &m)
  13. {
  14. matrix r;
  15. for(int i = 0; i < 2; i++)for(int j = 0; j < 2; j++)
  16. {
  17. r.x[i][j] = 0;
  18. for(int k = 0; k < 2; k++)r.x[i][j]+=x[i][k]*m.x[k][j]%MOD;
  19. r.x[i][j] %= MOD;
  20. }
  21. return r;
  22. }
  23. long long pow(long long exp)
  24. {
  25. matrix a, ans;
  26. a.x[0][0] = a.x[0][1] = a.x[1][0] = 1;
  27. a.x[1][1] = 0;
  28. ans.x[0][0] = ans.x[1][1] = 1;
  29. ans.x[1][0] = ans.x[0][1] = 0;
  30. while(exp)
  31. {
  32. if(exp & 1)
  33. {
  34. ans = ans.mul(a);
  35. }
  36. a = a.mul(a);
  37. exp >>= 1;
  38. }
  39. return ans.x[0][1];
  40. }
  41. };
  42.  
  43.  
  44. int main()
  45. {
  46. freopen("eins.in", "r", stdin);
  47. freopen("eins.out", "w", stdout);
  48. int T;
  49. scanf("%d", &T);
  50. matrix t;
  51. while(T--)
  52. {
  53. long long n;
  54. scanf("%lld %lld", &n, &MOD);
  55. printf("%lld\n", t.pow(n));
  56. }
  57. return 0;
  58. }