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

链接:https://www.nowcoder.com/acm/contest/144/J
来源:牛客网
 

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld

题目描述

skywalkert, the new legend of Beihang University ACM-ICPC Team, retired this year leaving a group of newbies again.
 

Rumor has it that he left a heritage when he left, and only the one who has at least 0.1% IQ(Intelligence Quotient) of his can obtain it.


To prove you have at least 0.1% IQ of skywalkert, you have to solve the following problem:

Given n positive integers, for all (i, j) where 1 ≤ i, j ≤ n and i ≠ j, output the maximum value among . means the Lowest Common Multiple.

输入描述:

The input starts with one line containing exactly one integer t which is the number of test cases. (1 ≤ t ≤ 50)

For each test case, the first line contains four integers n, A, B, C. (2 ≤ n ≤ 107, A, B, C are randomly selected in unsigned 32 bits integer range)

The n integers are obtained by calling the following function n times, the i-th result of which is ai, and we ensure all ai > 0. Please notice that for each test case x, y and z should be reset before being called.

No more than 5 cases have n greater than 2 x 106.

输出描述:

For each test case, output "Case #x: y" in one line (without quotes), where x is the test case number (starting from 1) and y is the maximum lcm.

示例1

输入

复制

2
2 1 2 3
5 3 4 8

输出

复制

Case #1: 68516050958
Case #2: 5751374352923604426

题意:给你1e7个数,从中找出两两之间的lcm最大是多少。

题解:刚看见这道题我是很懵逼的,这么大数据量,用n的复杂度运行多一点点常数就T了,不过后来又一想,这么大的数据量不可能完整的求完整个区间,猜测可能之间取较大的一些数求lcm即可,虽然感觉很玄,但是还是能A的....(不过感觉轻轻松松可以卡掉,但貌似那个函数类似产生随机数,不会出现那样的例子),学习到一个STL nth_element()可以将一个区间的第n位数排到他正确的位置去(强大的STL!)

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int maxn=1e7+7;
const int mod=1e9+7;

ll lcm(ll a,ll b)
{
    return a/__gcd(a,b)*b;
}

unsigned x,y,z;
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;
}

ll a[maxn];
int main()
{
    int T;
    scanf("%d",&T);
    for(int cas=1;cas<=T;cas++)
    {
        int n;
        cin>>n>>x>>y>>z;

        for(int i=1;i<=n;i++)
            a[i]=(ll)tang();

        nth_element(a+1,a+100,a+n+1,greater<ll>());

        ll ans=0;
        for(int i=1;i<=min(n,100);i++)
            for(int j=i+1;j<=min(100,n);j++)
                ans=max(ans,lcm(a[i],a[j]));

        printf("Case #%d: ",cas);
        cout<<ans<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/81428029
今日推荐