E - The Arrow

Time Limit:1000MS     Memory Limit:64000KB     64bit IO Format:%lld & %llu

Submit Status

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

Each test output a number rounded to the second digit.

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求期望)的更多相关文章

  1. HDU3853-LOOPS(概率DP求期望)

    LOOPS Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Total Su ...

  2. POJ2096 Collecting Bugs(概率DP,求期望)

    Collecting Bugs Ivan is fond of collecting. Unlike other people who collect post stamps, coins or ot ...

  3. LightOJ 1030 【概率DP求期望】

    借鉴自:https://www.cnblogs.com/keyboarder-zsq/p/6216762.html 题意:n个格子,每个格子有一个值.从1开始,每次扔6个面的骰子,扔出几点就往前几步, ...

  4. HDU-3853 LOOPS(概率DP求期望)

    题目大意:在nxm的方格中,从(1,1)走到(n,m).每次只能在原地不动.向右走一格.向下走一格,概率分别为p1(i,j),p2(i,j),p3(i,j).求行走次数的期望. 题目分析:状态转移方程 ...

  5. HDU 3853 LOOP (概率DP求期望)

    D - LOOPS Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit St ...

  6. HDU 4405 Aeroplane chess (概率DP求期望)

    题意:有一个n个点的飞行棋,问从0点掷骰子(1~6)走到n点须要步数的期望 当中有m个跳跃a,b表示走到a点能够直接跳到b点. dp[ i ]表示从i点走到n点的期望,在正常情况下i点能够到走到i+1 ...

  7. HDU-4035 Maze (概率DP求期望)

    题目大意:在一个树形迷宫中,以房间为节点.有n间房间,每间房间存在陷阱的概率为ki,存在出口的概率为ei,如果这两种情况都不存在(概率为pi),那么只能做出选择走向下一个房间(包括可能会走向上一个房间 ...

  8. HDU-4405 Aeroplane chess(概率DP求期望)

    题目大意:一个跳棋游戏,每置一次骰子前进相应的步数.但是有的点可以不用置骰子直接前进,求置骰子次数的平均值. 题目分析:状态很容易定义:dp(i)表示在第 i 个点出发需要置骰子的次数平均值.则状态转 ...

  9. hdu 4405 Aeroplane chess(简单概率dp 求期望)

    Aeroplane chess Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

随机推荐

  1. 关于 SSH Server 的整体设定

    # . 关于 SSH Server 的整体设定,包含使用的 port 啦,以及使用的密码演算方式 Port # SSH 预设使用 这个 port,您也可以使用多的 port ! # 亦即重复使用 po ...

  2. 『AngularJS』ngValue

    原文 描述 绑定给定的表达式到input[select]或input[radio]的值,以便当这个元素被选中的时候,设置这个元素的ngModel到绑定的值.当需要使用ng-repeat来动态生成rad ...

  3. Java Set集合(HashSet、TreeSet)

    什么是HashSet?操作过程是怎么样的? 1.HashSet底层实际上是一个HashMap,HashMap底层采用了哈希表数据结构 2.哈希表又叫做散列表,哈希表底层是一个数组,这个数组中每一个元素 ...

  4. Python Flask之旅

    <Pyhton Flask之旅> 以前学flask时做的总结,搬运到这里,markdown格式写的有点乱,凑合看吧. 参考博客 http://blog.csdn.net/nunchakus ...

  5. Python 把两个列表遍历为一个

    两个list, 有对应关系,希望同时完成遍历 用迭代器迭代的方法也不是不可以,python提供了更直观的方法: 可以使用zip把两个list打包 , 类似: list1 = [1,2,3,4] lis ...

  6. restFul介绍及其使用规范

    什么是REST和RESTful API? REST:(英文:Representational State Transfer,简称REST)表征性状态转移,是一种软件架构风格. RESTful : RE ...

  7. IDEA运行lambda表达式

    在idea写了一个lambda的测试例子,但是运行一直报错, public class LambdaTest { public static void main(String[] args) { ne ...

  8. Reactor模式是什么(转载)

    一.Reactor模式是什么反应器设计模式(Reactor pattern)是一种为处理并发服务请求,并将请求提交到一个或者多个服务处理程序的事件设计模式.当客户端请求抵达后,服务处理程序使用多路分配 ...

  9. 201621123033 《Java程序设计》第11周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...

  10. Redis能做什么?不能做什么?

    https://blog.csdn.net/u014229282/article/details/81174202 最近阅读了<redis设计与实现>,这是一本比较枯燥的书,毕竟涉及到re ...