Topcoder SRM 660 DIV1 500 Privateparty(數學)

題意:N個參加聚會,和一個數組a,ai表示第i個人討厭的人,如果一個到聚會門口的時候發現他討厭的人已經在聚會里面,則他不會參加聚會,否則他會參加聚會。ai==i表示他沒有討厭的人。N個人來的先後順序是任意的,也就是說n個來的先後順序構成的1到n的排列是任意的。問參加聚會的人的期望是多少?

題解:分開來計算每個人來參加聚會的期望,n個人來參加聚會的期望的和就是最後的答案。那麼現在的問題是如何計算一個人來參加聚會的概率?

對於第i個人是否來參加聚會,受到一些人的影響,他們是:w1,w2,w3....wk。其中w1==i,w1討厭w2,w2討厭w3,...wk討厭w1到wk-1的某個人。

第i個人參加聚會的概率爲:

+包含集合w1的概率

-包含集合(w2,w1),(w2比w1先來)的概率:

+包含集合(w3,w2,w1),(w3比w2先來,w2比w1先來)的概率

.................

.................

+/-包含集合(wk,....w1)

畫個圖來說明:


黃色部分所佔的概率就是我們要求的答案。

代碼如下:

// BEGIN CUT HERE
// END CUT HERE
#line 4 "Privateparty.cpp"
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <sstream>
#define OUT(x) cout << #x << ": " << (x) << endl
#define SZ(x) ((int)x.size())
#define FOR(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long LL;
bool vis[55];
class Privateparty
{
public:
    double getexp(vector <int> a)
    {
        int n=a.size();
        double ans=0;
        int i,j;
        for(i=0;i<n;i++)
        {
            memset(vis,false,sizeof(vis));
            int now=i;
            int ix=0;
            while(!vis[now])
            {
                ix++;
                vis[now]=true;
                now=a[now];
            }
            double p=1;
            double fc=1;
            for(j=2;j<=ix;j++)
            {
                fc*=1.0/j;
                if(j&1)
                    p+=fc;
                else
                    p-=fc;
            }
            ans+=p;
        }
        return ans;
    }

// BEGIN CUT HERE
	public:
	void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
	private:
	template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
	void verify_case(int Case, const double &Expected, const double &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
	void test_case_0() { int Arr0[] = {0,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 2.0; verify_case(0, Arg1, getexp(Arg0)); }
	void test_case_1() { int Arr0[] = {0,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 1.5; verify_case(1, Arg1, getexp(Arg0)); }
	void test_case_2() { int Arr0[] = {0,1,1}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 2.5; verify_case(2, Arg1, getexp(Arg0)); }
	void test_case_3() { int Arr0[] = {0,1,1,2}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 3.166666666666667; verify_case(3, Arg1, getexp(Arg0)); }
	void test_case_4() { int Arr0[] = {3,2,1,0}; vector <int> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); double Arg1 = 2.0; verify_case(4, Arg1, getexp(Arg0)); }

// END CUT HERE

};
// BEGIN CUT HERE
int main()
{
    Privateparty ___test;
    ___test.run_test(-1);
    system("pause");
    return 0;
}
// END CUT HERE


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章