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

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
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;
}