Heritage of skywalkert

Heritage of skywalkert

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.

 

输入

2
2 1 2 3
5 3 4 8

输出

Case #1: 68516050958
Case #2: 5751374352923604426

题意:n个数求任意两个数的最大的最小公倍数。
暴力模拟了一些数据发现答案的那两个数总是前15大的数,意思就是只有前15大的数才可能组合出答案。
于是用优先队列维护前15大的数,要注意最好尽量减少pop的操作,以免超时。
#include<bits/stdc++.h>
using namespace std;
unsigned int x,y,z;
unsigned int f()
{
    unsigned int t;
    x^=x<<16;
    x^=x>>5;
    x^=x<<1;
    t=x;
    x=y;
    y=z;
    z=t^x^y;
    return z;
}

priority_queue<unsigned long long,vector<unsigned long long>,greater<unsigned long long> >team;

unsigned long long gcd(unsigned long long a,unsigned long long b)
{
    if(a%b==0)return b;
    return gcd(b,a%b);
}

int tot=1;
int main()
{
    int t,n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %u %u %u",&n,&x,&y,&z);

        for(int i=1; i<=n; i++)
        {
            unsigned long long now=f();

            if(team.size()>=13&&team.top()>=now)continue;
            team.push(now);
            if(team.size()>13)team.pop();
        }

        unsigned long long now[16];
        unsigned long long ans=1;
        int c1=0;
        while(!team.empty())
        {
            now[c1++]=team.top();
            team.pop();
        }

        for(int i=0; i<c1; i++)
            for(int j=0; j<c1; j++)
                if(i!=j)ans=max(ans,now[i]/gcd(now[i],now[j])*now[j]);

        printf("Case #%d: %llu\n",tot++,ans);
    }
    return 0;
}
View Code

 



posted @ 2018-08-04 18:19  1371767389  阅读(240)  评论(0编辑  收藏  举报