题目描述
有一个长度为n的序列a,已知a[1]=a[n]=1,且对于2 <= x <= n,a[x] / a[x-1]是以下三个数字之一 [ 1,-2,0.5 ],问有多少种不同的序列满足题意。
两个序列不同当且仅当它们有至少一个位置上的数字不同,序列a可以为任何实数。
输入描述:
一个整数 表示n (1<= n <= 1e3)
输出描述:
一个整数 表示答案模109+7
思路:一开始觉得答案会有规律,或者出现的数值会有规律,其实直接整体来看,a[x] = a[x-1]*[1, -2, 0.5],那么等于选出n-1个数值相乘,最后答案要是1,所以-2就必须有偶数个,同理0.5的个数要等于-2.顺序无关.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1e3 + 5;
const int mod = 1e9 + 7;
long long c[maxn][maxn];
int main()
{
for(int i = 0; i < maxn; i++)
{
c[i][0] = 1;
c[i][i] = 1;
for(int j = 1; j < i; j++)
c[i][j] = (c[i-1][j] + c[i-1][j-1]) % mod;
}
int n;
while(~scanf("%d", &n))
{
n--;
long long ans = 0;
for(int i = 0; i*2 <= n; i += 2)
{
ans = (ans%mod + (c[n][i]*c[n-i][i])%mod)%mod;
}
printf("%lld\n", ans);
}
return 0;
}