寻找 n 个数中 k 个数的最小公倍数 x LCMSetEasy

题目来源: http://acm.nyist.net/JudgeOnline/problem.php?pid=976

LCMSetEasy

时间限制: 1000 ms  |  内存限制: 65535 KB
难度: 2
 
描述

For any non-empty sequence of positive integers s1, s2, ..., sK their least common multiple is the smallest positive integer that is divisible by each of the given numbers. We will use "lcm" to denote the least common multiple. For example, lcm(3) = 3, lcm(4,6) = 12, and lcm(2,5,7) = 70.

You are given an array S and an integer x. Find out whether we can select some elements from S in such a way that their least common multiple will be precisely x. Formally, we are looking for some s1, s2, ..., sK, K >= 1, such that each si belongs to S, and x=lcm(s1, s2, ..., sK). Return "Possible" if such elements of S exist, and "Impossible" if they don't.

 

 
输入
There are multiple test cases.
Each test case contains two lines.The first line is two integer N(1≤N≤100,represents the array contains N integers) and x(1≤x≤10^9). The second line contains N integers,the ith integer represents si(1≤si≤10^9).
输出
Printf "Possible" if such elements of S exist, and "Impossible" if they don't.
样例输入
4 20
2 3 4 5
3 60
2 3 4
样例输出
Possible
Impossible


分析:
在给定的n 个数中, 先去掉 不能整数 x 的数, 剩下的数 的最小公倍数 恰好 == x, 表示有解。
代码如下:
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
using namespace std;
vector<int> vk;
int gcd(int a, int b){
    return b==0?a : gcd(b, a%b);
}
int lcm(int a, int b){
    return b/gcd(a,b) * a;
}
int main(){
    int n , x,temp;
    while(cin>>n>>x){
        vk.clear();
        for(int i=0 ;i<n ;i++){
            cin>>temp;
            if(x%temp == 0)
                vk.push_back(temp);
        }
        int lc=1;
        for(int i=0; i<vk.size(); i++){
            lc=lcm(lc, vk[i]);
        }
        if(lc == x) printf("Possible\n");
        else printf("Impossible\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zn505119020/p/3606229.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值