3.1.1 超级楼梯
#include<stdio.h>
using namespace std;
long long a[100];
int main()
{
int n,m;
a[1] = 1;
a[2] = 1;
for (int i=3;i<=40;i++)
a[i]=a[i-1]+a[i-2];
scanf("%d",&n);
while (n--)
{
scanf("%d",&m);
printf("%lld\n",a[m]);
}
return 0;
}
3.1.2 一只小蜜蜂...
#include<stdio.h>
using namespace std;
long long a[100];
int main()
{
int n,x,y;
a[0] = 1;
a[1] = 1;
for (int i=2;i<=50;i++)
a[i]=a[i-1]+a[i-2];
scanf("%d",&n);
while (n--)
{
scanf("%d%d",&x,&y);
printf("%I64d\n",a[y-x]);
}
return 0;
}
3.1.3 骨牌铺方格
#include<stdio.h>
using namespace std;
long long a[100];
int main()
{
int n,m;
a[0] = 1;
a[1] = 1;
for (int i=2;i<=50;i++)
a[i]=a[i-1]+a[i-2];
while (scanf("%d",&m)!=EOF)
printf("%I64d\n",a[m]);
return 0;
}
3.1.4 母牛的故事
#include<stdio.h>
using namespace std;
long long a[100];
int main()
{
a[1]=1;
a[2]=2;
a[3]=3;
a[4]=4;
for (int i=5;i<=55;i++)
a[i] = a[i-1] + a[i-3];
int n;
scanf("%d",&n);
while (n!=0)
{
printf("%I64d\n",a[n]);
scanf("%d",&n);
}
return 0;
}
3.1.5 折线分割平面
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
long long a[11000];
int i;
a[1]=2;
for(i=2;i<11000;i++)
{
a[i]=(2*(i-1)-1)*2+3+a[i-1];
}
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&i);
printf("%I64d\n",a[i]);
}
}
3.1.6 Tiling_easy version
#include<stdio.h>
using namespace std;
int main()
{
long long a[100];
a[1]=1; a[2]=3;
for (int i=3;i<=30;i++)
a[i]=a[i-1]+2*a[i-2];
int t,n;
scanf("%d",&t);
while (t--)
{
scanf("%d",&n);
printf("%I64d\n",a[n]);
}
return 0;
}
3.1.7 统计问题
#include<stdio.h>
using namespace std;
int main()
{
int a[100],b[100],c[100],ret[100];
a[1]=b[1]=c[1]=1;
ret[1]=3;
for (int i=2;i<=20;i++)
{
a[i] = a[i-1]+b[i-1]+c[i-1];
b[i] = a[i-1]+b[i-1];
c[i] = a[i-1]+c[i-1];
ret[i] = a[i]+b[i]+c[i];
}
int C,n;
scanf("%d",&C);
while(C--)
{
scanf("%d",&n);
printf("%d\n",ret[n]);
}
return 0;
}
3.1.8 Queuing
用到了矩阵加速的一题
#include <stdio.h>
using namespace std;
const int N = 4;
struct Mat
{
int matrix[N][N];
};
Mat mat, mt;
int n, m;
Mat mul(Mat a, Mat b)
{
int i, j, k;
Mat c;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
{
c.matrix[i][j] = 0;
for (k = 0; k < N; k++)
{
c.matrix[i][j] += a.matrix[i][k] * b.matrix[k][j];
c.matrix[i][j] %= m;
}
}
return c;
}
Mat solve(int m)
{
Mat mt;
if (m == 1)
return mat;
if (m & 1)
return mul(solve(m - 1), mat);
else
{
mt = solve(m / 2);
return mul(mt, mt);
}
}
void init()
{
int i, j;
for (i = 0; i < N; ++i)
{
mat.matrix[0][i] = 1;
}
mat.matrix[0][1] = 0;
for (i = 1; i < N; ++i)
{
for (j = 0; j < N; ++j)
{
if (i - j == 1)
mat.matrix[i][j] = 1;
else
mat.matrix[i][j] = 0;
}
}
}
int main()
{
Mat ans;
int f41[] ={ 9, 6, 4, 2 }, sum, f04[] ={ 0, 2, 4, 6, 9 };
while (scanf("%d%d",&n,&m) != EOF)
{
if (n < 5)
{
printf("%d\n", f04[n] % m);
}
else
{
init();
ans = solve(n - 4);
sum = 0;
for (int k = 0; k < N; k++)
{
sum += ans.matrix[0][k] * f41[k];
sum %= m;
}
printf("%d\n", sum);
}
}
return 0;
}