HDU 4045 Machine scheduling (second class Stirling number +DP)

Source: Internet
Author: User

A Baidu ' s engineer needs to analyze and process large amount of the data on machines every day. The machines is labeled from 1 to N. On all day, the engineer chooses R machines to process data. He allocates the R machines to no more than M groups, and if the difference of 2 machines ' labels is less than k,they can Not work on the same day. Otherwise the machines won't work properly. That's to say, the machines labeled with 1 and k+1 can work in the same day while those labeled with 1 and K should not W Ork in the same day. Due to some unknown reasons, the engineer should isn't choose the allocation scheme the same as that on some previous day. Otherwise all the machines need to be initialized again. As you know, the initialization would take a long time and a lot of efforts. Can you tell the engineer the maximum days that he can use these machines continuously without re-initialization. 

Inputinput End With EOF.
Input would be a four integers n,r,k,m.we assume that they is all between 1 and 1000.
Outputoutput the Maxmium days modulo 1000000007.
Sample Input

5 2 3 2

Sample Output

6         

Hint

Sample input means you can choose 1 and 4,1 and 5,2 and 5 in the same day. And you can make the machines in the same group or the different group.  Got 6 schemes.1 and 4 in same group,1 and 4 in different groups.1 and 5 in same group,1 and 5 in different groups.2 and 5 in same group,2 and 5 in different groups. We assume 1 in a group and 4 in B group are the same as 1 in B group and 4 in a group.

Test instructions, now there are 1 to n,n number, from which to select the number of R, required to select the difference of at least K, and finally put the selected number into the box of no difference m, ask how many kinds of solutions? Take the mold 1e9+7

Analysis:

A two-step operation is required, the first step is to select the number of R, and find out the total number of scenarios

The second step is that the number of R is already there, then the specific value of the R number is not important, only need to put the number of R number in the box.

Finally multiply the results of the two steps

For the first step: you can use DP[I][J] to indicate number I is the number of programs of J

Dp[i][j]= (Dp[i][j]+dp[z][j-1])%mod (z = number from 1 to i-k)

But this requires a triple loop that takes more time than the data range

And this is actually only related to the former (i-k), so we need to preprocess it in a rolling array.

Step two: If you just put the M box, it's the second class of Stirling. But here is not much more than M boxes, so as a second class of Stirling numbers that just fit into a box (1 to m) sum

The code is as follows:

#include <cstdio>#include<algorithm>#include<iostream>#include<vector>#include<cstring>#include<cmath>using namespaceStd;typedefLong LongLL; LL stl[1100][1100]; LL sumstl[1100][1100]; LL dp[1100][1100]; LL d[1100]; LL d2[1100];Const intmod=1e9+7;voidinit () { for(intI=1; i<= +; i++) Stl[i][i]=1;  for(intI=1; i<= +; i++)    for(intj=1; j<i;j++) Stl[i][j]= (stl[i-1][j-1]+j*stl[i-1][J]%MOD)%MOD;  for(intI=1; i<= +; i++)     for(intj=1; j<= +; j + +) Sumstl[i][j]= (sumstl[i][j-1]+STL[I][J])%MOD;}intMain () {LL N,r,k,m,sum,ans;     Init ();  while(SCANF ("%lld%lld%lld%lld", &n,&r,&k,&m)! =EOF) {Sum=0; Memset (DP,0,sizeof(DP));  for(intI=1; i<=n;i++) {dp[i][1]=1; D[i]= (d[i-1]+dp[i][1])%MOD; }          for(intj=2; j<=r;j++)        {            for(intI=1; i<=n;i++)          {              if(i-k>=0) Dp[i][j]= (Dp[i][j]+d[i-k])%MOD; D2[i]= (d2[i-1]+DP[I][J])%MOD; }           for(intz=1; z<=n;z++) D[z]=D2[z]; }         for(intI=1; i<=n;i++) Sum= (Sum+dp[i][r])%MOD; //cout<<sum<<endl;Ans= (Sum*sumstl[r][m])%MOD; cout<<ans<<Endl; }    return 0;}

HDU 4045 Machine scheduling (second class Stirling number +DP)

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.