码迷,mamicode.com
首页 > 其他好文 > 详细

hiho一下158(hihocoder 1318)

时间:2017-07-16 14:59:28      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:end   sync   vector   [1]   get   com   fine   algorithm   ios   

非法二进制数

题意:求n位的二进制数中包含11的有多少个,并对1e9+7取模

思路:简单的状态压缩dp,dp[i][0]表示i位最末位为0的个数,dp[i][1]表示i位最末位为1的个数(这里指的是不包含11的),dp[i][2]表示答案,递推式见代码

AC代码:

#include "iostream"
#include "string.h"
#include "stack"
#include "queue"
#include "string"
#include "vector"
#include "set"
#include "map"
#include "algorithm"
#include "stdio.h"
#include "math.h"
#define ll long long
#define bug(x) cout<<x<<" "<<"UUUUU"<<endl;
#define mem(a) memset(a,0,sizeof(a))
#define mp(x,y) make_pair(x,y)
using namespace std;
const long long INF = 1e18+1LL;
const int inf = 2e9+1e8;
const int N=1e5+100;
const ll mod=1e9+7;

ll dp[105][5];
int n;
int main(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n;
    dp[1][0]=1,dp[1][1]=1,dp[1][2]=0;
    for(int i=2; i<=n; ++i){
        dp[i][0]=dp[i-1][0]+dp[i-1][1];
        dp[i][0]%=mod;
        dp[i][1]=dp[i-1][0];
        dp[i][1]%=mod;
        dp[i][2]=2*dp[i-1][2]+dp[i-1][1];
        dp[i][2]%=mod;
    }
    cout<<(dp[n][2]+mod)%mod<<endl;
    return 0;
}

 

hiho一下158(hihocoder 1318)

标签:end   sync   vector   [1]   get   com   fine   algorithm   ios   

原文地址:http://www.cnblogs.com/max88888888/p/7190426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!