首页 > 代码库 > hiho一下158(hihocoder 1318)

hiho一下158(hihocoder 1318)

非法二进制数

题意:求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)