码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 5339 Untitled

时间:2015-08-01 23:40:05      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

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 b技术分享1技术分享,,b技术分享n技术分享技术分享. After selecting some numbers from b技术分享1技术分享,,b技术分享n技术分享技术分享 in any order, say c技术分享1技术分享,,c技术分享r技术分享技术分享, we want to make sure that a mod c技术分享1技术分享 mod c技术分享2技术分享 mod mod c技术分享r技术分享=0技术分享 (i.e., a技术分享 will become the remainder divided by c技术分享i技术分享技术分享 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,1a10技术分享6技术分享技术分享).

2. The second line contains n技术分享 integers b技术分享1技术分享,,b技术分享n技术分享技术分享 (?1in,1b技术分享i技术分享10技术分享6技术分享技术分享).
 

Output
Print T技术分享 answers in T技术分享 lines.
 

Sample Input
2 2 9 2 7 2 9 6 7
 

Sample Output
2 -1
 

对于一组可能的答案ccc,如果先对一个觉小的cic_ic?i??取模,再对一个较大的cjc_jc?j??取模,那么这个较大的cjc_jc?j??肯定是没有用的。因此最终的答案序列中的ccc肯定是不增的。那么就枚举选哪些数字,并从大到小取模看看结果是否是000就可以了。时间复杂度O(2n)O(2^n)O(2?n??).从小到大枚举,就可以了,。复杂度o(2^n)

#define N 205
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int T,n,a,pri[N],ans;
int Dfs(int x,int m,int num){
    if(m == 0){
        ans = min(ans,num);
        return 0;
    }
    for(int i = x - 1;i>=0;i--){
        if(m >= pri[i])
            Dfs(i,m%pri[i],num+1);
    }
}
int main()
{
    while(S(T)!=EOF)
    {
        while(T--){
            S2(n,a);
            FI(n) S(pri[i]);
            sort(pri,pri+n);
            ans = INF;
            Dfs(n,a,0);
            if(ans == INF) ans = -1;
            printf("%d\n",ans);
        }
    }
    return 0;
}


Source
 

Recommend
hujie   |   We have carefully selected several similar problems for you:  5342 5341 5339 5338 5337 
 

Statistic | Submit | Discuss | Note

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

hdu 5339 Untitled

标签:

原文地址:http://blog.csdn.net/mengzhengnan/article/details/47191085

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!