HDU5159--BC--Card(概率写法,和组合写法)

概率方法

要求出和的期望,期望的基本定理, 和的期望 = 各部分期望的和。

E(sum) = E(1) + E(2) + ... + E(x) ;

每个数在b次中只有选到和没选到两种情况,在b次中被选到的概率p =1 -  (1 - 1/x)^b ;

所以每个数的期望也是 i*( 1-(1-1/x)^b )

得到sum的期望。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
double e[110000] ;
double f(double x,int d)
{
    double ans = 1.0 ;
    if( d == 0 ) return 1.0 ;
    ans = f(x,d/2) ;
    ans *= ans ;
    if( d%2 )
        ans *= x ;
    return ans ;
}
int main()
{
    int t , tt , x, d , i ;
    scanf("%d", &t) ;
    for(tt = 1 ; tt <= t ; tt++)
    {
        scanf("%d %d", &x, &d) ;
        for(i = 1 ; i <= x ; i++)
            e[i] = 1.0 - f(1.0-1.0/x,d) ;
        for(e[0] = 0 , i = 1 ; i <= x ; i++)
            e[0] += e[i]*i ;
        printf("Case #%d: %.3lf\n", tt, e[0]) ;
    }
    return 0;
}

组合方法

和的所有结果可能出现的和除以总的种类。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
double c[10] ;
int a[10][10] ;
int main()
{
    int t , tt , x , b , n , i , j ;
    double ans , k ;
    a[0][0] = 1 ;
    for(i = 1 ; i <= 5 ; i++)
    {
        a[i][1] = 1 ;
        a[i][i] = a[i-1][i-1] * i ;
    }
    a[3][2] = 6 ;
    a[4][2] = 14 ; a[4][3] = 36 ;
    a[5][2] = 30 ; a[5][3] = 150 ; a[5][4] = 240 ;
    scanf("%d", &t) ;
    for(tt = 1 ; tt <= t ; tt++)
    {
        ans = 0 ;
        scanf("%d %d", &x, &b) ;
        n = min(x,b) ;
        c[0] = 1 ;
        for(i = 1 ; i <= n-1 ; i++)
        {
            for(j = 1 , k = 1 ; j <= i ; j++)
            {
                k *= ((x-1)-j+1)*1.0/j ;
            }
            if( i >= 2 )
                c[i] = k/(x*1.0*x) ;
            else
                c[i] = k ;
        }
        for(i = 1 ; i <= n ; i++)
        {
            //printf("c -- %lf %lf\n", c[i-1], ((x+1)*1.0*x/2.0) * a[b][i]) ;
            k = c[i-1] * 1.0 * ((x+1)*1.0*x/2.0) * a[b][i] ;
            //printf("%lf\n", k) ;
            int m = b ;
            if( i >= 3 )
                m = b-2 ;
            for(j = 1 ; j <= m ; j++)
                k /= (x*1.0) ;
            //printf("%lf\n", k) ;
            ans += k ;
        }
        printf("Case #%d: %.3lf\n", tt, ans) ;
    }
    return 0;
}
时间: 2024-08-14 19:37:21

HDU5159--BC--Card(概率写法,和组合写法)的相关文章

【C++】智能指针的原始写法、scoped写法、shared写法

智能指针的三种常见写法: 一.最开始的原始写法,原始写法可以理解为指针转移的方法. template<typename T> class AutoPtr { public:     AutoPtr()         :_ptr(NULL)     {}     AutoPtr(T* ptr)         :_ptr(ptr)     {}     ~AutoPtr()     {         if (_ptr)         {             delete _ptr;  

【C语言】【面试题】C++中String类浅拷贝,深拷贝的传统写法与现代写法

C++ 的一个常见面试题是让你实现一个 String 类,在这我把String类的各种写法写了一下 1.浅拷贝 #define _CRT_SECURE_NO_WARNINGS 1 #include<iostream> using namespace std; //1.浅拷贝 class String { public:     String(char* str)         :_str(str)     {}     String(const String& s)         

【C语言】【面试题】C++中String类引用计数器的浅拷贝写法与深拷贝写法

Linux操作下String类的实现--引用计数器 1.引用计数器写法一 写法一个人比较喜欢叫他双指针法,因为他是在类里面创建了两个指针来实现的一个是指针_str,另外一个是用来保存指向同一块空间个数的指针_pRefCount. class String { public:     String(char* str = "")         :_str(new char[strlen(str) + 1])         , _pRefCount(new int(1))     {

javacc jjtree 写法 以及 jj写法 基本语法 以及应用

javacc jjtree 写法 以及 jj写法 基本语法 以及应用 2012-2-7阅读2279 评论0 /***********************************************************/ >我使用的测试jjt,jj文件来自于javacc5.0版本 >dir_hier/javacc-5.0/javacc-5.0/examples/ JJTreeExamples SimpleExamples /******************************

springboot-yml内list、map组合写法

yml:myProps: varmaplist: key11: - t1 - t2 - t3 key22: - t11 - t22 - t33 list: - topic1 - topic2 - topic3 maps: {key1: 'value1', key2: 'value2'} MyProps: @Component@Data@Configuration@PropertySource(value = {"classpath:/bootstrap.yml"}, encoding

sql查询一天内的where写法,sql写法

sql查询一天内的写法: 1. where createtime BETWEEN (select date_format(now(),'%Y-%m-%d 00:00:00')) and (select date_format(now(),'%Y-%m-%d 23:59:59')) 2. SELECT TO_DAYS(now());SELECT TO_DAYS('2016-07-02 20:50:06'); ========================================附录===

设计模式:单例模式的写法(基础写法和线程安全写法)

单例模式的写法非常多.先给出一种最基础的写法: (A种写法): package singleton; public class SingletonInstance { private static SingletonInstance mSingletonInstance = null; // 特意将构造函数设置成 private,防止外部使用者new SingletonInstance(). private SingletonInstance() { } public static Single

背景图片等比缩放的写法background-size简写法

1.背景图片或图标也可像img一样给其宽高就能指定其缩放大小了. 比如一个实际宽高36*28的图标,要缩小一半引用进来的写法就是: background:rgba(0, 0, 0, 0) url("../images/[email protected]") no-repeat scroll left center / 18px 14px;      //left center是图标定位,/后面的18px 14px是指定背景图标应用的大小,原图标大小是36px 28px 这样指定大小为1

ajax的jquery写法和原生写法

一.ajax的简介 Ajax被认为是(Asynchronous(异步) JavaScript And Xml的缩写).现在,允许浏览器与服务器通信而无须刷新当前页面的技术都被叫做Ajax. 同步是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式. 异步是指:发送方发出数据后,不等接收方发回响应,接着发送下个数据包的通讯方式 . 二.ajax的缺陷 AJAX大量使用了JavaScript和AJAX引擎,而这个取决于浏览器的支持.IE5.0及以上.Mozilla1.0.NetSc