Count 101

You know YaoYao is fond of his chains. He has a lot of chains and each chain has n diamonds on it. There are two kinds of diamonds, labeled 0 and 1. We can write down the label of diamonds on a chain. So each chain can be written as a sequence consisting of 0 and 1.
We know that chains are different with each other. And their length is exactly n. And what’s more, each chain sequence doesn’t contain “101” as a substring. 
Could you tell how many chains will YaoYao have at most?

Input

There will be multiple test cases in a test data. For each test case, there is only one number n(n<10000). The end of the input is indicated by a -1, which should not be processed as a case.

Output

For each test case, only one line with a number indicating the total number of chains YaoYao can have at most of length n. The answer should be print after module 9997.

Sample Input

3
4
-1

Sample Output

7
12

Hint

We can see when the length equals to 4. We can have those chains:
0000,0001,0010,0011
0100,0110,0111,1000
1001,1100,1110,1111

n个珠子构成一条项链,珠子只有0 1 ,组成项链后就是一条

01串,其中不能有101这个串,问这样的01串有多少个

刚看到这个题就想到去找到是否有通项公式,结果找了很久
交上去是错的。后来又想到前后项的关系,但就算是这样
也没想到是dp来做,而只是再数字间找规律。

设f[i-1]表示i-1个珠子所得到的项链的数目

令 dp[i] 表示长度为 i 的串满足要求的串的个数。

很容易想到 dp[i] = 2*dp[i-1] - { dp[i-1] 中末尾两位是"10"的串的个数 }。

而 { } 中的内容又可以表示成 dp[i-1] 中末位是“0”的串的个数 - dp[i-2] 中末位是“0”的串的个数。

又因为 dp[j] 中末位是“0”的个数等于 dp[j-1] 。

看来dp的意识还是不够,看到都不知道用啊

 

 

 

#include
#include
using namespace std;
int seg[200000];
int main()
{
    memset(seg,0,sizeof(seg));
    seg[3]=7;seg[1]=2;seg[2]=4;seg[0]=1;
    seg[4]=12;
    for(int i=5; i<10000; i++)
    {
        //seg[i]=(seg[i-1]*2)%9997;
        //seg[i]=((seg[i-1]%9997)*2)%9997;
        seg[i]=(2*seg[i-1]-(seg[i-2]-seg[i-3]))%9997;

    }
    int a;
    while(cin>>a&&a!=-1){
        printf("%d\n",seg[a]);
    }

}

 

你可能感兴趣的:(动态规划)