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,...,N−1

. He also has N

cages numbered 0,1,...,N−1

. 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.
1≤T≤105

1≤N≤105

 
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
10−6

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 = ;
double n;
int T; int main() {
scanf("%d", &T);
int k = ;
while (T--) {
k++;
scanf("%lf",&n);
printf("Case #%d: %.10f\n",k,n-);
}
return ;
}

poj 6243 Dogs and Cages的更多相关文章

  1. A - Dogs and Cages HDU - 6243(组合数学)

    题意:在1—n的数字,放入编号为1—n的框中,每个框只放一个数字,问数字与所放的框的编号不同的个数的期望值. 思路:在1—n中任选一个数字,设为k 那么 k 排到非k编号的框中的方案数为 n!-(n- ...

  2. Hdu-6243 2017CCPC-Final A.Dogs and Cages 数学

    题面 题意:问1~n的所有的排列组合中那些,所有数字 i 不在第 i 位的个数之和除以n的全排,即题目所说的期望,比如n=3 排列有123(0),132(2),231(3),213(2),312(3) ...

  3. CCPC 2017-2018, Finals Solution

    A - Dogs and Cages 水. #include <bits/stdc++.h> using namespace std; int t; double n; int main( ...

  4. 模拟赛小结:2017 China Collegiate Programming Contest Final (CCPC-Final 2017)

    比赛链接:传送门 前期大顺风,2:30金区中游.后期开题乏力,掉到银尾.4:59绝杀I,但罚时太高卡在银首. Problem A - Dogs and Cages 00:09:45 (+) Solve ...

  5. poj 2761 Feed the dogs (treap树)

    /************************************************************* 题目: Feed the dogs(poj 2761) 链接: http: ...

  6. 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 ...

  7. POJ 题目2761 Feed the dogs(主席树||划分树)

    Feed the dogs Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 16860   Accepted: 5273 De ...

  8. POJ 2761 Feed the dogs (主席树)(K-th 值)

                                                                Feed the dogs Time Limit: 6000MS   Memor ...

  9. POJ 2761 Feed the dogs

    主席树,区间第$k$大. #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> ...

随机推荐

  1. sort 与 sorted 区别:

    sort 与 sorted 区别: sort 是应用在 list 上的方法,sorted 可以对所有可迭代的对象进行排序操作. list 的 sort 方法返回的是对已经存在的列表进行操作,无返回值, ...

  2. 01_6_SERVLET如何从上一个页面取得参数

    01_6_SERVLET如何从上一个页面取得参数 1. sevlet实现 public void doGet(HttpServletRequest request, HttpServletRespon ...

  3. iOS中的数据存储方式_Preference(NSUserDefaults)

    NSUserDefaults适合存储轻量级的本地数据,项目中,我会把一些简单的数据密码.网址.登陆状态BOOL.整型/浮点型数据等和用户有关的数据用它存储.但是它不能存储自定义的对象! 实例化一个 N ...

  4. matlplotlib绘图(二)

    matplotlib基础知识 matpltlib中的基本图表包括的元素 1.x轴和y轴:水平和垂直的轴线 2.x轴和y轴的刻度:刻度标识坐标值的分隔,包括最小刻度和最大刻度 3.x轴和y轴刻度:表示特 ...

  5. Linux常用快捷键以及如何查看命令帮助

    1.1    Linux系统快速操作常用快捷键 快捷键名称 快捷作用 Ctrl + a 将光标移至行首 Ctrl + e 将光标移至行尾 Ctrl + u 前提光标在行尾,则清除当前行所有的内容(有空 ...

  6. Lecture 2

    1. Coordinate(坐标) data for GIS real coordinate system:Cartesian coordinate systems(笛卡尔坐标系) from 3D t ...

  7. HDU 5111 Alexandra and Two Trees 树链剖分 + 主席树

    题意: 给出两棵树,每棵树的节点都有一个权值. 同一棵树上的节点的权值互不相同,不同树上节点的权值可以相同. 要求回答如下询问: \(u_1 \, v_1 \, u_2 \, v_2\):询问第一棵树 ...

  8. Python学习-day4

    学习装饰器,首先听haifeng老师讲解了一下准备知识. 1.函数即变量 2.高阶函数+嵌套函数==>装饰器 装饰器的作用是在,1)不改变源代码,2)不改变原函数的调用方式的前提下为函数增加新的 ...

  9. Leetcode 474.一和零

    一和零 在计算机界中,我们总是追求用有限的资源获取最大的收益. 现在,假设你分别支配着 m 个 0 和 n 个 1.另外,还有一个仅包含 0 和 1 字符串的数组. 你的任务是使用给定的 m 个 0  ...

  10. 算法理论——PLA

    全称 perceptron learning algrithm 用武之地 二值分类问题,资料线性可分 算法核心(以二维平面为例) 找到一条直线WTX=0,一边全为+1,另一边全为-1.找到了这条线(即 ...