Cattle off winter break five f annoying chatters (dp + and prefixes)

Here Insert Picture Description
Our goal is to find the interval l and r may be order, then we can determine the course of 1 to x-1 of a certain order is only one full ac, i.e. dp [I] = dp [I-1]; It can only be transferred from the previous step, and each step can be done in two possible max x to the process, and the last is the last ac RJ, that is the state transition equation is dp [i] = dp [ i-1] + dp [ix]; we can get the number of each step of the method of arrival, then we have found a plurality of sets of questions provided topic, each time with the addition algorithm will cause a timeout, so we have to use a prefix before and i represents the method of the total species inside, so that each section can ask a calculating subtraction, and the nature of the application prefix

code show as below


#include <bits/stdc++.h>
using namespace std;
const int MAX=1e9+7;
int f[100008];
int main() {
    int k,q,l,r;
 cin>>k>>q;
        f[0]=1,f[k]=2;
        for(int i=1;i<k;i++) f[i]=1;
        for(int i=k+1;i<100008;i++) f[i]=(f[i-1]+f[i-k-1])%MAX;
        for(int i=1;i<100008;i++) f[i]=(f[i]+f[i-1])%MAX;
        while(q--)
        {
            cin>>l>>r,l--;
            printf("%d\n",(f[r]-f[l]+MAX)%MAX);
        }
    return 0;
}
Published 48 original articles · won praise 17 · views 4463

Guess you like

Origin blog.csdn.net/weixin_45757507/article/details/104317741