hdu 5339 Untitled【搜索】

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

题意:一个整数a 和一个数组b。问你能否在b中取出r个元素排列组成c数组满足a%c1%c1%…..%cr == 0。输出最小的r,不能满足条件输出-1。

思路:b按从大到小排序,暴搜。

代码:

#include <stdio.h>
#include <ctime>
#include <math.h>
#include <limits.h>
#include <complex>
#include <string>
#include <functional>
#include <iterator>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <list>
#include <bitset>
#include <sstream>
#include <iomanip>
#include <fstream>
#include <iostream>
#include <ctime>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <time.h>
#include <ctype.h>
#include <string.h>
#include <string>
#include <assert.h>

using namespace std;

int n, a, b[300],ans;

bool cmp11(int a, int b)
{
    return a > b;
}

void dfs(int x,int num,int A)
{
    if (x == 0)
    {
        ans = min(ans, A); return;
    }
    if (num == n) return;

    //printf("    %d\n",x);
    dfs(x % b[num + 1], num + 1, A + 1);
    dfs(x, num + 1, A);
}

int main()
{
    //printf("3 % 7");
    int t;
    scanf("%d",&t);
    while (t--)
    {
        scanf("%d %d", &n, &a);
        for (int i = 1; i <= n; i++)
            scanf("%d", &b[i]);
        sort(b + 1, b + 1 + n, cmp11);
        ans = 500;
        dfs(a, 0, 0);
        if (ans == 500) ans = -1;
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-10-31 07:25:55

hdu 5339 Untitled【搜索】的相关文章

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

HDU 5339 Untitled(暴搜)

Untitled Problem Description There is an integer $a$ and $n$ integers $b_1, \ldots, b_n$. After selecting some numbers from $b_1, \ldots, b_n$ in any order, say $c_1, \ldots, c_r$, we want to make sure that $a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ m

HDU 5339 Untitled (状态压缩枚举)

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

hdu 5339 Untitled

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

Hdu 5339 Untitled (数学思维)

题意:给一个数a和n个数b1,b2,...,bn. 从n个数中选择一些数重新排列成c1,c2,...,cm使得a%c1%c2%...%cm=0. 如果能选出则输出最少需要几个数,否则输出-1. 分析: 首先很重要的一点就是 “一定是先除大的数再除小的数” 因为如果先除小的数x,则取模的数一定是在0到x-1中,再除大于x的数是没意义的. 即选出的一列数是非递增序列. 由于n最多只有20个,简单的2^n的搜索即可搞定了. 通过这题发现hdu oj的一个bug 就是排序的时候不能手写整数的cmp使其逆

HDU 5339 Untitled (递归穷举)

题意:给定一个序列,要求从这个序列中挑出k个数字,使得n%a1%a2%a3....=0(顺序随你意).求k的最小值. 思路:排个序,从大的数开始模起,这是因为小的模完还能模大的么? 每个元素可以选,也可以不选,两种情况.递归穷举每个可能性,O(2n). 1 //#include <bits/stdc++.h> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 #include <al

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,

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 =

HDU 2828 DLX搜索

Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 771    Accepted Submission(s): 230 Special Judge Problem Description There are several switches and lamps in the room, however, the connecti