3002. 泰波拿契数列的前74项

Deuchie

按照题意即可。

#include "bits/stdc++.h"
using namespace std;

uint64_t tribonacci[74]{0, 1, 1}; // Signature

int main() {
    for (int i = 3; i < 74; ++i)
        tribonacci[i] = tribonacci[i - 1] + tribonacci[i - 2] + tribonacci[i - 3];
    int t;
    cin >> t;
    for (int i = 0; i < t; ++i) {
        int n;
        cin >> n;
        cout << "case #" << i << ":\n" << tribonacci[n] << '\n';
    }
}

还可以用矩阵快速幂优化哦,感兴趣可以去看看。

Li Dao

没啥好说的,记得开long long

include

using namespace std;

define LL long long

int T,n;
LL a[80];
void solve()
{
cin>>n;
cout<<a[n]<<endl;
}
void init()
{
a[0]=0;a[1]=a[2]=1;
for(int i=3;i<=74;i++) a[i]=a[i-1]+a[i-2]+a[i-3];
return;
}
int main()
{
cin>>T;
init();
for(int step=0;step<T;step++)
{
printf(“case #%d:\n”,step);
solve();
}
return 0;
}

lnu_cxn

为啥如果是int后面的值就不变了,而不是负数

using namespace std;

long long s[74] = { -1 };

long long fb(int n) {
if (s[n] > 0)
return s[n];
if (n <= 1)
return s[n] = n;
else if (n == 2)
return s[n] = 1;
return s[n] = fb(n - 1) + fb(n - 2) + fb(n - 3);
}

int main() {
int t, count = 0;
cin >> t;
fb(73);
while (t–) {
int n;
cin >> n;
cout << “case #” << count++ << “:\n”;
cout << s[n] << endl;
}
return 0;
}

aiden
#include <iostream>
using namespace std;

int main()
{
    unsigned long long tribonacci[74] = { 0,1,1 };
    for (auto i = 3; i < 74; ++i)
        tribonacci[i] = tribonacci[i - 1] + tribonacci[i - 2] + tribonacci[i - 3];
    int t;
    cin >> t;
    for (auto z = 0; z < t; ++z)
    {
        int n;
        cin >> n;
        cout << "case #" << z << ":" << endl;
        cout << tribonacci[n] << endl;
    }
    return 0;
}
CoolCarrot

测试数据坑爹!
题目说好n<74的我就开了73个数组结果WA,改成100就过了[em:08]
代码如下:

include

include

int main()
{
int cas,i,j,temp,carry,n;
scanf(“%d”,&cas);
static int t[100][20]={{0},{1},{1}};
for(i=3;i<100;i++)
{
carry=0;
for(j=0;j<20;j++)
{
temp=t[i-1][j]+t[i-2][j]+t[i-3][j]+carry;
t[i][j]=temp%10;
carry=temp/10;
}
}
for(i=0;i=0;j–)
{
if(t[n][j]) flag=1;
if(flag)
printf(“%d”,t[n][j]);
}
printf(“\n”);
}
return 0;
}

10175101249

你再多开一个就对了……

你当前正在回复 博客/题目
存在问题!