HDU 3485 count 101

Source: Internet
Author: User
Count 1, 101

Time Limit: 2000/1000 MS (Java/others) memory limit: 65536/32768 K (Java/Others)
Total submission (s): 1114 accepted submission (s): 568


Problem descriptionyou 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.
Cocould you tell how would your chains will Yaoyao have at most?

 

Inputthere 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 shoshould not be processed as a case.

 

Outputfor 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 shocould be print after module 9997.

 

Sample Input34-1

 

Sample output712 HintWe can see when the length equals to 4. We can have those chains :,

 

Source2010 ACM-ICPC multi-university training Contest (5) -- host by bjtu

 

Recommendzhengfeng is a bit similar to the digital DP, but it is not set to the digital DP [I] [J] [k] to indicate that the last two digits of the I string are I, j does not include 101. So the DP equation has the following:

DP [I] [0] [0] + = DP [I-1] [0] [1] + dp [I-1] [0] [0]
DP [I] [0] [1] + = DP [I-1] [1] [0] + dp [I-1] [1] [1]
DP [I] [1] [0] + = DP [I-1] [0] [0]
DP [I] [1] [1] + = DP [I-1] [1] [0] + dp [I-1] [1] [1]

Finally, retrieve the total number of ans.

#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<stdlib.h>#include<algorithm>using namespace std;const int mod=9997;const int MAXN=10000+5;int dp[MAXN][2][2],n,ans[MAXN];void init(){    memset(dp,0,sizeof(dp));    memset(ans,0,sizeof(ans));    ans[0]=0;ans[1]=2;ans[2]=4;    dp[2][0][0]=dp[2][0][1]=dp[2][1][0]=dp[2][1][1]=1;    dp[1][1][0]=dp[1][0][0]=1;    for(int i=3;i<MAXN;i++)    {        dp[i][0][0]=(dp[i][0][0]+(dp[i-1][0][1]+dp[i-1][0][0])%mod)%mod;        dp[i][0][1]=(dp[i][0][1]+(dp[i-1][1][0]+dp[i-1][1][1])%mod)%mod;        dp[i][1][0]=(dp[i][1][0]+(dp[i-1][0][0])%mod)%mod;        dp[i][1][1]=(dp[i][1][1]+(dp[i-1][1][0]+dp[i-1][1][1])%mod)%mod;        ans[i]=(dp[i][0][0]+dp[i][0][1]+dp[i][1][0]+dp[i][1][1])%mod;    }}int main(){    init();    while(scanf("%d",&n)&&n!=-1)    {        printf("%d\n",ans[n]);    }    return 0;}
View code

 

HDU 3485 count 101

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.