[BestCoder] Round #7

1001

http://acm.hdu.edu.cn/showproblem.php?pid=4985

输出循环节

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
#define rd(x) scanf("%d",&x)
#define rd2(x,y)  scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
int dx[8]={0,0,-1,1,1,1,-1,-1};
int dy[8]={1,-1,0,0,-1,1,-1,1};
const int maxn=1e5+2;
bool vis[maxn];
int p[maxn];
int n;

int main()
{
    while(rd(n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            rd(p[i]);
        memset(vis,0,sizeof(vis));
        int position;
        bool first;
        for(int i=1;i<=n;i++)
        {
            position=i;
            first=1;
            while(!vis[position])
            {
                vis[position]=1;
                if(first)
                {
                    cout<<"("<<position;
                    first=0;
                }
                else
                    cout<<" "<<position;
                position=p[position];
                if(vis[position])
                    cout<<")";

            }
        }
        cout<<endl;
    }
    return 0;
}

1002

http://acm.hdu.edu.cn/showproblem.php?pid=4986

题意为在n个盒子里随机放着一个钥匙,且该钥匙和某一个盒子相对应,用1个魔法可以打开一个盒子,问把这些盒子全部打开,使用魔法个数的期望.

设p[i]为打开i个盒子的期望,那么另外一个盒要么是单独的一个循环节,那么插入到前面中去。p[i]=  (p[i-1]+1)/i + p[i-1]  *(i-1)/i  得p[i]=p[i-1]+ 1/i

也就是p[n]=1+ 1/2 +1/3 + .....1/n

这个数组是发散的,所以没有求和公式,只有一个近似的求解方法:
   1+1/2+1/3+......+1/n ≈ lnn+C(C=0.57722......一个无理数,称作欧拉初始,专为调和级数所用)当n很大时,有:1+1/2+1/3+1/4+1/5+1/6+...1/n = 0.57721566490153286060651209 + ln(n)//C++里面用log(n),pascal里面用ln(n)  
0.57721566490153286060651209叫做欧拉常数  

#include <iostream>
#include <stdio.h>
#include <cmath>
#include <map>
#include <stack>
#include <queue>
#include <cctype>
#define rd(x) scanf("%d",&x)
#define rd2(x,y)  scanf("%d%d",&x,&y)
#define rd3(x,y,z) scanf("%d%d%d",&x,&y,&z)
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const double C=0.57721566490153286060651209;//欧拉常数
const int maxn=100000;
double p[maxn];
int n;

void init()
{
    p[0]=0;
    for(int i=1;i<=maxn;i++)
        p[i]=p[i-1]+(1.0/i);
}

int main()
{
    init();
    while(rd(n)!=EOF)
    {
        if(n<=maxn)
            printf("%.4lf\n",p[n]);
        else
            printf("%.4lf\n",log(n)+C);

    }
    return 0;
}



你可能感兴趣的:([BestCoder] Round #7)