ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

牛客2018暑假多校训练营2

2021-10-02 17:01:02  阅读:167  来源: 互联网

标签:状态 different int 多校 牛客 meters 2018 White Cloud


比赛链接

牛客2018暑假多校训练营2

题目描述

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.

输入

3 3
3 3
1 4
1 5

输出

2
7
11

解题思路

dp

  • 状态表示:\(f[i][0/1]\) 表示行进了 \(i\) 米,且最后一步是走/跑的方案数
  • 状态计算:
    \(f[i][0]=f[i-1][0]+f[i-1][1]\)
    \(f[i][1]=f[i-k][0]\)

分析:无论是走还是跑,每秒都有一个最终状态,dp枚举的就是这个最终状态,但这个最终状态是走时,前一个状态可能是走,也可能是跑;最终状态是跑时,前一个状态只能是走~
用一个前缀和数组 \(s\) 维护 \(0\sim i\) 的所有方案数,求 \(l\sim r\) ,即 \(s[r]-s[l-1]\)

  • 时间复杂度:\(O(10^5)\)

代码

//dp
//状态表示:f[i][0/1]表示走了i米,最后是跑的(1)还是走的(0)
//状态计算:f[i][0]=f[i-1][0]+f[i-1][1]
//         f[i][1]=f[i-k][0]
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int f[100010][2],s[100010];
int q,k;
int main()
{
    scanf("%d%d",&q,&k);
    f[0][0]=1;
    for(int i=1;i<=100000;i++)
    {
        f[i][0]=(f[i][0]+f[i-1][0]+f[i-1][1])%mod;
        if(i>=k)f[i][1]=(f[i][1]+f[i-k][0])%mod;
    }
    for(int i=1;i<=100000;i++)
        s[i]=(s[i-1]+f[i][0]+f[i][1])%mod;
    while(q--)
    {
        int l,r;
        scanf("%d%d",&l,&r);
        printf("%d\n",(s[r]-s[l-1]+mod)%mod);
    }
    return 0;
}

标签:状态,different,int,多校,牛客,meters,2018,White,Cloud
来源: https://www.cnblogs.com/zyyun/p/15362196.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有