BestCoder #49 Untitled HDU 5339



BestCoder #49 Untitled  HDU 5339

题目: http://acm.hdu.edu.cn/showproblem.php?pid=5339

本题采用深搜, 数据量小,先做排序处理(降序), 然后深搜的时候,进行剪枝,比K大的数就没必要往后搜索,直接剪掉。

#include <iostream>
#include <algorithm>
#include <cstdio>

using namespace std;

const int INF = 200;
int arr[20+5] = {};

void dfs(int t, int n, int k, int &ans)
{
    if (k==0)
    {
        ans = min(ans, t);
        return;
    }

    if (t < n)
    {
        if (k%arr[t] == 0)
        {
            ans = min(ans, t+1);
        }
        else if (k > arr[t])    // 要做剪枝处理, 大于k 就没必要往后处理了
        {
            dfs(t+1, n, k%arr[t], ans);
            dfs(t+1, n, k, ans);
        }
    }
}

inline bool cmp(const int &a, const int &b)
{
    return a > b;
}

int main(void)
{
    //freopen("in.txt", "r", stdin);

    int t = 0;
    cin>>t;
    while(t--)
    {
        int n, k;
        cin>>n>>k;

        int zero = 0;      // 如果输入数据含有 k 的因子,那么结果肯定是 1
        int v = 0;
        int j = 0;
        for(int i=0; i<n; ++i)
        {
            scanf("%d", &v);

            if (k%v == 0)
                zero = 1;
            if (v < k)
                arr[j++] = v;
        }

        if (zero == 1)
        {
            printf("1\n");
            continue;
        }

        sort(arr, arr+j, cmp);      // order by DESC
        //printf("%d\n", arr[0]);

        int ans = INF;
        dfs(0, j, k, ans);
        printf("%d\n", ans==INF ? -1: ans);

    }

    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。 http://blog.csdn.net/core__code

时间: 2024-12-26 18:44:19

BestCoder #49 Untitled HDU 5339的相关文章

CodeForce Round#49 untitled (Hdu 5339)

Untitled Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 947    Accepted Submission(s): 538 Problem Description There is an integer a and n integers b1,…,bn. After selecting some numbers from b1

hdu 5339 Untitled(回溯)

hdu 5339 Untitled 题目大意:给出n个数字的序列,和一个数a,在n中有m个数b1,...,bm使得__a %b1%b2%...%bm = 0,求最小的m. 解题思路:回溯. #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> using namespace std; const int N = 5

Bestcoder round #65 &amp;&amp; hdu 5592 ZYB&#39;s Premutation 线段树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 74 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutat

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

[BestCoder Round #4] hdu 4932 Miaomiao&#39;s Geometry (贪心)

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 92 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by

[BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)

Task schedule Problem Description 有一台机器,并且给你这台机器的工作表,工作表上有n个任务,机器在ti时间执行第i个任务,1秒即可完成1个任务. 有m个询问,每个询问有一个数字q,表示如果在q时间有一个工作表之外的任务请求,请计算何时这个任务才能被执行. 机器总是按照工作表执行,当机器空闲时立即执行工作表之外的任务请求. Input 输入的第一行包含一个整数T, 表示一共有T组测试数据. 对于每组测试数据: 第一行是两个数字n, m,表示工作表里面有n个任务,

[BestCoder Round #4] hdu 4931 Happy Three Friends

Happy Three Friends Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 70    Accepted Submission(s): 62 Problem Description Dong-hao , Grandpa Shawn , Beautful-leg Mzry are good friends. One day ,

[BestCoder Round #5] hdu 4956 Poor Hanamichi (数学题)

Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 743    Accepted Submission(s): 275 Problem Description Hanamichi is taking part in a programming contest, and he is assigned to so