记录编号 283064 评测结果 AAAAAAAAAA
题目名称 斐波那契平方和 最终得分 100
用户昵称 Gravatar洛克索耶夫 是否通过 通过
代码语言 C++ 运行时间 0.029 s
提交时间 2016-07-14 07:54:50 内存使用 0.32 MiB
显示代码纯文本
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. unsigned long long n, p;
  4. struct Matrix{
  5. unsigned long long n, m, a[5][5];//琛 鍒
  6. void clear()
  7. {
  8. n = m = 0;
  9. memset(a, 0, sizeof(a));
  10. }
  11. Matrix operator * (const Matrix& x) const {//鐭╅樀涔樻硶
  12. Matrix tmp;
  13. tmp.clear();
  14. tmp.n = n;
  15. tmp.m = x.m;
  16. for(int i = 1; i <= tmp.n; i++){
  17. for(int j = 1; j <= tmp.m; j++){
  18. for(int k = 1; k <= m; k++){
  19. tmp.a[i][j] += a[i][k] * x.a[k][j];
  20. tmp.a[i][j] %= 1000000007;
  21. }
  22. }
  23. }
  24. return tmp;
  25. }
  26. Matrix operator + (const Matrix& x) const {//鐭╅樀鍔犳硶
  27. Matrix tmp;
  28. tmp.clear();
  29. for(int i = 0; i <= n; i++)
  30. for(int j = 0; j <= m; j++)
  31. tmp.a[i][j] = a[i][j] + x.a[i][j];
  32. return tmp;
  33. }
  34. };
  35. int main()
  36. {
  37. freopen("fibsqr.in", "r", stdin);freopen("fibsqr.out", "w", stdout);
  38. scanf("%llu", &n);
  39. if(n == 0){
  40. puts("0");
  41. return 0;
  42. }
  43. Matrix first, x;
  44. first.clear(); x.clear();
  45. first.n = 1; first.m = 2;
  46. first.a[1][1] = 1, first.a[1][2] = 0;
  47. x.n = 2; x.m = 2;
  48. x.a[1][1] = 1; x.a[1][2] = 1; x.a[2][1] = 1; x.a[2][2] = 0;
  49. for(unsigned long long i = 1; i <= n; i <<= 1, x = x * x){
  50. if(n & i) first = first * x;
  51. }
  52. printf("%llu\n", first.a[1][1]%1000000007 * first.a[1][2]%1000000007);
  53. //printf("%lf", (double)clock()/CLOCKS_PER_SEC);
  54. //system("pause");
  55. fclose(stdin);
  56. fclose(stdout);
  57. return 0;
  58. }