Bestcoder round # 7-A, B, c

Source: Internet
Author: User

A: Little Pony and Permutation

Direct brute force search, complexity O (N)

#include<stdio.h>#include<iostream>#include<stdlib.h>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<queue>#include<stack>#include<map>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define maxn 110000int a[maxn];int vis[maxn];int main(){    int n;    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        memset(vis,0,sizeof(vis));        for(int i=1;i<=n;i++)        {            if(vis[i])continue;            int x=i;            cout<<"("<<x;            vis[x]=1;            x=a[x];            while(!vis[x])            {                vis[x]=1;                cout<<" "<<x;                x=a[x];            }            cout<<")";        }        cout<<endl;    }    return 0;}

B: Little Pony and Alohomora Part I

We can find that this is a harmonic sequence.

Then... That's all .. ,

#include<stdio.h>#include<iostream>#include<stdlib.h>#include<string.h>#include<algorithm>#include<vector>#include<math.h>#include<queue>#include<stack>#include<map>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define maxn 1100000double f[maxn];int main(){    int n;    f[1]=1.0;    for(int i=2;i<maxn;i++)f[i]=f[i-1]+1.0/i;    while(~scanf("%d",&n))    {        if(n<maxn)printf("%.4lf\n",f[n]);        else printf("%.4lf\n",log(n)+0.5772156649);    }    return 0;}

C: Little Pony and dice

DP [I]: probability of just going to point I

DP [I] = DP [I-1] * (1/m) + (DP [I-1]-DP [i-m-1] * (1.0/m ))

DP [I-1] * (1/m) represents the probability of moving from the point of I-1 to the point of I.

(DP [I-1]-DP [i-m-1] * (1.0/m) All points that walk to the I-1 can go to I, and the probability of going to I-1 is the same as that of going to I.

If the difference between two adjacent I dp [I] is very small, the change of the next DP [I] is also very small. This value will be used in the future. Therefore, direct output is good.

<pre name="code" class="cpp">#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<vector>#include<set>#include<string>#include<math.h>using namespace std;#define LL __int64#define maxn 703000#define eps 1e-13#define zero(x) (fabs(x)<eps?0:x)double dp[maxn];int main(){    int n,m;    while(~scanf("%d%d",&m,&n))    {        dp[0]=1.0;        dp[1]=1.0/m;        int i;        for(i=2;i<=n;i++)        {            dp[i]=dp[i-1]+dp[i-1]*1.0/m;            if(i>m)dp[i]=dp[i]-dp[i-m-1]*1.0/m;            if((zero(dp[i]-dp[i-1])==0))            {                printf("%.5lf\n",dp[i]);                break;            }        }        if(i>n)printf("%.5lf\n",dp[n]);    }    return 0;}


 












Bestcoder round # 7-A, B, c

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.