小工具知识网

当前位置: 首页 > 行业知识 > 洛谷题单算法1-4递推与递归

行业知识

洛谷题单算法1-4递推与递归

2026-11-05 04:06:43 来源:互联网转载

P1255 数楼梯

题���本质是 Fibonacci 数列

走楼梯,要么一次走一格,要么一次走两格

那每次的走法 = 上一格的方案数 + 上上格的方案数

这就有了递推公式:

a[i] = a[i - 1] + a[i - 2]

这题要注意数据,开 long long 也只能得60points :

#includeusing namespace std;long long a[10000];int main(){ int n;cin >> n;a[1] = 1; a[2] = 2;for (int i = 3; i  n;if (n == 2 || n == 1) {cout = 0; i--)cout > bx >> by >> cx >> cy;if (cx >= 0 && cy >= 0)a[cx][cy] = 0;if (cx + 2 >= 0 && cy + 1 >= 0)a[cx + 2][cy + 1] = 0;if (cx + 1 >= 0 && cy + 2 >= 0)a[cx + 1][cy + 2] = 0;if (cx - 1 >= 0 && cy + 2 >= 0)a[cx - 1][cy + 2] = 0;if (cx - 2 >= 0 && cy + 1 >= 0)a[cx - 2][cy + 1] = 0;if (cx - 2 >= 0 && cy - 1 >= 0)a[cx - 2][cy - 1] = 0;if (cx - 1 >= 0 && cy - 2 >= 0)a[cx - 1][cy - 2] = 0;if (cx + 1 >= 0 && cy - 2 >= 0)a[cx + 1][cy - 2] = 0;if (cx + 2 >= 0 && cy - 1 >= 0)a[cx + 2][cy - 1] = 0;int x = 1;while (a[x][0] == 1)x++;for (x; x  n;for (int i = 1; i  n;a[1] = 1, a[2] = 2, a[3] = 2;if (n >= 4) {for (int i = 4; i  n;int x = n - m + 1;a[1] = 0, a[2] = 1, a[3] = 2;for (int i = 4; i  m >> n;int x = n - m + 1;a[0] = 1, b[0] = 1;for (int i = 3; i = 0; i--)if (c[i] != 0)break;for (i; i >= 0; i--)cout > n >> m;for (int i = 1; i > a[i];for (int i = 1; i > k;for (int i = 1; i > a[i];dfs(1, 0, 0);cout > n;len = n + 1 + cnt;n--;while (len > cnt) {chx[cnt] = ch[cnt - 1];for (int i = cnt + 1; i = 1;

最后输出即可

注意:>>n 是除以(2的n次方), n;long long flag = n / cnt + 1;while (max cnt) {if (x == 1)n--;if (x > 1)n = n - (max >> 1) - 1;max >>= 1; x = n - (max >> 1);}cout

上一篇:轻松添加网站模板后台的详细步骤教程

下一篇:警惕风险!Bet下载违法,切勿尝试——一网打尽违法赌博行为