zoukankan      html  css  js  c++  java
  • HDU 5339 Untitled(暴搜)

                     Untitled



     

    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

     
     
     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 
     5 int ans,m,n;
     6 int b[21];
     7 
     8 bool cmp(int a,int b)
     9 {
    10     return a>b;
    11 }
    12 
    13 void dfs(int total,int cur,int num)
    14 {
    15     if(total==0)
    16     {
    17         ans=min(ans,num);
    18         return;
    19     }
    20     if(cur==m)
    21     return;
    22     dfs(total%b[cur],cur+1,num+1);
    23     dfs(total,cur+1,num);
    24 }
    25 
    26 int main()
    27 {
    28     //freopen("in.txt","r",stdin);
    29     int i,t;
    30     scanf("%d",&t);
    31     while(t--)
    32     {
    33         ans=21;
    34         scanf("%d%d",&m,&n);
    35         for(i=0;i<m;i++)
    36         scanf("%d",&b[i]);
    37         sort(b,b+m,cmp);
    38         dfs(n,0,0);
    39         if(ans==21)
    40         printf("-1
    ");
    41         else
    42         printf("%d
    ",ans);
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    中文字体网页开发指南
    使用免费模板需要注意的几个问题
    bootstrap总结
    HTML5 新模块元素兼容问题
    Oracle EBS 系统仅存在英文的环境
    Oracle EBS INV 更新物料慢
    Oracle EBS 跳跳转标准销售订单程序转标准销售订单程序
    Oracle EBS 数据访问权限集
    Oracle 数据库执行慢SQL
    Oracle EBS AR 事务处理到期余额总计API
  • 原文地址:https://www.cnblogs.com/homura/p/4711105.html
Copyright © 2011-2022 走看看