zoukankan      html  css  js  c++  java
  • hdu 5339 Untitled dfs

    Problem Description
    There is an integer a and n integers b1,,bn. After selecting some numbers from b1,,bn in any order, say c1,,cr, we want to make sure that a mod c1 mod c2 mod mod cr=0 (i.e., a will become the remainder divided by ci each time, and at the end, we want a to become 0). Please determine the minimum value of r. If the goal cannot be achieved, print 1 instead.
     
    Input
    The first line contains one integer T5, which represents the number of testcases. 
    For each testcase, there are two lines:
    1. The first line contains two integers n and a (1n20,1a106).
    2. The second line contains n integers b1,,bn (1in,1bi106).
     
    Output
    Print T answers in T lines.
     
    Sample Input
    2
    2 9
    2 7
    2 9
    6 7
     
    Sample Output
    2
    -1

        dfs题:不过这题很容易超时。要注意剪枝。剪枝地方我会在代码中说明,看代码就可以了。

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int n,b[30],r,mn;
    int v[30];
    void dfs(int k,int a)
    {
        int i,s;
        for (i=1;i<=n;i++)
        {
            if (!v[i])
            {
                s=a%b[i];
                if (s==0) {r=min(r,k+1);return ;}
                if (s<mn) continue; //剪枝。如果s比这些数中最小值还小,那么s就不可能为0.
                v[i]=1;
                dfs(k+1,s);
                v[i]=0;
            }
        }
        return ;
    }
    int main()
    {
        int t,i,a,c,d;
        scanf("%d",&t);
        while (t--)
        {
            r=30;
            scanf("%d%d",&n,&a);
            c=n;
            i=1;
            mn=1000000;
            while (c--)
            {
                scanf("%d",&d);
                if (d>a) continue; //剪枝。a模比a大的数还是a,就可以不用管。
                if (a%d==0) r=1;  //剪枝。如果a是这些数中一些数的倍数,那么最小值就是一。
                b[i++]=d;
                mn=min(mn,d);
            }
            n=i-1;
            sort(b+1,b+i);
            if (r==1) {printf("1
    ");continue;}
            memset(v,0,sizeof(v));
            dfs(0,a);
            if (r==30) printf("-1
    ");
            else printf("%d
    ",r);
        }
    }
    
  • 相关阅读:
    BZOJ.1028.[JSOI2007]麻将(贪心)
    BZOJ.1024.[SCOI2009]生日快乐(记忆化搜索)
    BZOJ.1023.[SHOI2008]cactus仙人掌图(DP)
    BZOJ.1026.[SCOI2009]windy数(数位DP)
    BZOJ.2125.最短路(仙人掌 最短路Dijkstra)
    BZOJ.1021.[SHOI2008]循环的债务(DP)
    BZOJ.1019.[SHOI2008]汉诺塔(递推)
    POJ.1379.Run Away(模拟退火)
    BZOJ.3680.吊打XXX(模拟退火/爬山算法)
    BZOJ.1018.[SHOI2008]堵塞的交通(线段树维护连通性)
  • 原文地址:https://www.cnblogs.com/pblr/p/4714612.html
Copyright © 2011-2022 走看看