2019/10/8今日头条笔试

2019/10/8 今日头条笔试第五题

在这里插入图片描述

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

/*
m个台阶,一次可爬a~b个台阶
部分台阶损坏 坏的台阶出现在n个给定的位置
有多少种登山方案
 */

int helper(int m,int a,int b,unordered_map<int,int>& bad,vector<long long>& dp,int x) {
  if(bad[x]) {
    dp[x] = -1;
    return 0;
  }
  if(x>m) {
    return 1;
  }

  for(int i=a;i<=b;++i) {
    if(dp[x+i]>0)
      dp[x] += dp[x+i];
    else if(dp[x+1]==0)
      dp[x] += helper(m,a,b,bad,dp,x+i);
  }
  return dp[x];
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

   int m,a,b,n;
   cin >> m >> a >> b >> n;
   unordered_map<int,int> bad;
   for(int i=0;i<n;++i) {
    int temp;
    cin >> temp;
    bad[temp] = 1;
   }

   vector<long long> dp(m+1,0);
   int res = 0;
   for(int i=a;i<=b;++i) {
      res += helper(m,a,b,bad,dp,i)%(1000000007);
   }

   cout << res%(1000000007) << endl;

   return 0;
}

猜你喜欢

转载自blog.csdn.net/zhc_24/article/details/82976964