1. 程式人生 > >hiho一下158(hihocoder 1318)

hiho一下158(hihocoder 1318)

map target 進制 hihocoder tor ring urn std ostream

非法二進制數

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