Problem:
Beautiful Sequence

Alice is spending his time on an independent study to apply to the Nationwide Mathematics Contest. This year’s theme is "Beautiful Sequence." As Alice is interested in the working of computers, she wants to create a beautiful sequence using only 0 and 1. She defines a "Beautiful" sequence of length $N$ that consists only of 0 and 1 if it includes $M$ successive array of 1s as its sub-sequence.

Using his skills in programming, Alice decided to calculate how many "Beautiful sequences" she can generate and compile a report on it.

Make a program to evaluate the possible number of "Beautiful sequences" given the sequence length $N$ and sub-sequence length $M$ that consists solely of 1. As the answer can be extremely large, divide it by $1,000,000,007 (= 10^9 + 7)$ and output the remainder.

Input

The input is given in the following format.

$N$ $M$

The input line provides the length of sequence $N$ ($1 \leq N \leq 10^5$) and the length $M$ ($1 \leq M \leq N$) of the array that solely consists of 1s.

Output

Output the number of Beautiful sequences in a line.

Sample Input 1

4 3

Sample Output 1

3

The sequences with length 4 that include 1s in successive array of length 3 are: 0111, 1110 and 1111.

Sample Input 2

4 2

Sample Output 2

8

The sequences with length 4 that include 1s in successive array of length 2 are: 0011, 0110, 0111, 1011, 1100, 1101, 1110 and 1111.

Solution in C++ (judge verdict: Accepted)

#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
ll dp0[100005]={0};
ll pow(ll x,ll y){
  if(y==0) return 1;
  ll ret=pow(x,y/2);
  ret*=ret;
  ret%=mod;
  if(y%2==1) ret*=x;
  return ret%mod;
}
const ll g2=pow(2,mod-2);
struct segtree{
private:
  vector<ll> node,lazy;
  ll siz;
public:
  segtree(int n){
    siz=1;
    while(siz<=n) siz*=2;
    node.resize(siz*2,0);
    lazy.resize(siz*2,0);
  }
  void eval(ll pos){
    node[pos]+=lazy[pos];
    node[pos]%=mod;
    if(pos<siz-1) {
      lazy[pos*2+1]+=lazy[pos]/2;
      lazy[pos*2+2]+=lazy[pos]/2;
    //  lazy[pos*2+1]%=mod;
    //  lazy[pos*2+2]%=mod;
    }
    lazy[pos]=0;
  }
  void add_q(ll a,ll b,ll x,ll l,ll r,ll pos){
    eval(pos);
    if(b<=l||r<=a) return;
    if(a<=l&&r<=b){
      lazy[pos]+=(x*(r-l));
      eval(pos);
      return;
    }
    ll mid=(l+r)/2;
    add_q(a,b,x,l,mid,pos*2+1);
    add_q(a,b,x,mid,r,pos*2+2);
    node[pos]=(node[pos*2+1]+node[pos*2+2])%mod;
  }
  ll sum_q(ll a,ll b,ll l,ll r,ll pos){
    if(b<=l||r<=a) return 0;
    eval(pos);
    if(a<=l&&r<=b){
      return node[pos]%mod;
    }
    ll mid=(l+r)/2;
    return (sum_q(a,b,l,mid,pos*2+1)+sum_q(a,b,mid,r,pos*2+2))%mod;
  }
  void add(ll a,ll b,ll x){
    add_q(max((ll)0,a),min(b,siz),x,0,siz,0);
  }
  ll sum(ll a,ll b){
    return sum_q(max((ll)0,a),min(b,siz),0,siz,0);
  }
  void debug(){
    cout<<"Node:"<<endl;
    ll t=2;
    for(int i=2;i<=siz*2;i++) {
      cout<<node[i-2]<<" ";
      if(i==t){
        t*=2;
        cout<<endl;
      }
    }
    cout<<"Lazy:"<<endl;
    t=2;
    for(int i=2;i<=siz*2;i++){
      cout<<lazy[i-2]<<" ";
      if(i==t){
        t*=2;
        cout<<endl;
      }
    }
  }
};
int main(){
  ll n,m;
  cin>>n>>m;
  segtree dp1=segtree(n+3);
  dp0[0]=1;
  for(int i=0;i<n;i++){
    dp1.add(i+1,min(n+1,i+m),dp0[i]);
    dp0[i+1]=dp0[i]+dp1.sum(i,i+1);
    dp0[i+1]%=mod;
  }
/*  for(int i=0;i<=n;i++) cout<<dp0[i]<<" ";
  cout<<endl;
  for(int i=0;i<=n;i++) cout<<dp1.sum(i,i+1)<<" ";
  cout<<endl;*/
  ll ans=pow(2,n);
  //cout<<ans<<endl;
  ans-=(dp0[n]+dp1.sum(n,n+1));
  if(ans<0){
    ll t=(0-ans)/mod;
    ans+=(t+1)*mod;
  }
  cout<<ans%mod<<endl;
}