1193 - Dice (II)

   PDF (English) Statistics Forum
Time Limit: 3 second(s) Memory Limit: 32 MB

You have N dices; each of them has K faces numbered from 1 to K. Now you can arrange the N dices in a line. If the summation of the top faces of the dices is S, you calculate the score as the multiplication of all the top faces.

Now you are given N, K, S; you have to calculate the summation of all the scores.

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) 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: 3

Case 2: 84

Case 3: 74335590

Case 4: 33274428

Case 5: 165



PROBLEM SETTER: JANE ALAM JAN

思路:和1145差不多。

dp[i][j]表示前i次点数之和为j的数所有对数的各个的乘积之和。

那么dp[i][j]=dp[i-1][j-1]+dp[i-1][j-2]*2+....dp[i-1][j-k]*k;

这样的复杂度是n*n*n;这样是过不了的;

那么dp[i][j+1]=dp[i-1][j]+dp[i-1][j-1]*2+dp[i-1][j-2]*3+....dp[i-1][j-k+1]*k;

那么dp[i][j+1]=dp[i][j-1]+dp[i-1][j-1]+sum[j-2]-sum[max(0,j-1-m)]-dp[i-1][max(0,j-m-1)]*m;

那么我们每次维护一个dp[i][j]的前缀和就可以了。

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<string.h>
 5 #include<math.h>
 6 #include<stdlib.h>
 7 #include<queue>
 8 using namespace std;
 9 typedef long long LL;
10 LL dp[2][15005];
11 const int mod=100000007;
12 LL sum[15005];
13 int main(void)
14 {
15     int i,j,k;
16     scanf("%d",&k);
17     int s;
18     int n,m,c;
19     for(s=1; s<=k; s++)
20     {
21         scanf("%d %d %d",&n,&m,&c);
22         memset(dp,0,sizeof(dp));
23         memset(sum,0,sizeof(sum));
24         for(i=1; i<=m; i++)
25         {
26             dp[1][i]=i;
27             dp[1][i]%=mod;
28             sum[i]=(sum[i-1]+dp[1][i])%mod;
29         }
30         for(i=m+1;i<=c;i++)
31             sum[i]=sum[i-1];
32         for(i=2; i<=n; i++)
33         {
34             int uu=(i)%2;
35             int kk=(i+1)%2;
36             for(j=0; j<i; j++)
37             {
38                 dp[uu][j]=0;
39             }
40             for(j=i;j<=c; j++)
41             {
42       dp[uu][j]=((dp[kk][j-1]+dp[uu][j-1])%mod+(sum[j-2]-sum[max(0,j-1-m)])%mod-dp[kk][max(0,j-m-1)]*m%mod)%mod;
43                 dp[uu][j]%=mod;
44                 dp[uu][j]+=mod;
45                 dp[uu][j]%=mod;
46             }
47             for(j=1; j<=c; j++)
48             {
49                 sum[j]=sum[j-1]+dp[uu][j];
50                 sum[j]%=mod;
51             }
52         }
53         printf("Case %d: ",s);
54         printf("%lld\n",(dp[n%2][c]%mod+mod)%mod);
55     }
56     return 0;
57 }
时间: 2024-08-04 07:04:54

1193 - Dice (II)的相关文章

lightoj-1193 - Dice (II)(dp+前缀和)

1193 - Dice (II) PDF (English) Statistics ForumTime Limit: 3 second(s) Memory Limit: 32 MBYou have N dices; each of them has K faces numbered from 1 to K. Now you can arrange the N dices in a line. If the summation of the top faces of the dices is S,

126. Word Ladder II(js)

126. Word Ladder II Given two words (beginWord and endWord), and a dictionary's word list, find all shortest transformation sequence(s) from beginWord to endWord, such that: Only one letter can be changed at a time Each transformed word must exist in

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

使用华邦的SPI FLASH作为EPCS时固化NIOS II软件报错及解决方案

Altera器件有EPCS系列配置器件,其实,这些配置器件就是我们平时通用的SPIFlash,据AlteraFAE描述:"EPCS器件也是选用某家公司的SPIFlash,只是中间经过Altera公司的严格测试,所以稳定性及耐用性都超过通用的SPIFlash".就本人看来,半导体的稳定性问题绝大部分都是由本身设计缺陷造成的,而成熟的制造工艺不会造成产品的不稳定:并且,现在Altera的器件在读入配置数据发生错误时,可以重新读取SPIFlash里面的数据,所以在工艺的稳定性以及设计的可靠性

hdu 1207 汉诺塔II (DP+递推)

汉诺塔II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4529    Accepted Submission(s): 2231 Problem Description 经典的汉诺塔问题经常作为一个递归的经典例题存在.可能有人并不知道汉诺塔问题的典故.汉诺塔来源于印度传说的一个故事,上帝创造世界时作了三根金刚石柱子,在一根柱子上从下往

AC日记——小A和uim之大逃离 II 洛谷七月月赛

小A和uim之大逃离 II 思路: spfa: 代码: #include <bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f struct NodeType { int x,y,k; NodeType(int x_,int y_,int k_):x(x_),y(y_),k(k_){} NodeType(){} }; const int dx[5]={0,-1,0,1,0}; const int dy[5]={0,0,1,0,-

remove-duplicates-from-sorted-list I&amp;II——去除链表中重复项

I.Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3. PS:遍历,而后记录pre,并删除后续重复node 1 /** 2 * Definition for singly-linke

Intersection of Two Arrays I &amp; II

题目链接:https://leetcode.com/problems/intersection-of-two-arrays/ 题目大意:要求两个数组的交集(注意集合是不能含有重复的元素的) 方法1) 先对两个数组进行排序,设置两个指针pA和pB,分别指向这两个数组,比较nums1[pA]和nums[pB] a. 如果想等,则为交集中的元素,++pA, ++pB b. 如果nums[pA] < nums[pB],则++pA c. 否则,++pB 注意数组中有重复的元素(实现代码中的小trick)

Wiggle Sort II

Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... 注意事项 You may assume all input has valid answer. 样例 Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. Given nums