HDU_5339_Untitled

HDU 5339

题意:在 b1,…,bn利任选序列 c1,…,cr使得a mod c1 mod c2 mod… mod cr=0 (即a能被分解为k1*c1 + k2*c2 +...+ kr*cr),求满足条件的序列的最小长度,不满足条件输出-1
思路:先将序列b排序,然后从大到小(小数可能是大数的因子)dfs即可。

#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <deque>
#include <queue>
#include <vector>
#include <algorithm>
#include <functional>

#define debug(x) cout << "--------------> " << x << endl

using namespace std;

const double PI = acos(-1.0);
const double eps = 1e-10;
const long long INF = 0x7fffffff;
const long long MOD = 1000000007;
const int MAXN = 20 + 7;

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

void dfs(int pos, int x, int r)   //递归的位置,递归的值,满足条件的长度 
{
    if(x == 0) ans = ans < r ? ans : r;
    if(pos == -1) return ;
    dfs(pos - 1, x, r);
    dfs(pos - 1, x % b[pos], r + 1);
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        memset(b, 0, sizeof(b));
        scanf("%d%d", &n, &a);
        for(int i = 0; i < n; ++i)
        {
            scanf("%d", &b[i]);
        }
        ans = 30;
        sort(b, b+n);
        dfs(n-1, a, 0);
        ans < 30 ? printf("%d\n", ans) : printf("-1\n");
    }
     return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值