We call a sequence of non-negative integers, , awesome if there exists some positive integer such that each element in (where ) is evenly divisible by . Recall that evenly divides if there exists some integer such that .
Given an awesome sequence, , and a positive integer, , find and print the maximum integer, , satisfying the following conditions:
- is also awesome.
Input Format
The first line contains two space-separated positive integers, (the length of sequence ) and (the upper bound on answer ), respectively.
The second line contains space-separated positive integers describing the respective elements in sequence (i.e., ).
The second line contains space-separated positive integers describing the respective elements in sequence (i.e., ).
Constraints
Output Format
Print a single, non-negative integer denoting the value of (i.e., the maximum integer such that is awesome). As is evenly divisible by any , the answer will always exist.
Sample Input 0
3 5
2 6 4
Sample Output 0
4
Explanation 0
The only common positive divisor of , , and that is is , and we need to find such that . We know because would not evenly divide . When we look at the next possible value, , we find that this is valid because it's evenly divisible by our value. Thus, we print .
Sample Input 1
1 5
7
Sample Output 1
0
Explanation 1
Being prime, is the only possible value of . The only possible such that is (recall that ), so we print as our answer.
Solution:
- #include <bits/stdc++.h>
- using namespace std;
- vector <int> PF;
- void primeFactors(int n){
- while (n%2 == 0){
- PF.push_back(2);
- n = n/2;
- }
- for (int i = 3; i <= sqrt(n); i = i+2){
- while (n%i == 0){
- PF.push_back(i);
- n = n/i;
- }
- }
- if (n > 2)PF.push_back(n);
- }
- int main(){
- int n, k, x;
- scanf("%d", &n);
- scanf("%d", &k);
- int g, f = 0;
- for(int i = 0; i < n; i++){
- scanf("%d", &x);
- if(!f){
- f = 1;
- g = x;
- }
- g = __gcd(g, x);
- }
- if(g < 2)return 0*puts("0");
- primeFactors(g);
- int sz = PF.size();
- int mx = 0;
- for(int i = 0; i < sz; i++){
- int z = k/PF[i];
- mx = max(mx, z*PF[i]);
- }
- printf("%d\n", mx);
- return 0;
- }