-
Notifications
You must be signed in to change notification settings - Fork 46
/
PTIT_SSAM019I(1).cpp
51 lines (48 loc) · 1.07 KB
/
PTIT_SSAM019I(1).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
using namespace std;
/*Ham tinh tung so trong Tribonacci*/
int Tribonacci(int n) // n la so thu tu trong day Tribonacci cua so
{
int a1 = 1, a2 = 2, a3 = 3; // 3 so dau mac dinh cua day Tribonacci
if (n ==1 )
return 1;
if (n ==2 )
return 2;
if (n ==3)
return 3;
int i = 4, sum;
while(i<=n)
{
sum = a1 + a2 + a3;
a1 = a2;
a2 = a3;
a3 = sum;
i++;
}
return sum;
}
/*Tinh Fn*/
int Fn(int n) // Tong cua n so Tribonacci dau tien
{
if (n ==1)
return Tribonacci(1);
if (n ==2)
return Tribonacci(1)+Tribonacci(2);
if (n==3)
return Tribonacci(1)+Tribonacci(2)+Tribonacci(3);
int SUM = (3*Tribonacci(n)+2*Tribonacci(n-1)+Tribonacci(n-2)+1-3)/2;
return SUM;
}
int main()
{
int input; // so test
cin >> input;
int arr[input];
for (int i=0; i<input; i++)
{
cin >> arr[i];
}
for (int i=0; i<input; i++)
cout << Fn(arr[i]) << endl;
return 0;
}