Coco
来源:互联网 发布:淘宝店铺申请企业店铺 编辑:程序博客网 时间:2024/05/16 12:59
1058 - Coco
Time Limit:1s Memory Limit:64MByte
Submissions:282Solved:135
DESCRIPTION
Coco just learned a math operation call mod.Now,there is an integer aa and nn integers b1,…,bnb1,…,bn. After selecting some numbers from b1,…,bnb1,…,bn in any order, say c1,…,crc1,…,cr, Coco want to make sure that amodc1modc2mod…modcr=0amodc1modc2mod…modcr=0\ (i.e., aa will become the remainder divided by cici each time, and at the end, Coco want aa to become 00). Please determine the minimum value of rr. If the goal cannot be achieved, print −1−1 instead.
INPUT
The first line contains one integer T(T≤5)T(T≤5), which represents the number of testcases. For each testcase, there are two lines:1. The first line contains two integersnn and aa\ (1≤n≤20,1≤a≤1061≤n≤20,1≤a≤106).2. The second line contains nn integers b1,…,bnb1,…,bn\ (∀1≤i≤n,1≤bi≤106∀1≤i≤n,1≤bi≤106).
OUTPUT
Print TT answers in TT lines.
SAMPLE INPUT
2
2 9
2 7
2 9
6 7
SAMPLE OUTPUT
2
-1
题意:给一个数k和其他n个数,问能否从n个数中任选几个数使k模这几个数之后结果为0。若能则输出最少选的数字个数,不能输出-1。
思路:直接从大到小排序,然后比k大的数直接跳过,比k小的数每次递归模,中间只要模到结果为0就结束,并更新最小值
#include <iostream>#include <string>#include <string.h>#include <algorithm>#include<math.h>using namespace std;int ans;int a[30],f,n;bool cmp(int a,int b){ return a>b;}int we(int a1,int s,int step){ if(a1==0){ ans=ans>step?step:ans; return 1; } int i; for(i=s;i<n;i++){ if(a[i]<=a1) { f=i; break; } } if(i==n) return 0; for(i=f;i<n;i++){ return we(a1%a[i],i+1,++step); }}int main(){ int t; int a1; cin>>t; while(t--){ cin>>n>>a1; for(int i=0;i<n;i++){ cin>>a[i]; } ans=10000000; sort(a,a+n,cmp); if(we(a1,0,0)) cout<<ans<<endl; else cout<<-1<<endl; }}
0 0
- Coco
- Coco
- COCO API
- COCO数据库
- COCO Loss
- COCO奶茶配方
- coco又过生日拉
- Coco学习之KVC
- coco游戏android.mk
- Microsoft COCO 数据集
- 玲珑杯 C -- Coco
- 玲珑学院 1058 - Coco
- coco参考文档网址
- COCO 数据集
- COCO Detection Challenge
- Coco数据集
- coco dataset solve
- coco评价指标
- hdu 1007 Quoit Design 平面上的最近点对 分治法+鸽笼原理
- hdu1108最小公倍数
- Leetcode中的二分题目
- Matlab学习笔记1——基础
- Linux下ls与dir的区别
- Coco
- ashx文件和aspx文件的区别,box-sizing的使用方法,px,em,rem的使用,zepto和jquery的区别
- CUBIC拥塞控制算法是天生干坏事的吗
- Android录制视频添加水印
- HTML5 canvas 实例
- 蓝桥杯 运送马匹
- 升级php7+安装laravel过程中遇到的问题汇总
- Python函数(上)
- 网站开发流程以及HTML5简介(九)