1. 程式人生 > >牛客網多校練習賽2 A run (簡單dp+取模細節)

牛客網多校練習賽2 A run (簡單dp+取模細節)

題目描述

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
#include<iostream>
#include<algorithm>
#include<string>
#include<map>//int dx[4]={0,0,-1,1};int dy[4]={-1,1,0,0};
#include<set>//int gcd(int a,int b){return b?gcd(b,a%b):a;}
#include<queue>
#include<cmath>
#include<stack>
#include<string.h>
#include<stdlib.h>
#include<cstdio>
#define ll long long
#define maxn 500005
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000") ///在c++中是防止暴棧用的
const int mod=((1e9)+7);
ll n,k,l,r;
ll dp[maxn][2];
ll sum[maxn];
/*
題目大意:一個人每秒可以走1一米,
也可選擇跑k米,但跑完後必須走不能連續跑,
給定l和r,問有多少中跑步方案跑l到r的距離。

首先方案數肯定是滿足字首和性質的,即
f(l,r)=sum(r)-sum(l-1);
那麼dp的思路就很清晰了,
dp[i][0]表示跑到i米最後一步是走的,
dp[i][1]表示跑到i米最後一次是跑的,
遞推性質很簡單,看程式碼即可,
然後計算個字首和即可。

*/
int main()
{
    scanf("%lld%lld",&n,&k);
    sum[0]=0;

    for(int i=1;i<k;i++)
    {
        dp[i][0]=1;
        sum[i]=sum[i-1]+1;
    }
    sum[k]=sum[k-1]+2;
    dp[k][1]=dp[k][0]=1;
    for(int i=k+1;i<maxn;i++)
    {
        dp[i][0]=(dp[i-1][0]+dp[i-1][1])%mod;
        dp[i][1]=(dp[i-k][0])%mod;
        sum[i]=(sum[i-1] + dp[i][0]+dp[i][1])%mod;
    }
    for(int i=1;i<=n;i++)
    {
        scanf("%lld%lld",&l,&r);
        printf("%lld\n",(sum[r]-sum[l-1]+mod)%mod);///////
    }
    return 0;
}

相關推薦

練習賽2 A run (簡單dp+細節)

題目描述 White Cloud is exercising in the playground. White Cloud can walk 1 meters or run k meters per second. Since White Cloud is tired,

練習賽7 A Minimum Cost Perfect Matching (數學規律+位運算)

題目描述 You have a complete bipartite graph where each part contains exactly n nodes, numbered from 0 to n - 1 inclusive. The weight of th

訓練 道路問題

一個 question 有道 void lib 以及 -h 測試 iostream 題目描述 隨著如今社會的不斷變化,交通問題也變得越來越重要,所以市長決定建設一些公路來方便各個城市之間的貿易和交易。雖然市長的想法很好,但是他也遇到了一般人也經常頭疼的問題,那就是手頭

訓練 訓練技巧

bits 輸入 targe 單個 sdn namespace 能力 tps i+1 題目描述 來源 常州大學組織了新生寒假訓練一共N天,每天訓練可以獲得的訓練效果是Ei。但是如果連續訓練超過K天,萌新們會受不了而被勸退。 現在負責人想知道,如何安排能保證萌新不會被勸退

訓練 酸堿滴定

itl code spa 簡單 等於 con 數據 aid 輸入 題目描述 題目來源 有時候你會抱怨,什麽時候才能到終點。 有時候你會迫不及待,怎麽顏色還不改變。 滴定管長場的,我們的路長長的。 用心的放入每一滴,終點就在你手心。 今天小星需要去完成一個酸堿滴定實

訓練 德瑪西亞萬歲

while 關系 out iostream 整數 戰鬥 來源 示例 -m 題目描述 來源 德瑪西亞是一個實力雄厚、奉公守法的國家,有著功勛卓著的光榮軍史。 這裏非常重視正義、榮耀、職責的意識形態,這裏的人民為此感到強烈自豪。 有一天他們想去制裁邪惡的比爾吉沃特,於是

第五場

scanner ima one bigint cos system image style util A:gpa 分數規劃裸題了吧,然後二分次數過多的話會超時 #include<cstdio> #include<cstdlib> #includ

訓練第一場 I - Substring(後綴數組 + 重復處理)

.com get 處理 man string target span roman targe 鏈接: https://www.nowcoder.com/acm/contest/139/I 題意: 牛客網多校訓練第一場 I - Substring(後綴數組 + 重復處理

訓練第三場 C - Shuffle Cards(Splay / rope)

str spl shuf 鏈接 contest www size targe strong 鏈接: https://www.nowcoder.com/acm/contest/141/C 題意: 牛客網多校訓練第三場 C - Shuffle Cards(Splay /

訓練 j題 (莫隊)

時間限制:C/C++ 2秒,其他語言4秒 空間限制:C/C++ 524288K,其他語言1048576K 64bit IO Format: %lld 題目描述 Given a sequence

訓練營第五場gpa(01分數規劃)

01分數規劃 01分數規劃問題:所謂的01分數規劃問題就是指這樣的一類問題,給定兩個陣列,a[i]表示選取i的收益,b[i]表示選取i的代價。如果選取i,定義x[i]=1否則x[i]=0。每一個物品只有選或者不選兩種方案,求一個選擇方案使得R=sigma(a[i]*x[

小白月賽9 A簽到(分數,逆元)

傳送門 對分母求一下逆元,把除法取模變成乘法取模,逆元介紹看這裡 這種方法只適合模為質數的情況 1 #include<bits/stdc++.h> 2 using namespace std; 3 const long long mod=1e9+7; 4

2018暑期訓練賽 第一場 (A.D.E.J)

題目連結 A. Monotonic Matrix 給出n和m,問存在多少個n*m矩陣使得對於任意i, j都滿足a(i, j) <= a(i+1, j)且a(i, j) <= a(i, j+1),其中0 <= a(i, j) <= 2。 顯然一個位置的元素

暑假——A-Ternary String(找規律+尤拉降冪)(模板)

題目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a ternary string s which can self-repr

-2018算法第五場D-集合問題+並查集

const 問題 max sin href www post style blank 集合問題 題意: 給你a,b和n個數p[i],問你如何分配這n個數給A,B集合,並且滿足: 若x在集合A中,則a-x必須也在集合A中。 若x在集合B中,則b-x必須也在集合B中。 思路:並

暑假第二場 K carpet

set src freopen pri hide for fine names urn 題意:給你一個n*m的矩陣 ,每個位置都有一個字符並且都有一個值,現在需要找到一個p*q的子矩陣, 原來的矩陣可以由現在這個矩陣無限復制然後截取其中的一部分得到,並且要求 子矩陣裏最大的

暑期 - 尋找子矩陣的個數

inpu have uppercase imp cell 多校 return bottom ron 鏈接:https://www.nowcoder.com/acm/contest/145/J來源:牛客網 題目描述 You have a n * m grid of c

—頭條

題目描述 頭條的2017校招開始了!為了這次校招,我們組織了一個規模巨集大的出題團隊,每個出題人都出了一些有趣的題目,而我們現在想把這些題目組合成若干場考試出來,在選題之前,我們對題目進行了盲審,並定出了每道題的難度系統。一場考試包含3道開放性題目,假設他們的難度從小到大分

——2017招真題線上程式設計(python&C++)

牛客網——2017校招真題線上程式設計(python&C++)題目描述找出n個數裡最小的k個輸入描述:每個測試輸入包含空格分割的n+1個整數,最後一個整數為k值,n 不超過100。輸出描述:輸出

21天 3.2表示式求值

今天上課,老師教了小易怎麼計算加法和乘法,乘法的優先順序大於加法,但是如果一個運算加了括號,那麼它的優先順序是最高的。例如: 1 2 3 4 1+2*3=7 1*(2+3)=5 1*2*3=6 (