ACdream 1113 The Arrow (概率dp求期望)
Description
The history shows that We need heroes in every dynasty. For example, Liangshan Heroes. People hope that these heroes can punish the bad guys and recover the justice. Nowadays, we also need heroes to provent us from being chopped, or being attacked by a bomb.
Kuangbin is a very very very very very.... (very * 1e9 ) good boy, after he knows The Arrow, he want to be The Arrow of China. But he is also a little afraid of being killed by the bad guys. So he decides to throw dices to make the decision.
The dice is a cube with 1 2 3 4 5 6 on it's sides. When he throws a dice, every number is of the same probablity to appear. He will write down a number N in the paper at first, and then throw the dice. When the sum of the number he throwed is less than N, he will keep throwing. But if the sum exceeds N, this throwing does not count.
For example, If the sum is 5,and N is 6, if we throw 2, 5 + 2 > 6, then the sum keeps to be 5.
If he can end the throwing in a certain time, he will make the decision to become The Arrow.
Now , kuangbin wonders that what's the expectation of the time of throwing dices.
Input
First line, The number of cases t <= 100
In the next t lines, there will be t numbers.
every number is not bigger than 100000
Output
Sample Input
1
1
Sample Output
6.00
题意:给定一个数n。现有一个骰子,六个面分别是123456,掷骰子,记录下掷出来的数的总和以及掷的次数,求和为n的时候,掷的次数的期望为多少。如果掷的和超过n就保持原来的n,比如n=10,第一次掷5第二次掷6,和是11>10,n保持5不变。有T组数据,每组数据给定一个n。
题解:掷到123456的期望是6,掷到7的期望是掷到123456的期望总和除以6+1。给出优化前和优化后的代码:
优化前:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int main()
{
int t;
double dp[];
cin>>t;
while(t--)
{
int n;
cin>>n;
memset(dp,,sizeof(dp));
for(int i=;i<=;i++)
dp[i]=;
for(int i=;i<=n;i++)
{
dp[i]++; //又掷了一次
for(int j=;j<=;j++)
dp[i]=dp[i]+dp[i-j]/;
}
printf("%.2lf\n",dp[n]);
}
return ;
}
优化后:
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=1e5+;
double dp[maxn];
void get()
{
memset(dp,,sizeof(dp));
for(int i=; i<=; i++)
dp[i]=;
for(int i=; i<=maxn; i++)
dp[i]=dp[i-]-dp[i-]/+dp[i-]/;
}
int main()
{
get();
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
printf("%.2lf\n",dp[n]);
}
return ;
}
ACdream 1113 The Arrow (概率dp求期望)的更多相关文章
- HDU3853-LOOPS(概率DP求期望)
LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others) Total Su ...
- POJ2096 Collecting Bugs(概率DP,求期望)
Collecting Bugs Ivan is fond of collecting. Unlike other people who collect post stamps, coins or ot ...
- LightOJ 1030 【概率DP求期望】
借鉴自:https://www.cnblogs.com/keyboarder-zsq/p/6216762.html 题意:n个格子,每个格子有一个值.从1开始,每次扔6个面的骰子,扔出几点就往前几步, ...
- HDU-3853 LOOPS(概率DP求期望)
题目大意:在nxm的方格中,从(1,1)走到(n,m).每次只能在原地不动.向右走一格.向下走一格,概率分别为p1(i,j),p2(i,j),p3(i,j).求行走次数的期望. 题目分析:状态转移方程 ...
- HDU 3853 LOOP (概率DP求期望)
D - LOOPS Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- HDU 4405 Aeroplane chess (概率DP求期望)
题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点须要步数的期望 当中有m个跳跃a,b表示走到a点能够直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点能够到走到i+1 ...
- HDU-4035 Maze (概率DP求期望)
题目大意:在一个树形迷宫中,以房间为节点.有n间房间,每间房间存在陷阱的概率为ki,存在出口的概率为ei,如果这两种情况都不存在(概率为pi),那么只能做出选择走向下一个房间(包括可能会走向上一个房间 ...
- HDU-4405 Aeroplane chess(概率DP求期望)
题目大意:一个跳棋游戏,每置一次骰子前进相应的步数.但是有的点可以不用置骰子直接前进,求置骰子次数的平均值. 题目分析:状态很容易定义:dp(i)表示在第 i 个点出发需要置骰子的次数平均值.则状态转 ...
- hdu 4405 Aeroplane chess(简单概率dp 求期望)
Aeroplane chess Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
随机推荐
- 引用外部静态库(.a文件)时或打包.a时,Category方法无法调用。崩溃
我的这个是MJRefresh,学习打.a包Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: ...
- Vue一些重要的知识点
vue sync修饰(1)双向数据绑定,父子组件之间信息的交互 1⃣️在自组件中使用this.emmit('toFather'),子组件产生一个tofather事件,然后在父组件中通过@进行监听,那么 ...
- 自动化测试--封装getDriver的方法
在自动化测试的时候,通常都会把最常用的功能封装起来,实现通用性. 该篇博客是实现了getDriver方法的封装. 第一次封装的时候,是使用的传参. @Parameters(value = {" ...
- SPFA模板 Bellmanford优化版
SPFA模板: queue<int>Q; ]; ],sumv[]; *],__next[*],e,w[*],first[],cnts[]; void AddEdge(int U,int V ...
- 了解游戏编程与 AI
噫语系列... 闲话 最近在重写我的一个 QQ 群机器人项目,并尝试将它改成更通用的结构,以方便在未来加入对 Wechat 和 Telegram 的支持. 在查资料的过程中,发现很多人认为一个群内多人 ...
- flask - 1
from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, Worl ...
- POJ 2761 Feed the dogs(平衡树or划分树or主席树)
Description Wind loves pretty dogs very much, and she has n pet dogs. So Jiajia has to feed the dogs ...
- 第十四次ScrumMeeting会议
第十三次Scrum Meeting 时间:2017/12/2 地点:咖啡馆 人员:策划组美工组 目前工作情况 名字 完成的工作 计划工作 蔡帜 科技树策划设计,科技图鉴蓝图设计 员工方面细节设定 游心 ...
- 揭开网络编程常见API的面纱【上】
Linux网络编程API函数初步剖析 今天我们来分析一下前几篇博文中提到的网络编程中几个核心的API,探究一下当我们调用每个API时,内核中具体做了哪些准备和初始化工作. 1.socket(famil ...
- zookeeper3.4.6完全分布式安装
首先在官网下载zookeeper3.4.6安装包,解压到/usr/local目录下 然后改名为zookeeper. 环境变量配置:sudo vim /etc/profile 添加环境变量如下图 然后 ...