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
问题描述
有一个整数aa和nn个整数b_1, \ldots, b_nb?1??,…,b?n??。在这些数中选出若干个数并重新排列,得到c_1, \ldots, c_rc?1??,…,c?r??。我们想保证a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ mod \ c_r = 0a mod c?1?? mod c?2?? mod… mod c?r??=0。请你得出最小的rr,也就是最少要选择多少个数字。如果无解,请输出-1−1.
输入描述
输入文件的第一行有一个正整数 T \leq 5T≤5,表示数据组数。 接下去有TT组数据,每组数据的第一行有两个正整数nn和aa (1 \leq n \leq 20, 1 \leq a \leq 10^{6}1≤n≤20,1≤a≤10?6??). 第二行有nn个正整数b_1, \ldots, b_nb?1??,…,b?n?? (\forall 1\leq i \leq n, 1 \leq b_i \leq 10^{6}∀1≤i≤n,1≤b?i??≤10?6??).
输出描述
输出TT行TT个数表示每次询问的答案。
输入样例
2 2 9 2 7 2 9 6 7
输出样例
2 -1
这道题使用dfs是很容易求的,可是需要剪枝,当时手贱没剪好,正确答案都变成-1了。。。。。。。。。。
#include"iostream" #include"algorithm" #include"cstdio" using namespace std; int n,c[30],a,sum,ok=0; bool cmp(int a,int b) { return a>b; } void dfs(int cc,int d) { int j; if(cc==0) {ok=d;return;} if(d==n) return; for(j=1;j<=n;j++) if(c[j]<=cc) break; for(int i=j;i<=n;i++) dfs(cc%c[i],d+1); } int main() { int T; cin>>T; while(T--) { cin>>n>>a; ok=-1; for(int i=1;i<=n;i++) cin>>c[i]; sort(c+1,c+1+n,cmp); for(int i=1;i<=n;i++) dfs(a%c[i],1); cout<<ok<<endl; } return 0; }