随笔分类 -  递推

摘要:想一想知道n-1位的结果,如何得到n位的结果呢?假如我们知道n-1的后两位再枚举第n位的数字,不就得到n位的结果了吗?我们用res[n][i][j]表示n位最后两位是i和j;Res[i][j][k]=∑res[i-1][l][j];(其中100*l+10*j+k 是素数)我们还可以优化一下,因为最后一位只有可能是1、3、7、9。所以可以将k和j所在的循环的长度改为4。#include<iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;int r 阅读全文
posted @ 2012-04-30 11:30 书山有路,学海无涯 阅读(183) 评论(0) 推荐(0) 编辑
摘要:StaircasesTime Limit: 1.0 second Memory Limit: 16 MBOne curious child has a set of N little bricks (5 ≤ N ≤ 500). From these bricks he builds different staircases. Staircase consists of steps of different sizes in a strictly descending order. It is not allowed for staircase to have steps equal sizes 阅读全文
posted @ 2012-04-05 15:36 书山有路,学海无涯 阅读(176) 评论(0) 推荐(0) 编辑
摘要:K-based NumbersTime Limit: 1.0 secondMemory Limit: 16 MBLet’s consider K-based numbers, containing exactly N digits. We define a number to be valid if its K-based notation doesn’t contain two successive zeros. For example:1010230 is a valid 7-digit number;1000198 is not a valid number;0001235 is not 阅读全文
posted @ 2012-04-05 09:32 书山有路,学海无涯 阅读(420) 评论(1) 推荐(0) 编辑
摘要:令a[i]表示i不排在最后的总数,b[i]表示排在最后的总数,则递推公式如下:a[i]=a[i-1]+b[i-2];b[i]=b[i]+b[i-3];#include<iostream>#include<cstdio>#include<cstring>using namespace std;int a[56],b[56];int main(){ int i,n; a[1]=0; b[1]=1; a[2]=0; b[2]=1; a[3]=1; b[3]=1; for(i=4;i<=55;i++) { a[i]=a[i-1]+b[i-2]; ... 阅读全文
posted @ 2012-03-27 22:03 书山有路,学海无涯 阅读(169) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示