J. simple recursive

Single Point Test time:  1.0 seconds

Memory Limit:  512 MB

This is a C / C ++ code for calculating the recurrence sequence an.

const int MOD = (int)1e9 + 7;
int a(int n) {
    if(n < 3) return 1;
    return ((a(n - 1) << 1) % MOD + (a(n - 2) >> 1)) % MOD;
}

Please an output value of n items according to the number of columns in the code.

Entry

A first line integer T.
T lines after each line an integer representing n.
0 <T≤105, 0 <n≤106.

Export

Please each line of an output value of one.

Sample

input

3
1
2
3

output

1
1
2
直接递归一定会超时所以可直接转化为数学式子来处理

 

#include<bits/stdc++.h>
#include<algorithm>

using namespace std;
typedef long long ll;
const int MOD = (int)1e9 + 7;

ll b[1000005];

/*ll a(ll n) {
    if(n < 3) return 1;
    return 
		((a(n - 1)%MOD << 1) % MOD + (a(n - 2)%MOD >> 1)) % MOD;
}*/

/*ll a(ll n) {
    if(n < 3) return b[n];
    int res = ((a(n - 1)%MOD << 1) % MOD + (a(n - 2)%MOD >> 1)) % MOD;
	return b[n] = res;
}*/

int main()
{
	int t;
	scanf("%d",&t);
	b[1] = b[2] = 1;
	for (int i=3;i<=1000000;i++)
	{
		b[i] = (b[i-1] * 2 % MOD +b [i-2] / 2 % MOD)%MOD;
	}
	while (t--)
	{
		ll n;
		scanf("%lld",&n);
		printf("%lld\n",b[n]);
	}
	return 0;
}

 

Guess you like

Origin blog.csdn.net/qq_40912854/article/details/88959425