首页 > 其他分享 >HDU 5339 Untitled

HDU 5339 Untitled

时间:2022-11-09 20:38:57浏览次数:49  
标签:HDU return int dfs Untitled ans 5339 include mod


Problem Description

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


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


T answers in  T lines.


 


Sample Input


2
2 9
2 7
2 9
6 7



Sample Output


2
-1


暴力枚举就好了


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
const int maxn = 100005;
int T, n, m, a[maxn], ans;

void dfs(int x, int y, int z)
{
if (!y) {
if (ans < 0) ans = z;
else ans = min(ans, z);
return;
}
if (!x) return;
if (ans>=0&&z>=ans) return;
dfs(x - 1, y%a[x], z + 1);
dfs(x - 1, y, z);
}

int main()
{
cin >> T;
while (T--)
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
sort(a + 1, a + n + 1);
ans = -1;
dfs(n, m, 0);
printf("%d\n", ans);
}
return 0;
}


标签:HDU,return,int,dfs,Untitled,ans,5339,include,mod
From: https://blog.51cto.com/u_15870896/5838722

相关文章

  • HDU 3760 Ideal Path
    ProblemDescriptionNewlabyrinthattractionisopeninNewLostlandamusementpark.Thelabyrinthconsistsofnroomsconnectedbympassages.Eachpass......
  • HDU 4101 Ali and Baba
    ProblemDescriptionThereisarectanglearea(withNrowsandMcolumns)infrontofAliandBaba,eachgridmightbeoneofthefollowing:1.Empty......
  • HDU 4198 Quick out of the Harbour
    ProblemDescriptionCaptainClearbearddecidedtogototheharbourforafewdayssohiscrewcouldinspectandrepairtheship.Now,afewdayslater,......
  • HDU 2589 正方形划分
    ProblemDescription一个边长为L的正方形可以分成L*L个小正方形.有N个石子放在N个小正方形里,能否将它分成N个正方形,使得每个正方形里恰有一个石子且这N个正方......
  • HDU 3085 Nightmare Ⅱ
    ProblemDescriptionLastnight,littleerriyuehadahorriblenightmare.Hedreamedthatheandhisgirlfriendweretrappedinabigmazeseparately.Mo......
  • HDU 3313 Key Vertex
    ProblemDescriptionYouneedwalkingfromvertexStovertexTinagraph.IfyouremoveonevertexwhichstopsyoufromwalkingfromStoT,thatverte......
  • HDU 3316 Mine sweeping
    ProblemDescriptionIthinkmostofyouareusingsystemnamedofxporvistaorwin7.Andthesesystemisconsistofafamousgamewhatisminesweeping......
  • HDU 3355 Hop — Don’t Walk!
    ProblemDescriptionKERMITTHEFROGisaclassicvideogamewithasimplecontrolandobjectivebutrequiresagooddealofthinking.Youcontrolanani......
  • HDU 3427 Clickomania
    ProblemDescriptionClickomaniaisapuzzleinwhichonestartswitharectangulargridofcellsofdifferentcolours.Ineachstep,aplayerselects("......
  • HDU 2209 翻纸牌游戏
    ProblemDescription有一种纸牌游戏,很有意思,给你N张纸牌,一字排开,纸牌有正反两面,开始的纸牌可能是一种乱的状态(有些朝正,有些朝反),现在你需要整理这些纸牌。但是麻烦的是,......