zoukankan      html  css  js  c++  java
  • hdu 5194(DFS)

    DZY Loves Balls

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 860    Accepted Submission(s): 467


    Problem Description
    There are n black balls and m white balls in the big box.

    Now, DZY starts to randomly pick out the balls one by one. It forms a sequence S. If at the i-th operation, DZY takes out the black ball, Si=1, otherwise Si=0.

    DZY wants to know the expected times that '01' occurs in S.
     
    Input
    The input consists several test cases. (TestCase150)

    The first line contains two integers, n, m(1n,m12)
     
    Output
    For each case, output the corresponding result, the format is p/q(p and q are coprime)
     
    Sample Input
    1 1 2 3
     
    Sample Output
    1/2 6/5
    Hint
    Case 1: S='01' or S='10', so the expected times = 1/2 = 1/2 Case 2: S='00011' or S='00101' or S='00110' or S='01001' or S='01010' or S='01100' or S='10001' or S='10010' or S='10100' or S='11000', so the expected times = (1+2+1+2+2+1+1+1+1+0)/10 = 12/10 = 6/5
     
    Source
     
    题解:特判了下12 12以及 11 12 和 12 11..其余的话暴力枚举就行了..
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include <algorithm>
    #include <math.h>
    using namespace std;
    typedef long long LL;
    int n,m,p,q;
    int ans[30];
    void dfs(int step,int a,int b){
        if(a>m||b>n) return; ///剪枝,不然TLE
        if(a==m&&b==n){
            q++;
            int k = 0;
            for(int i=0;i<n+m-1;i++){
                if(ans[i]==0&&ans[i+1]==1){
                    p++;
                }
            }
            return;
        }
        for(int i=0;i<=1;i++){
            if(a<=m&&i==0){
                ans[step] = i;
                dfs(step+1,a+1,b);
            }
            if(b<=n&&i==1){
                ans[step] = i;
                dfs(step+1,a,b+1);
            }
        }
    }
    int gcd(int a,int b){
        return b==0?a:gcd(b,a%b);
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF){
            if(n==12&&m==12){
                printf("6/1
    ");
                continue;
            }
            if(n==12&&m==11||n==11&&m==12){
                printf("132/23
    ");
                continue;
            }
            p = q = 0;
            dfs(0,0,0);
            int d = gcd(p,q);
            printf("%d/%d
    ",p/d,q/d);
        }
        return 0;
    }

     改成了标记前结点,快了许多。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include <algorithm>
    #include <math.h>
    using namespace std;
    typedef long long LL;
    int n,m,p,q;
    void dfs(int a,int b,int pre,int num){
        if(a>n||b>m) return;
        if(a==n&&b==m){
            p+=num;
            q++;
            return;
        }
        if(b<=m) dfs(a,b+1,0,num);
        if(a<=n){
            if(pre==0) dfs(a+1,b,1,num+1);
            else dfs(a+1,b,1,num);
        }
    }
    int gcd(int a,int b){
        return b==0?a:gcd(b,a%b);
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF){
            p = q = 0;
            dfs(0,0,-1,0);
            int d = gcd(p,q);
            printf("%d/%d
    ",p/d,q/d);
        }
        return 0;
    }
  • 相关阅读:
    作为一个蒟蒻谈一点考试经验(总结)
    Codeforces Round #517 Div. 2/Div. 1
    Codeforces访问提速攻略(小技巧)
    基数排序模板(基数排序,C++模板)
    k短路模板(洛谷P2483 [SDOI2010]魔法猪学院)(k短路,最短路,左偏树,priority_queue)
    洛谷P4907【CYH-01】小奔的国庆练习赛 :$A$换$B$ $problem$(DFS,剪枝)
    BSGS及扩展BSGS总结(BSGS,map)
    FWT模板(洛谷P4717 【模板】快速沃尔什变换)(FWT)
    洛谷CF264D Colorful Stones(子序列匹配,思维)
    洛谷SP22343 NORMA2
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5702724.html
Copyright © 2011-2022 走看看