牛客网暑期ACM多校训练营(第六场) J Heritage of skywalkert(数论, eth_element)

链接:

https://www.nowcoder.com/acm/contest/144/J

题意:

给定一个函数, 求它n次结果中任意两次的lcm最大值

分析:

首先要看出这个函数并没有什么含义, 类似于一个随机函数去生成数字。

第二是要知道任意两个正整数互质的概率是 6/π² , 那么只要取出前100个最大的数, 大概率能找出答案。

另外取前100个最大的数用sort会超时, nth_element可以把第n个元素( n从0开始)挑出, 然后他的前面全是比他少的, 它的后面全是比他大的, 但是可能无序。

nth_element(begin, 第n个元素的位置(begin + n) , end )

#include <bits/stdc++.h>
using namespace std;
const int maxN = 1e7 + 7;
int n, m;
unsigned int x, y, z;
unsigned int tang()
{
    unsigned int t;
    x ^= x<<16;
    x ^= x>>5;
    x ^= x<<1;
    t = x;
    x =y;
    y =z;
    z = t^x^y;
    return z;
}
unsigned long long ans[maxN];
int main(){
    int T;
    cin >> T;
    for(int kase = 1; kase <= T; kase++){
        cin >> n >> x >> y >> z;
        for(int i = 0; i < n; i++){
            ans[i] = (unsigned long long)tang();
        }
        m = min(n , 75);
        nth_element(ans , ans + n - m, ans + n); 

        unsigned long long res = 0;

        for(int i = n - m; i < n; i++){
            for(int j = i + 1; j < n; j++){
                res = max(res , ans[i] / __gcd(ans[i], ans[j]) * ans[j]);
            }
        }
        cout << "Case #" << kase <<": " << res<<"
";
    }
}
原文地址:https://www.cnblogs.com/Jadon97/p/9426594.html