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


你可能感兴趣的:(数学,ACM,topcoder)