CF1034A-A. Enlarge GCD(埃式筛+GCD)

总结

自己之前写一个TLE算法——时间复杂度O(n*sqrt(m))
没去注意时间复杂度,卡卡的死死的

百度看了一下,发现用到了埃式筛的原理,就是走的质因子倍数,这个时间复杂度m*log log m,

/*
                ____________    ______________       __
               / _________  /\ /_____   _____/\     / /\
              / /\       / /  \\    /  /\    \ \   / /  \
             / /  \_____/ /   / \__/  /  \____\/  / /   /
            / /   /    / /   /    /  /   /       / /   /
           / /   /    / /   /    /  /   /       / /   /
          / /   /    / /   /    /  /   /       / /   /
         / /___/____/ /   /    /  /   /       / /___/________
        /____________/   /    /__/   /       /______________/\
        \            \  /     \  \  /        \              \ \
         \____________\/       \__\/          \______________\/
           ___       ___               ___    __________
          /  /\     /  /\             /  /\  /_______  /\
         /  /__\___/  /  \           /  /  \ \      /  /  \
        /____    ____/   /          /  /   /  \____/  /   /
        \   /   /\   \  /          /  /   /       /  /   /
         \_/   /  \___\/ ___      /  /   /       /  /   /
          /   /   /     /  /\    /  /   /       /  /   /
         /   /   /     /  /__\__/  /   /       /  /___/____
        /___/   /     /___________/   /       /___________/\
        \   \  /      \           \  /        \           \ \
         \___\/        \___________\/          \___________\/

          A CODE OF CBOY

*/
#include<bits/stdc++.h>
//typedef long long ll;
//#define ull       unsigned long long
//#define int       long long
#define F           first
#define S           second
#define endl        "\n"//<<flush
#define lowbit(x)   (x&(-x))
#define ferma(a,b)  pow(a,b-2)
#define pb          push_back
#define mp          make_pair
#define all(x)      x.begin(),x.end()
#define memset(a,b) memset(a,b,sizeof(a));
#define IOS         ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int MAXN=0x7fffffff;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
void file()
{
#ifdef ONLINE_JUDGE
#else
    freopen("cin.txt","r",stdin);
    //  freopen("cout.txt","w",stdout);
#endif
}
const int N=3e5+5;
const int NN=1.5e7+5;
int cnt[NN];
bool prime[NN];
signed main()
{
    IOS;
    //file();
    int n,gcd=0;
    cin>>n;
    vector<int>a(n);
    for(auto &it:a)
        cin>>it,gcd=__gcd(gcd,it);
    for(auto &it:a)
        it/=gcd,cnt[it]++;

    int ans=n;
    for(int i=2;i<NN;i++)///埃式筛原理
    {
        if(prime[i])
            continue;
        int sum=0;
        for(int j=i;j<NN;j+=i)
            sum+=cnt[j],prime[j]=true;
        ans=min(ans,n-sum);
    }
    cout<<(ans==n?-1:ans)<<endl;

    return 0;
}

发布了130 篇原创文章 · 获赞 5 · 访问量 4999

猜你喜欢

转载自blog.csdn.net/weixin_44224825/article/details/104145118