Nuclear power plant

Source: Internet
Author: User

Describe

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;

Nuclear power plant

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.