• poj 6243 Dogs and Cages


    Dogs and Cages

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 56    Accepted Submission(s): 40
    Special Judge


    Problem Description
    Jerry likes dogs. He has N dogs numbered 0,1,...,N1 . He also has N cages numbered 0,1,...,N1 . Everyday he takes all his dogs out and walks them outside. When he is back home, as dogs can’t recognize the numbers, each dog just randomly selects a cage and enters it. Each cage can hold only one dog.
    One day, Jerry noticed that some dogs were in the cage with the same number of themselves while others were not. Jerry would like to know what’s the expected number of dogs that are NOT in the cage with the same number of themselves.
     
    Input
    The first line of the input gives the number of test cases, T . T test cases follow.
    Each test case contains only one number N , indicating the number of dogs and cages.
    1T105
    1N105
     
    Output
    For each test case, output one line containing “Case #x: y”, where x is the test case number (starting from 1) and y is the expected number of dogs that are NOT in the cage with the same number of itself.
    y will be considered correct if it is within an absolute or relative error of 106 of the correct answer.
     
    Sample Input
    2 1 2
     
    Sample Output
    Case #1: 0.0000000000 Case #2: 1.0000000000
    Hint
    In the first test case, the only dog will enter the only cage. So the answer is 0. In the second test case, if the first dog enters the cage of the same number, both dogs are in the cage of the same number, the number of mismatch is 0. If both dogs are not in the cage with the same number of itself, the number of mismatch is 2. So the expected number is (0+2)/2=1.
     
    题意:随机产生的长度为 n 的排列 {Ai},求期望有多少位置满足 A[i] != i。

    思路:和的期望等于期望的和;可以考虑每个位置对总期望的贡献,每个位置满足A[i]!=i有n-1种情况,满足条件的期望为(n-1)/n;
    一共n个位置,故总期望为n-1;
    AC代码:
    #define _CRT_SECURE_NO_DEPRECATE
    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<algorithm>
    #include<cstring> 
    #include<string>
    #include<queue>
    #include<cmath>
    using namespace std;
    #define INF 0x3f3f3f3f
    typedef vector<double> vec;
    typedef vector<vec> mat;
    const int N_MAX = 5000;
    double n;
    int T;
    
    int main() {
        scanf("%d", &T);
        int k = 0;
        while (T--) {
            k++;
            scanf("%lf",&n);
            printf("Case #%d: %.10f
    ",k,n-1);
        }
        return 0;
    }
  • 相关阅读:
    [读书笔记]SQLSERVER企业级平台管理实践读书笔记02
    [工作相关] 一个虚拟机上面的SAP4HANA的简单使用维护
    vCenter机器查找功能不可用的解决
    [日常工作]GS使用安装盘修改密码后的处理
    [读书笔记]SQLSERVER企业级平台管理实践读书笔记01
    [书摘]架构真经--可扩展性规则的利益与优先级排行榜
    2017年总结2018年规划
    Windows 通过命令行设置固定ip地址
    ACDsee的安装过程
    [工作相关] GS产品使用LInux下Oracle数据库以及ASM存储时的数据文件路径写法.
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/8028392.html
Copyright © 2020-2023  润新知