Untitled
There is an integer aaa and nnn integers b1,…,bnb_1, \ldots, b_nb1,…,bn. After selecting some numbers from b1,…,bnb_1, \ldots, b_nb1,…,bn in any order, say c1,…,crc_1, \ldots, c_rc1,…,cr, we want to make sure that a mod c1 mod c2 mod… mod cr=0a \ mod \ c_1 \ mod \ c_2 \ mod \ldots \ mod \ c_r = 0a mod c1 mod c2 mod… mod cr=0 (i.e., aaa will become the remainder divided by cic_ici each time, and at the end, we want aaa to become 000). Please determine the minimum value of rrr. If the goal cannot be achieved, print −1-1−1 instead.
The first line contains one integer T≤5T \leq 5T≤5, which represents the number of testcases.
For each testcase, there are two lines:
-
The first line contains two integers nnn and aaa (1≤n≤20,1≤a≤1061 \leq n \leq 20, 1 \leq a \leq 10^61≤n≤20,1≤a≤106).
-
The second line contains nnn integers b1,…,bnb_1, \ldots, b_nb1,…,bn (∀1≤i≤n,1≤bi≤106\forall 1\leq i \leq n, 1 \leq b_i \leq 10^6∀1≤i≤n,1≤bi≤106).
Print TTT answers in TTT lines.
2 2 9 2 7 2 9 6 7
2-1
解析:对于一组可能的答案cc,如果先对一个觉小的cici取模,再对一个较大的cjcj取模,那么这个较大的cjcj肯定是没有用的。因此最终的答案序列中的c肯定是不增的。那么就枚举选哪些数字,并从大到小取模看看结果是否是0就可以了。时间复杂度O(2n)O(2^n)
我做的时候是暴力枚举的所有排列,超时了。。。
代码:
#include<iostream> #include<cstdio> #include<string.h> #include<algorithm> #include<queue> using namespace std; int main() { int n,T,a,b[25],c[25]; cin>>T; while(T--){ cin>>n>>a; for(int i=0;i<n;i++) cin>>b[i]; sort(b,b+n); int flag=0,id=n; for(int i=0;i<n;i++) if(a%b[i]==0){ cout<<"1"<<endl; flag=1; break; } if(flag==1)continue; do{ int newa=a; for(int i=0;i<n;i++) { newa%=b[i]; if(newa==0){ if(id>i)id=i; flag=1; break; } } }while(next_permutation(b,b+n)); if(flag)cout<<id+1<<endl; else cout<<"-1"<<endl; } return 0; }
其实按照解析,不用枚举所有排列,只需从大到小排序,然后依次枚举一下就好了
#include<iostream> #include<stdio.h> #include<cmath> #include<algorithm> #define INF 999999999 using namespace std; bool cmp(int a,int b) { return a>b; } int solve(int a,int b[],int n) { int ss=INF; int flag; for(int i=1; i<=n; i++) { int temp=a; flag = 0; for(int j=i; j<=n; j++,flag++) if(temp==0) break; else temp=temp%b[j]; if(temp==0) ss=min(ss,flag); } return ss==INF?-1:ss; } int main() { int T; int n,a; int b[21]; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&a); for(int i=1; i<=n; i++) scanf("%d",&b[i]); sort(b+1,b+n+1,cmp); printf("%d\n",solve(a,b,n)); } return 0; }