Lightoj-1145-dice (I) (DP count)

Source: Internet
Author: User

1145-dice (I)
PDF (中文版) statisticsforum
Time Limit:2 second (s) Memory limit:32 MB
You have N dices; Each of the them have K faces numbered from 1 to K. Now there are arranged the N dices in a line. You can rotate/flip any dice if you want. How many ways can set the top faces such so the summation of all the top faces equals S?

Now is given N, K, S; Calculate the total number of ways.

Input
Input starts with an integer T (≤25), denoting the number of test cases.

Each case contains three integers:n (1≤n≤1000), K (1≤k≤1000) and S (0≤s≤15000).

Output
For each case print, the case number and the result modulo 100000007.

Sample Input
Output for Sample Input
5
1 6 3
2 9 8
500 6 1000
800 800 10000
2 100 10
Case 1:1
Case 2:7
Case 3:57,286,574
Case 4:72,413,502
Case 5:9

The idea of solving a problem: defining dp[i][j] means shaking n dice, the sum of which is J.

Not hard to find: dp[i][j] = dp[i-1][j-1] + ... +dp[i-1][max (0,j-k)]; Adding up to the number of J in all i-1 layers is the number of times the N dice can be invited to J.

Then dp[i][j-1] = Dp[i-1][j-2]+......+dp[i-1][max (0,j-k-1)];

Merged dp[i][j] = Dp[i][j-1]+dp[i-1][j-1]-dp[i-1][max (0,j-k-1)];

Because to add mod, so the formula is written as dp[i][j] = (Dp[i][j-1]+dp[i-1][j-1]-dp[i-1][max (0,j-k-1)]+mod)%mod;ps: Inside +mod is because there may be more than the first 2 mod. At this time to +mod to correct him to positive.

The above state transfer is sufficient to solve this problem, but the topic gives only 32MB of memory. This will exceed the limit.

We will find in the analysis, in fact, each calculation only need to i-1,i these two states, then we define a dp[2][j] to scroll the storage can be

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMoD =100000007;Long Longdp[2][15010];intMain () {intt,n,k,s; scanf ("%d",&T);  for(intt=1; t<=t;t++) {scanf ("%d%d%d",&n,&k,&s); Memset (DP,0,sizeof(DP));  for(intI=1; i<=k;i++) dp[1][i] =1;  for(intI=2; i<=n;i++){             for(intj=1; j<=s;j++) {Dp[i%2][J] = (dp[i%2][j-1]+dp[(i+1)%2][j-1]-dp[(i+1)%2][max (0, j-k-1)]+mod)%MoD; }            //cout<<dp[i][s]<<endl;} printf ("Case %d:%lld\n", t,dp[n%2][s]); }    }

Lightoj-1145-dice (I) (DP count)

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.