BNU 13064 Dice (I) 前缀和优化DP
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
///
#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的更多相关文章
- LOJ 6089 小Y的背包计数问题 —— 前缀和优化DP
题目:https://loj.ac/problem/6089 对于 i <= √n ,设 f[i][j] 表示前 i 种,体积为 j 的方案数,那么 f[i][j] = ∑(1 <= k ...
- P5241 序列(滚动数组+前缀和优化dp)
P5241 序列 挺神仙的一题 看看除了dp好像没什么其他办法了 想着怎么构个具体的图出来,然鹅不太现实. 于是我们想办法用几个参数来表示dp数组 加了几条边肯定要的吧,于是加个参数$i$表示已加了$ ...
- CDOJ 1307 ABCDE 前缀和优化dp
ABCDE 题目连接: http://acm.uestc.edu.cn/#/problem/show/1307 Description Binary-coded decimal (BCD) is a ...
- bzoj 1044 [HAOI2008]木棍分割——前缀和优化dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1044 前缀和优化. 但开成long long会T.(仔细一看不用开long long) #i ...
- bzoj 3398 [Usaco2009 Feb]Bullcow 牡牛和牝牛——前缀和优化dp / 排列组合
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3398 好简单呀.而且是自己想出来的. dp[ i ]表示最后一个牡牛在 i 的方案数. 当前 ...
- bzoj2431: [HAOI2009]逆序对数列(前缀和优化dp)
2431: [HAOI2009]逆序对数列 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 2312 Solved: 1330[Submit][Stat ...
- CF601C Kleofáš and the n-thlon(期望+前缀和优化dp)
传送门 解题思路 要求这个人的排名,我们可以先求出某个人比他排名靠前的概率,然后再乘上\(m-1\)即为答案.求某个人比他排名靠前可以用\(dp\),设\(f[i][j]\)表示前\(i\)场比赛某人 ...
- 5.19 省选模拟赛 小B的夏令营 概率 dp 前缀和优化dp
LINK:小B的夏令营 这道题是以前从没见过的优化dp的方法 不过也在情理之中. 注意读题 千万不要像我这个sb一样 考完连题意都不知道是啥. 一个长方形 要求从上到下联通的概率. 容易发现 K天只是 ...
- 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 ...
随机推荐
- 大数除法(除数在int范围内)
#include<iostream> #include<cstdio> #include<cstring> #define N 1000 using namespa ...
- 并发编程学习笔记(12)----Fork/Join框架
1. Fork/Join 的概念 Fork指的是将系统进程分成多个执行分支(线程),Join即是等待,当fork()方法创建了多个线程之后,需要等待这些分支执行完毕之后,才能得到最终的结果,因此joi ...
- java byte
项目中有段代码,一直让我疑惑不解,但我是个很会偷懒的人,只要拷贝来改改能用的代码,万万不会自己动手写,虽然一直有疑惑,也懒得搭理是怎么个原理. 直到今天,又要解析协议,又要动这个地方的代码,还是来盘他 ...
- day20-面向对象基础
目录 面向对象基础 面向过程编程与面向对象编程 面向过程编程 面向对象编程 类与对象 类 对象 定义类和对象 定制对象独有特征 对象属性查找顺序 类与对象的绑定方法 类与数据类型 对象的高度整合 面向 ...
- 梦想MxWeb3D协同设计平台 2018.10.12更新
SDK开发包下载地址: http://www.mxdraw.com/ndetail_10107.html 1. 全新的在线的三维协同设计平台,高效异步方式,基于JavaScript和WebGL技术,前 ...
- nginx做反向代理配置文件的例子
worker_processes ; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error ...
- python logger日志
直接上代码 import logging import logging.handlers import datetime import time import threading from conf. ...
- 一个简单的java年龄计算器
制作一个如下图年龄计算器 根据题目,我做了一个由Calendar类以及年月日各相减得到的年龄,当然正确的方法不止一个,以下为我的源代码和结果截图: package com.Date; import j ...
- ListView常用属性 (2012-01-12 17:20:27)
比较特别的属性,通过设置这样的属性可以做出更加美观的列表.stackFromBottom——设置该属性之后你最新条目就会显示你列表的最下面,值为true和false,如android:stackFro ...
- Xcode git 忽略user interface state文件
退出xcdoe, 打开终端(Terminal),进入到你的项目目录下 在终端输入如下代码 git rm --cached *.xcuserstate git commit -m "Remov ...