HDU5339
题意:给你数a和数组b,然后用a模b中的数,求至少模多少个才能使a==0
思路:直接模拟吧,首先排序,因为模最大的符合(比如2,3,6)然后遍历b,去模其他的所有数,直到为0,标记退出,否则继续遍历b,循环操作。
#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);
//for(int i=1; i<=n; i++)
// printf("%d",b[i]);
printf("%d\n",solve(a,b,n));
}
return 0;
}
HDU5339的更多相关文章
- HDU5339——Untitled
Problem Description There is an integer a and n integers b1,…,bn. After selecting some numbers from ...
随机推荐
- F题 - A+B for Input-Output Practice (V)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description You ...
- Java库使用----xstream1.3.1
package com.xstream; import java.util.Map; /** * XStream可以自动生成相关的xml配置 */ public class XstreamTest { ...
- Truck History(poj 1789)
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- android 如何进入某个具体的应用管理页面
http://stackoverflow.com/questions/4421527/start-android-application-info-screen/4772481#4772481 pri ...
- XFS:大数据环境下Linux文件系统的未来?
XFS:大数据环境下Linux文件系统的未来? XFS开发者Dave Chinner近日声称,他认为更多的用户应当考虑XFS.XFS经常被认为是适合拥有海量数据的用户的文件系统,在空间分配方面的可 ...
- C++中的 new / delete
new的3种形态: new operator , operator new , placement new 1.new operator: new操作符,像 + - * / && . ...
- open和fopen的区别(转)
转载自:http://www.cnblogs.com/joeblackzqq/archive/2011/04/11/2013010.html open和fopen的区别: 1.缓冲文件系统缓 冲文件系 ...
- CodeForce 2A Winner
很多人玩一个游戏,每一轮有一个人得分或者扣分,最后分数最高的人夺冠:如果最后有多个人分数都是最高的,则这些人里面,在比赛过程中首先达到或者超过这个分数的人夺冠.现在给定最多1000轮每轮的情况,求最后 ...
- 数据结构(主席树):COGS 2213. K个串
2213. K个串 ★★★★ 输入文件:bzoj_4504.in 输出文件:bzoj_4504.out 简单对比时间限制:20 s 内存限制:512 MB [题目描述] 兔子们在玩k个 ...
- 动态规划——G 回文串
G - 回文串 Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Stat ...