列表

详情


NC16730. run

描述

White Cloud is exercising in the playground.
White Cloud can walk 1 meters or run k meters per second.
Since White Cloud is tired,it can't run for two or more continuous seconds.
White Cloud will move L to R meters. It wants to know how many different ways there are to achieve its goal.
Two ways are different if and only if they move different meters or spend different seconds or in one second, one of them walks and the other runs.

输入描述

The first line of input contains 2 integers Q and k.Q is the number of queries.(Q<=100000,2<=k<=100000)
For the next Q lines,each line contains two integers L and R.(1<=L<=R<=100000)

输出描述

For each query,print a line which contains an integer,denoting the answer of the query modulo 1000000007.

示例1

输入:

3 3
3 3
1 4
1 5

输出:

2
7
11

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++ 11.0.1) 解法, 执行用时: 284ms, 内存消耗: 2928K, 提交时间: 2022-12-22 22:25:57

#include<iostream>
using namespace std;
int main()
{
	long long b[100001],sum[100001];
	int q,k,x,y;
	cin>>q>>k;
	b[0]=1,sum[0]=0;
	for(int i=1;i<k;i++)
	b[i]=1;
	b[k]=2;
	for(int i=k+1;i<100001;i++)
	b[i]=b[i-1]+b[i-k-1];
	for(int i=1;i<100001;i++)
	sum[i]=sum[i-1]+b[i];
	for(int i=0;i<q;i++)
	{
		cin>>x>>y;
		cout<<(sum[y]-sum[x-1])%1000000007<<endl;
	}
	return 0;
}

Python3(3.5.2) 解法, 执行用时: 639ms, 内存消耗: 8588K, 提交时间: 2018-07-21 12:41:01

dp = [0]*100005
n, k = map(int, input().split())
for i in range(k+1):
    dp[i] = 1
dp[k] = 2
for i in range(k+1, 100005):
    dp[i] = (dp[i-1]+dp[i-k-1]) % 1000000007
for i in range(1, 100005):
    dp[i] += dp[i-1]
for _ in range(n):
    ans = 0
    L, R = map(int, input().split())
    print((dp[R]-dp[L-1]) % 1000000007)

上一题