Heritage of skywalkert

鏈接:https://www.nowcoder.com/acm/contest/144/J
來源:牛客網
 

題目描述

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

題解:查詢可知隨機兩個數之間互質的的機率很大,所以選取隨機生成的前100個數找最大的最小公倍數就可以。(也有人用10個數過的);

tips:nth_element(),作用:將第n_thn_th 元素放到它該放的位置上,左邊元素都小於它,右邊元素都大於它.

         nth_element(first, nth, last, compare);

         求[first, last]這個區間中第n大小的元素,如果參數加入了compare函數,就按compare函數的方式比較;

        

#include<bits/stdc++.h>
using namespace std;
unsigned x,y,z;
unsigned long long a[10000000];
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;
}
int main()
{
    int t,n;
    cin>>t;
    int sum=0;
    while(t--)
    {
        unsigned long long ans=0;
        scanf("%d %u %u %u",&n,&x,&y,&z);
        for(int i=0; i<n; i++)
        {
            a[i]=1ull*tang();
        }
        int m=max(0,n-100);
        nth_element(a,a+m,a+n);//排序m-th到n-th個數最大不過100
        for(int j=m; j<n-1; j++)
        {
            for(int k=j+1; k<n; k++)
            {
                ans=max(ans,1ull*a[j]*a[k]/__gcd(a[j],a[k]));
            }
        }
        cout<<"Case #"<<++sum<<": "<<ans<<endl;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章