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 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
T≤5,
which represents the number of testcases.

For each testcase, there are two lines:

1. The first line contains two integers n
and a
(1≤n≤20,1≤a≤106).

2. The second line contains n
integers b1,…,bn
(?1≤i≤n,1≤bi≤106).

Output

Print
T
answers in T
lines.

Sample Input

2
2 9
2 7
2 9
6 7

Sample Output

2
-1

Source

BestCoder Round #49 ($)

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

题目大意:重排列bi,问a对重排列的数不断取模最快能为0的取模次数

题目分析:其实是水题,首先对数字小的取完余后再对数字大的取余等于没取,所以先对大数字取余,从大到小排序,因为n很小,DFS随意搜,也可以用状态压缩做,把每个数字选或不选的状态二进制压缩

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 1 << 22;
int const INF = 0x3fffffff;
int b[25], sta[MAX];

int lowbit(int x)
{
    return x & (-x);
}

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

int main()
{
    int T;
    scanf("%d", &T);
    while(T --)
    {
        memset(sta, 0, sizeof(sta));
        int n, a;
        scanf("%d %d", &n, &a);
        for(int i = 1; i <= n; i++)
            scanf("%d", &b[i]);
        sort(b + 1, b + n + 1, cmp);
        for(int i = 1; i <= n; i++)
            sta[1 << (i - 1)] = b[i];
        int cnt, ans = INF;
        for(int i = 1; i < (1 << n); i++)
        {
            int tmp = a;
            cnt = 0;
            for(int j = i; j > 0; j -= lowbit(j))
            {
                tmp %= sta[lowbit(j)];
                cnt ++;
            }
            if(tmp == 0)
                ans = min(ans, cnt);
        }
        if(ans == INF)
            printf("-1\n");
        else
            printf("%d\n", ans);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-15 07:13:39

HDU 5339 Untitled (状态压缩枚举)的相关文章

hdu 4568(状态压缩dp)

题意:一张n*m的网格内每个点有话费,还有若干个宝藏,问一个人要走进去拿走所有宝藏在走出来的最小花费. 思路:看宝藏只有13个直接想到了状压dp[i][j]拿了哪几个前一个为j的最小花费,先bfs+优先队列预处理出最短路,然后记忆化搜索就可. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao

zoj2977Strange Billboard (状态压缩+枚举)

Strange Billboard Time Limit: 2 Seconds Memory Limit: 65536 KB The marketing and public-relations department of the Czech Technical University has designed a new reconfigurable mechanical Flip-Flop Bill-Board (FFBB). The billboard is a regular two-di

codeforces B - Preparing Olympiad(dfs或者状态压缩枚举)

B. Preparing Olympiad You have n problems. You have estimated the difficulty of the i-th one as integer ci. Now you want to prepare a problemset for a contest, using some of the problems you've made. A problemset for the contest must consist of at le

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

状态压缩+枚举 POJ 3279 Fliptile

题目传送门 1 /* 2 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 3 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 const int MAXN = 20; 11 const int INF = 0x3f3f3f3

UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)

题目链接:UVA 811 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees from thieves, the king or

HDU 6607 Time To Get Up(状态压缩+枚举)

题目网址: http://acm.hdu.edu.cn/showproblem.php?pid=6077 思路: 先预处理一下,将每个数字块的"X"看作1,"."看作0,进行状态压缩转换成二进制数,用数组保存.再遍历每个块点的元素,枚举0-9看是否符合当前位数. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 typedef long long l

hdu 1429(BFS+状态压缩)

胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7895    Accepted Submission(s): 2795 Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这 次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢的某些地方安装了

hdu 3681 二分+状态压缩dp+bfs

题意就不用多说了,开始想直接bfs+二分简化下做试试   果断超时,然后不得不用状态压缩dp(其实一开始就想这样做的,15个点   每个点都能走多次   绝逼状态压缩dp) 先bfs跑每个关键点之间的做短路径    再二分起点的能量值               状态压缩dp来判断     不说了    AC吧!!!!! #include<stdio.h> #include<string.h> #include<queue> #include<iostream&g