STL——nth_element()

nth_element(a,a+m,a+n) 是一个不完全排序,效果是把数组的前m个数都比m+1小,而后面的数右都比m+1大。时间复杂度上比sort要快。一个应用  只需得到前m大的数

这个题还用到了gcd,貌似STL中也可以用__gcd这个函数。(两横)

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e7+5;
unsigned x,y,z;
int n;
unsigned long long gcd(unsigned a,unsigned b)
{
    while(b)
    {
        int c=a%b;
        a=b;
        b=c;
    }
    return a;
}
unsigned tang()
{
    unsigned t;
    x ^= x << 16;
    x ^= x >> 5;
    x ^= x << 1;
    t = x;
    x = y;
    y = z;
    z = t ^ x ^ y;
    return z;
}
unsigned a[maxn];

int main()
{
    int t;
    cin>>t;
    int cnt=1;
    while(t--)
    {
        scanf("%d%u%u%u",&n,&x,&y,&z);
        for(int i=0;i<n;++i)
            a[i]=tang();
        int m=min(100,n);
        nth_element(a,a+n-m,a+n);//找出前100个大的
        unsigned long long ans=0;
        for(int i=n-m;i<n;++i)
            for(int j=i+1;j<n;++j)
        {
            ans=max(ans,1ull*a[i]*a[j]/gcd(a[i],a[j]));
        }
        printf("Case #%d: %llu\n",cnt++,ans);
    }
}

猜你喜欢

转载自blog.csdn.net/codetypeman/article/details/81433974