1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| #include<stdio.h> #include<stdlib.h>
typedef long long ll; #define N 10010
ll a[N],b[N],c[N];
int cmp(const void *a,const void *b){ return *(ll *)a-*(ll *)b; }
ll mod(ll a){ return a%(1000000007); }
int main() { int T; scanf("%d",&T); while(T--){ int n,k,index_b=0,index_c=0; scanf("%d%d",&n,&k); for(int i=0;i<n;i++){ scanf("%lld",&a[i]); if(a[i]<0) b[index_b++]=a[i]; else c[index_c++]=a[i]; } qsort(b,index_b,sizeof(ll),cmp); qsort(c,index_c,sizeof(ll),cmp); if(index_b+index_c==k){ ll ans=1; while(k) ans*=a[--n],k--,ans=mod(ans); printf("%lld\n",ans); continue; } if(index_b<2){ ll ans=1; while(index_c,k) ans*=c[--index_c],k--,ans=mod(ans); printf("%lld\n",ans); continue; } if(index_c==0){ ll ans=1,i=0; if(k%2==0){ while(i<k) ans*=b[i++],ans=mod(ans); }else{ while(index_b,k) ans*=b[--index_b],k--,ans=mod(ans); } printf("%lld\n",ans); continue; } int left=0,right=index_c-1; ll ans=1; while(k){ if(k%2==0){ while(k){ ll x=0,y=0; if(right-2>=-1) y=c[right]*c[right-1]; if(left+2<=index_b) x=b[left]*b[left+1]; ans=mod(ans); if(x>y) x=mod(x),ans*=x,left+=2; else y=mod(y),ans*=y,right-=2; ans=mod(ans); k-=2; } }else{ k--; ans*=mod(c[right--]); ans=mod(ans); } } printf("%lld\n",ans); } return 0; }
|