Dogs and Cages(數學 排列)

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 \leq T \leq 10^5$ 
$1 \leq N \leq 10^5$

 

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.
         

題解:題意是找出所有沒有進入對的盒子的總數除以總共排列的種數。如果1進入對的盒子,那n-1

代碼:

#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    cin>>t;
    int k=1;
    while(t--)
    {
        int n;
        cin>>n;
        cout<<"Case #"<<k<<": "<<n-1<<".0000000000"<<endl;
        k++;
    }
    return 0;
}

 

發佈了65 篇原創文章 · 獲贊 3 · 訪問量 9225
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章