Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. void Input(int &a, int &n)
  5. {
  6. cout << "a = ";
  7. cin >> a;
  8. cout << "n = ";
  9. cin >> n;
  10. }
  11.  
  12. void Cau2a()
  13. {
  14. const int a = 2;
  15. cout << "a) Voi a = 2" << endl;
  16. int f1, f2, f3, f4;
  17. f1 = a;
  18. f2 = 18 * a * a + 10;
  19. f3 = 18* f2 + 10 * f1;
  20. f4 = 18 * f3 + 10 * f2;
  21. cout << "F3 = " << f3 << endl;
  22. cout << "F4 = " << f4 << endl;
  23. }
  24.  
  25. long long Cau2b(int a, int n)
  26. {
  27. if (n == 1)
  28. return a;
  29. if (n == 2)
  30. return (long long)18 * a * a + 10;
  31. else
  32. return 18 * Cau2b(a, n - 1) + 10 * Cau2b(a, n - 2);
  33. }
  34.  
  35. long long Cau2c(int a, int n)
  36. {
  37. long long f1 = a;
  38. long long f2 = (long long)18 * a * a + 10;
  39. int temp = 3;
  40. long long temp1;
  41. while (temp <= n)
  42. {
  43. temp1 = f1;
  44. f1 = f2;
  45. f2 = 18 * f2 + 10 * temp1;
  46. temp++;
  47. }
  48. return f2;
  49. }
  50.  
  51. long long Cau2c_Cach2(int a, int n)
  52. {
  53. long long F[100000];
  54. F[1] = a;
  55. F[2] = (long long)18 * a * a + 10;
  56. int temp = 3;
  57. while (temp <= n)
  58. {
  59. F[temp] = 18 * F[temp - 1] + 10 * F[temp - 2];
  60. temp++;
  61. }
  62. return F[n];
  63. }
  64.  
  65.  
  66. void Caud()
  67. {
  68. /*Phương pháp không dùng đệ qui(câu c) hiệu quả hơn phương phương pháp dùng đệ qui(câu b)
  69. bởi vì theo độ phức tạp thuật toán thì câu c có độ phức tạp là O(n) còn câu b là O(2 ^ n);*/
  70. }
  71.  
  72.  
  73.  
  74. int main()
  75. {
  76. int a, n;
  77. Cau2a();
  78. Input(a, n);
  79. cout << "b) Cau b: ";
  80. cout << Cau2b(a, n) << endl;
  81. cout << "c) Cau c: ";
  82. cout << Cau2c(a, n) << endl;
  83. cout << "c) Cau c(Cach 2): ";
  84. cout << Cau2c_Cach2(a, n) << endl;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement