Gym-101808 Problem G. Weird Requirements

It is hard to find teams for students to participate in ACM contests, most students have weird requirements
for their team mates. For example, Ziad wants the GCD of all his team mate’s ratings on all famous
websites to be exactly X and the LCM of his ratings to be exactly Y .
There are N famous competitive programming websites. Ramzi’s rating on the i
th website is Ai
. Ramzi
is very experienced, in one contest, he can change his rating on one website to any other value. What is
the minimum number of contests that Ramzi must participate in, so that Ziad accepts him as his team
mate?
Input
The first line contains one integer T, the number of test cases.
Each test case starts with one line containing three space-separated integers N, X, and Y , the number
of famous websites, the required GCD value, and the required LCM value respectively. (1 ≤ N ≤ 105
)
(1 ≤ X, Y ≤ 109
)
The next line contains N space-separated integers Ai
, Ramzi’s rating on all websites. (1 ≤ Ai ≤ 109
)
Output
For each test case print one line containing one integer, the minimum number of contests Ramzi must
participate in so that he can become Ziad’s team mate.
If it is impossible for Ziad to accept him as his team mate , print -1.
Example
standard input standard output
1
5 1 10
1 5 1 10 10
0
Note
The Greatest Common Divisor (GCD) of an array is the maximum number that divides all integers in
the array without remainder.
The Least Common Multiple (LCM) of an array is the minimum positive number that is a multiple of all
integers in the array

这个题太好玩了hhh,我竟然写了一个小时才AC。

题目的意思是 给出n个数 求变换多少次可以满足它们的最大公约数是x,最小公倍数是y。

比较水吧,考虑不全。

记录不满足条件的数的个数,必须变的cnt。

计算出满足条件的数的最大公约数和最小公倍数。

算出为达到x,y的个数必须改变的个数 0,1,2

                        注意:2 1 12

                                   3 3 应该是1 所以判断gcd(y/aa,bb/x)==1 即只需要改变一个数。

取最大值即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll maxn=1e5+5;
ll a[maxn];
ll gcd(ll x,ll y)
{
    return y==0?x:gcd(y,x%y);
}
ll lcm(ll x,ll y)
{
    ll z=gcd(x,y);
    return x*y/z;
}
int main()
{
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    ll t;
    cin>>t;
    ll n,x,y;
    while(t--){
        cin>>n>>x>>y;
        ll cnt=0,aa=0,bb=0;
        for(ll i=0;i<n;i++){
            cin>>a[i];
            if(a[i]%x!=0||y%a[i]!=0) cnt++;
            else{
                if(aa==0){
                    aa=a[i];bb=a[i];
                }
                else{
                    aa=gcd(aa,a[i]);
                    bb=lcm(bb,a[i]);
                }
            }
        }
        if((n==1&&x!=y)||y%x!=0){
            cout<<"-1"<<endl;
            continue;
        }
        ll res=0;
        if(aa!=0){
            if(aa!=x) res++;
            if(bb!=y) res++;
            //cout<<x<<" "<<y<<" "<<aa<<" "<<bb<<endl;
            if(res==2&&gcd(y/aa,bb/x)==1) res=1;
            cnt=max(cnt,res);
        }
        cout<<cnt<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/haohaoxuexilmy/article/details/89337764