このシーケンスは、a(n+2) = 2 a(n+1) + 2 a(n) を満たします。
また、a(n)=[(1+sqrt(3))^(n+2)-(1-sqrt(3))^(n+2)]/(4sqrt(3))。
私は C++ を使用しています n は 1 から 10^9 まで変化します。モジュロ (10^9)+7 の答えが必要ですが、ここでの速度は非常に重要です。
formula1 を使用した私のコードは、10^7 を超える数値では遅い
#include <iostream>
#define big unsigned long long int
#include<stdlib.h>
int ans[100000001]={0};
big m =1000000007;
using namespace std;
int main()
{
//cout << "Hello world!" << endl;
big t,n;
cin>>t;
big a,b,c;
a=1;
b=3;
c=8;
ans[0]=0;
ans[1]=1;
ans[2]=3;
ans[3]=8;
for(big i=3;i<=100000000;i++)
{
ans[i]=(((((ans[i-2])+(ans[i-1])))%m)<<1)%m;
}
// while(t--)
// {
// int f=0;
// cin>>n;
// if(n==1){
// cout<<1<<endl;f++;}
// if(n==2){
// cout<<3<<endl;
// f++;
// }
// if(!f){
// a=1;
// b=3;
// c=8;
// for(big i=3;i<=n;i++)
// {
// c=(((((a)+(b
// )))%m)<<1)%m;
// a=b%m ;
// b=c%m;
// }
// cout<<ans[n]<<endl;
// }
// }
while(t--)
{
cin>>n;
if(n<=100000000)
cout<<ans[n]<<endl;
else
cout<<rand()%m;
}
return 0;
}
より速い方法が欲しい。2 番目の式を使用して n 番目の項を計算するにはどうすればよいですか? このシーケンスの生成を高速化するための提案はありますか?
助けてください