Dice (I)

 

You have N dices; each of them has K faces numbered from 1 to K. Now you have arranged the N dices in a line. You can rotate/flip any dice if you want. How many ways you can set the top faces such that the summation of all the top faces equals S?

Now you are given N, K, S; you have to 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

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: 57286574

Case 4: 72413502

Case 5: 9

Source

 
题意:给你n个骰子,每个骰子有1-k个分数,问你多少种方式的和是S
题解:  首先dp[i][j]表示前i个骰子,和为S的方案数
    那么 dp[i][j]=dp[i-1][j-1]+............+dp[i-1][j-k];
    对于i完全由i-1的状态得到,我们这可以用   sum[j]表示 dp[i-1][1到j]的一个前缀和,
    用i表示当前要计算的now状态,last表示i-1的状态 由此我们可以 得到dp[now][j]=sum[j-1]-sum[j-k-1];
    最终答案就是dp[now][s];

///
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define memfy(a) memset(a,-1,sizeof(a));
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mod 100000007
#define inf 100000000
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//**************************************** #define maxn 15000+5
ll dp[][maxn],sum[maxn];
int n,k,s;
int main()
{ int T=read();
int oo=;
while(T--)
{
scanf("%d%d%d",&n,&k,&s);
int last=,now=;
mem(dp);
dp[now][]=;
for(int i=;i<=n;i++)
{
swap(last,now);
mem(dp[now]);
sum[]=dp[last][];
for(int j=;j<=s;j++)
{
sum[j]=(sum[j-]+dp[last][j])%mod;
}
for(int j=;j<=s;j++)
{
if(j<=k) dp[now][j]=(sum[j-])%mod;
else
dp[now][j]=(sum[j-]-sum[j-k-]+mod)%mod;
}
}
printf("Case %d: ",oo++);
cout<<dp[now][s]<<endl;;
}
return ;
}

代码

BNU 13064 Dice (I) 前缀和优化DP的更多相关文章

  1. LOJ 6089 小Y的背包计数问题 —— 前缀和优化DP

    题目:https://loj.ac/problem/6089 对于 i <= √n ,设 f[i][j] 表示前 i 种,体积为 j 的方案数,那么 f[i][j] = ∑(1 <= k ...

  2. P5241 序列(滚动数组+前缀和优化dp)

    P5241 序列 挺神仙的一题 看看除了dp好像没什么其他办法了 想着怎么构个具体的图出来,然鹅不太现实. 于是我们想办法用几个参数来表示dp数组 加了几条边肯定要的吧,于是加个参数$i$表示已加了$ ...

  3. CDOJ 1307 ABCDE 前缀和优化dp

    ABCDE 题目连接: http://acm.uestc.edu.cn/#/problem/show/1307 Description Binary-coded decimal (BCD) is a ...

  4. bzoj 1044 [HAOI2008]木棍分割——前缀和优化dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1044 前缀和优化. 但开成long long会T.(仔细一看不用开long long) #i ...

  5. bzoj 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——前缀和优化dp / 排列组合

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 好简单呀.而且是自己想出来的. dp[ i ]表示最后一个牡牛在 i 的方案数. 当前 ...

  6. bzoj2431: [HAOI2009]逆序对数列(前缀和优化dp)

    2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 2312  Solved: 1330[Submit][Stat ...

  7. CF601C Kleofáš and the n-thlon(期望+前缀和优化dp)

    传送门 解题思路 要求这个人的排名,我们可以先求出某个人比他排名靠前的概率,然后再乘上\(m-1\)即为答案.求某个人比他排名靠前可以用\(dp\),设\(f[i][j]\)表示前\(i\)场比赛某人 ...

  8. 5.19 省选模拟赛 小B的夏令营 概率 dp 前缀和优化dp

    LINK:小B的夏令营 这道题是以前从没见过的优化dp的方法 不过也在情理之中. 注意读题 千万不要像我这个sb一样 考完连题意都不知道是啥. 一个长方形 要求从上到下联通的概率. 容易发现 K天只是 ...

  9. Codeforces Round #274 (Div. 1) C. Riding in a Lift 前缀和优化dp

    C. Riding in a Lift Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/480/pr ...

随机推荐

  1. spring加载classpath与classpath*的区别别

    1.无论是classpath还是classpath*都可以加载整个classpath下(包括jar包里面)的资源文件. 2.classpath只会返回第一个匹配的资源,查找路径是优先在项目中存在资源文 ...

  2. (转)淘淘商城系列——商品搜索功能Dao实现

    http://blog.csdn.net/yerenyuan_pku/article/details/72909286 终于进入商品搜索功能的开发中了,本文我来教大家编写实现商品搜索功能的Dao层代码 ...

  3. jboss解决ip访问受限问题

    jboss启动后,localhost可以访问,127.0.0.1可以访问,但是内网ip却访问不了,比如ip是192.168.1.2,这个192.168.1.2就访问不到web页面 解决方案: jbos ...

  4. JAVA基础——is-a 、have-a、和 like-a的区别

    1.is-a,has-a,like-a是什么 在面向对象设计的领域里,有若干种设计思路,主要有如下三种: is-a.has-a.like-a java中在类.接口.抽象类中有很多体现. 了解java看 ...

  5. thupc & cts & apio & thusc 游记 (北京17日游记)

    thupc & cts & apio & thusc 游记 (北京17日游记) Day 0 和隔壁校两人py了一下,六个人组了两队,(左哼哼)与(右哼哼),我和Camoufla ...

  6. TWaver 3D应用于大型数据中心(续)

    在2014年11月份,我们当时发了一篇有关TWaver HTML5 3D应用于大型数据中心的文章,该blog比较详细的描述一些常用的功能的实现方法,比如:动态添加机柜,告警,温度,湿度等相关的功能的具 ...

  7. Python学习笔记(1)对象类型

    强制转换字符串函数str 如果我们求2的一百万次方是多少那么我们可以 print(2**1000000) 如果我们要求2的一百万次方有多少位那么我们可以用str函数强制转换成字符串然后len函数计算 ...

  8. MySql-了解存储引擎

    怎么应对不同版本 在不同的 mysql 版本中,很多特性和语法有可能是不一样的,我们怎么样才能知道当前版本的语法是什么样呢?最好的办法是学会使用 mysql 的帮助. A.按照层次看帮助 例如:mys ...

  9. nginx代理标准配置

    #nginx开启的进程数worker_processes   4;     #4核CPU   #定义全局错误日志定义类型,[debug|info|notice|warn|crit]error_log  ...

  10. Flask项目中整合各组件

    一.介绍 主要介绍flask_sqlalchemy.flask_script.flask_migrate这三个组件该如何整合到flask项目中,以及如何使用. # 安装组件 pip3 install ...