【题解】CF474D:Flowers

原题传送门
dp水题,切掉的那种
d p i = d p i − 1 + d p i − k dp_i=dp_{i-1}+dp_{i-k} dpi=dpi1+dpik
再用前缀和统计一下 s u m i = ∑ j = 1 i d p j sum_i=\sum_{j=1}^{i}dp_j sumi=j=1idpj
对于每个询问 [ l , r ] [l,r] [l,r]答案便是 s u m r − s u m l − 1 sum_r-sum_{l-1} sumrsuml1
Code:

#include 
#define maxn 100010
#define LL long long
using namespace std;
const LL qy = 1000000007;
int n, k;
LL dp[maxn], sum[maxn];

inline int read(){
	int s = 0, w = 1;
	char c = getchar();
	for (; !isdigit(c); c = getchar()) if (c == '-') w = -1;
	for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48);
	return s * w;
}

int main(){
	n = read(), k = read();
	dp[0] = 1;
	for (int i = 1; i <= maxn - 1; ++i){
		dp[i] = dp[i - 1];
		if (i >= k) if ((dp[i] += dp[i - k]) >= qy)  dp[i] -= qy;
	}
	for (int i = 1; i <= maxn - 1; ++i) sum[i] = (sum[i - 1] + dp[i]) % qy;
	while (n--){
		int l = read(), r = read();
		printf("%lld\n", (sum[r] - sum[l - 1] + qy) % qy);
	}
	return 0;
}

你可能感兴趣的:(题解,codeforces,DP)