Luo Gu P1192 level problem

Topic link: https: //www.luogu.org/problem/P1192

Set a [i] for the i-th method come Number

Suppose can take up k steps, for any x (x> = k) is easy to know

1.a[x]=a[x-1]+a[x-2]+…+a[x-k]

2.a[x+1]=a[x]+a[x-1]+a[x-2]+…+a[x+1-k]=2*a[x]-a[x-k]

Therefore, we only need to first determine A [. 1] A [K] and the remaining A [K +. 1] A [n-] 2 can be directly through a calculation by the above formula was calculated directly as the time when the data is relatively large k << n, which is approximately the time complexity of O (n)

As a [1] ~ a [k] can be obtained by clicking method

    void init(){
        a[0]=1;   //a[0]表示走到第0阶(即起点)的方法数很容易理解为1
        for(register int i=0;i<k;i++)
        //从起点开始,访问每一阶级,假设当前访问的是第i阶,那么a[i+1]~a[i+k]要加上a[i],很容易理解
          for(register int j=1;j<=k;j++) a[i+j]=(a[i+j]+a[i])%100003;
        //可能会改变a[x](x>k)的值,但是没有影响,因为接下来是直接给a[k+1]~a[n]赋值的,所以会覆盖掉
    }
    
    //实际上对于a[1]~a[k],a[i](1<=i<=k)为2的(i-1)次方
    //至于为什么大家可以根据我上面的init()函数带入几个k写一写很容易就懂了

The following is the complete code

#include <cstdio>
int n,k,a[100005];
void init(){
    a[0]=a[1]=1;
    for(register int i=2;i<=k;i++)
        a[i]=(2*a[i-1])%100003;
}
int main(){
    scanf("%d %d",&n,&k);
    init();
    for(register int i=k+1;i<=n;i++) a[i]=(2*a[i-1]+100003-a[i-1-k])%100003;
    printf("%d\n",a[n]);
    return 0;
}
Published 77 original articles · won praise 19 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43488547/article/details/100049547