牛客 牛牛与LCM(LCM)

牛牛与LCM

链接:https://ac.nowcoder.com/acm/problem/21674
来源:牛客网

题目描述
牛牛最近在学习初等数论,他的数学老师给他出了一道题,他觉得太简单了, 懒得做,于是交给了你,
题目是这样的:
有一堆数,问你能否从中选出若干个数使得这些数的最小公倍数为x
输入描述:
第一行输入一个整数n (1 ≤ n ≤ 50)
第二行输入n个整数ai (1 ≤ ai ≤ 109)
第三行输入一个整数x (2 ≤ x ≤ 109)
输出描述:
如果可以,输出"Possible"
否则输出"Impossible"
示例1
输入
4
2 3 4 5
20
输出
Possible

示例2
输入
3
2 3 4
611
输出
Impossible

示例3
输入
3
2 3 4
12
输出
Possible

示例4
输入
10
1 2 3 4 5 6 7 8 9 10
24
输出
Possible

备注:
子任务1:x <= 1000
子任务2:x <= 1000000
子任务3:无限制

题解
先去除所有不是 x x x因子的 a i a_i ai,对剩余的数求最小公倍数 l c lc lc,如果 l c lc lc能够整除 x x x,则代表在其中能选出数,其最小公倍数是 x x x

代码

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <queue>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <set>
#include <cmath>

using namespace std;
#define me(x,y) memset(x,y,sizeof x)
#define MIN(x,y) x < y ? x : y
#define MAX(x,y) x > y ? x : y
typedef long long ll;
const int maxn = 1e6;
const double INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int eps = 1e-8;

ll gcd(ll a,ll b){
    return b == 0? a: gcd(b,a%b);
}

ll lcm(ll a,ll b){
    return a/gcd(a,b)*b;
}
int main(){
    int n,x;
    cin>>n;
    ll a[55];
    for(int i = 1; i <= n; ++i)scanf("%lld",&a[i]);
    cin>>x;
    ll lc = 1;
    for(int i = 1; i <= n; ++i){
        if(x%a[i] == 0) lc = lcm(lc,a[i]);
    }
    
    if(lc%x == 0)puts("Possible");
    else puts("Impossible");
    return 0;
}

/*


*/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值