Description
给定n(N<=100),编程计算有多少个不同的n轮状病毒。
Input
第一行有1个正整数n。
Output
将编程计算出的不同的n轮状病毒数输出
Sample Input
3
Sample Output
16
两种做法:
1.基尔霍夫矩阵+高斯消元暴搓
2.根据基尔霍夫矩阵推出推出递推公式推出f[i]=(f[i-2]*3-f[i-1]+2)。(解释见:http://vfleaking.blog.163.com/blog/static/17480763420119685112649/)
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std; int n;
struct node
{
int a[],l;
node()
{
memset(a,,sizeof(a));
l = ;
}
friend inline node operator *(int x,node &y)
{
node ret; ret.l = y.l+;
for (int i = ;i <= y.l;++i)
{
ret.a[i] += y.a[i]*x;
ret.a[i+] += ret.a[i]/;
ret.a[i] %= ;
}
if (ret.a[ret.l] == ) ret.l--;
return ret;
}
friend inline node operator -(node x,node y)
{
node z; z.l = max(x.l,y.l);
for (int i = ;i <= z.l;++i)
{
z.a[i] = x.a[i]-y.a[i];
while (z.a[i] < )
z.a[i] += ,x.a[i+]--;
}
while (z.l > &&z.a[z.l] == )z.l--;
return z;
}
friend inline node operator +(node &x,int y)
{
node ret = x;
ret.a[] += y;
for (int i = ;i <= ret.l;++i)
{
if (ret.a[i] >= )
ret.a[i]-=,ret.a[i+]++;
else break;
}
if (ret.a[ret.l+]) ret.l++;
return ret;
}
inline void print()
{
for (int i = l;i >= ;--i)
printf("%d",this->a[i]);
}
}f[]; int main()
{
freopen("1002.in","r",stdin);
freopen("1002.out","w",stdout);
scanf("%d ",&n);
f[] = f[]+; f[] = f[] + ;
for (int i = ;i <= n;++i)
{
f[i] = *f[i-];
f[i] = f[i]-f[i-];
f[i] = f[i]+;
}
f[n].print();
fclose(stdin); fclose(stdout);
return ;
}