A nuclear power plant has n a pit of nuclear material, and the pits are arranged in a straight line. If the nuclear material is placed in a continuous M-pit, an explosion will occur and the nuclear substance may not be released in certain pits.
Mission: For a given N and m, the total number of solutions for placing nuclear matter without explosion
--by Noi.openjudge
http://noi.openjudge.cn/ch0206/9267/
《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《《 《《《《《《
The old question ...
OK, is the previous problem, recently was mentioned by others, then tidy up a bit of ideas;
There are two ways of thinking:
1.F[N]=F[N-1]+F[N-2]+...+F[N-M];
When n is not put, the scheme of the first n equals the scheme of the former n-1, and then when N is put, it can be n-1, equal to the number of n-2 scheme, and then when the n,n-1 is put, it can be n-2, equal to the n-3 scheme number ... Until when n to N-m+2 put, can n-m+1, equal to the number of N-M scheme;
2.F[N]=2*F[N-1]-F[N-M-1];
The former N scheme equals the case of the former n-1, but at this time there is a continuous n-m+1~n full put but n-m not put the illegal situation (but there is no continuous n-m+1~n all put and n-m also put the illegal situation), her number is n-m-1 number of programs multiplied by 1, cut her off;
It is worth mentioning that:
F[N]=F[N-1]+F[N-2]+...+F[N-M];
F[N-1]=F[N-2]+F[N-3]+...+F[N-M-1];
therefore f[n]=f[n-1]+f[n-1]-f[n-m-1];
Namely equation two can be regarded as the optimization of equation one!!!
The code is as follows:
#include <cstdio>using namespacestd;Long Longf[Wuyi];intMain () {inti,k,n,m; Long LongJ; scanf ("%d%d",&n,&m); f[0]=1; for(i=1; i<=n;i++) { if(i-m-1<-1) j=0; Else if(i-m-1==-1) j=f[0]; Elsej=f[i-m-1]; F[i]=2*f[i-1]-J; } printf ("%lld", F[n]); return 0;}
Wish AC;