牛客网暑期ACM多校训练营(第二场) A run(dp)

题目链接:http://henuly.top/?p=591

题目:

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.

Input:

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)

Output:

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

Sample Input:

3 3
3 3
1 4
1 5

Sample Output:

2
7
11

题目链接

White Cloud一秒内可以走路一米或者跑 k 米,但是不能连续奔跑两秒及以上,对每次询问 l r 输出White Cloud从 0 点到达 [ l , r ] 内所有点的不同抵达方式种类数之和。

先对从 0 出发到最大值进行打表, d p [ i ] [ 0 ] 代表最后是走路抵达 i 点的种类数, d p [ i ] [ 1 ] 代表最后是跑步抵达i点的种类数。 d p [ i ] [ 0 ] 等于最后是走路抵达 i 1 的种类数与最后是跑步抵达 i 1 的种类数之和, d p [ i ] [ 1 ] 等于最后是走路抵达 i k 点的种类数(不能加最后是跑步抵达 i k 的种类数,因为不能连续奔跑)。之后用一个数组 a n s [ i ] 记录前 i 个点所有抵达方式只和,每次对询问区间取 ( a n s [ r ] a n s [ l 1 ] ) 即为最终结果。

AC代码:

#pragma comment(linker, "/STACK:102400000,102400000")
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define mp make_pair
#define lowbit(x) (x&(-x))
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<double,double> PDD;
const int INF = 0x3f3f3f3f;
const int maxn = 1e5+5;
const int mod = 1e9+7;
const double eps = 1e-8;
const double pi = asin(1.0)*2;
const double e = 2.718281828459;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}

int main(int argc, char *argv[]) {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    int q, k;
    read(q); read(k);
    ll dp[maxn][2];
    mem(dp, 0);
    dp[0][0] = 1;
    ll ans[maxn];
    mem(ans, 0);
    for (int i = 1; i < maxn; ++i) {
        dp[i][0] = dp[i - 1][0] % mod + dp[i - 1][1] % mod;
        if (i - k >= 0) {
            dp[i][1] = dp[i - k][0] % mod;
        }
        ans[i] = ans[i - 1] + dp[i][0] + dp[i][1];
    }
    for (int i = 1, l, r; i <= q; ++i) {
        read(l); read(r);
        printf("%lld\n", (ans[r] - ans[l - 1]) % mod);
    }
#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    system("gedit out.txt");
#endif
    return 0;
}

猜你喜欢

转载自blog.csdn.net/tony5t4rk/article/details/81149513