牛牛与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;
}
/*
*/