1. 程式人生 > >牛客網暑期ACM多校訓練營(第六場)J Heritage of skywalkert

牛客網暑期ACM多校訓練營(第六場)J Heritage of skywalkert

題意 lcm time \n main 一句話 bsp boa first

題目鏈接: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

題意: 讀題只讀最後一句話系列。t組樣例,t不超過50。每組樣例四個數字n,a,b,c,n代表序列有n個數字,不超過5組樣例n大於2e6,n<1e7。a,b,c在推出序列的過程中使用,數據範圍unsigned int
按照題意中給出的代碼,每運行一次得到的就是a[i],最後要求選出兩個數字使其LCM最大,輸出最大的LCM。
做法:喪心病狂,不是第一次遇見這樣的解法了,也就是保留前100大,然後暴力求LCM即可。
註意******* 題目上說了a,b,c都是unsigned int類型的,不能開成unsigned long long類型的,不正常取舍答案會錯誤。
nth_element(a,a+k,a+n,cmp);可以得到前k小或者前k大,自定義排序方式
代碼如下:
#include<stdio.h>
#include<iostream>
#include<algorithm>

using namespace std;

const int maxn = 10000007;

int t;
int n;
unsigned int x , y , z;
unsigned long long num[maxn];

unsigned long long gcd(unsigned long long a , unsigned long long b)
{
    unsigned long long c;
    c = a%b;
    while( c )
    {
        a = b;
        b = c;
        c = a%b;
    }
    return b;
}

unsigned int solve()
{
    unsigned int tt;
    x ^= x<<16;
    x ^= x>>5;
    x ^= x<<1;
    tt = x;
    x = y;
    y = z;
    z = tt^x^y;
    return z;
}

bool cmp(unsigned long long a , unsigned long long b)
{
    return a>b;
}

int main()
{
    scanf("%d" , &t);
    for(int cas=1; cas<=t; cas++)
    {
        scanf("%d%u%u%u" , &n , &x , &y , &z);
        for(int i=0; i<n; i++)
        {
            num[i] = solve();
        }
        int k = min(100 , n);   ///取前100大
        nth_element(num , num+k , num+n , cmp);
        unsigned long long ans;
        ans = 0;
        for(int i=0; i<k; i++)
        {
//            printf("%llu\n" , num[i]);
            for(int j=i+1; j<k; j++)
            {
                ans = max(ans , num[i]*num[j]/gcd(num[i],num[j]));
//                 printf("%d..%d..%llu..%llu..%llu..%llu..\n" , i , j , num[i] , num[j] , num[i]*num[j]/gcd(num[i],num[j]) , ans);
            }
        }
        printf("Case #%d: %llu\n" , cas , ans);

    }


    return 0;
}

/*
337929
608269
1351708
64488027082
85984357633

405514
675854
1351708
1384776332
2769433858

0..1..405514..675854..137034129478..137034129478..
0..2..405514..1351708..274068258956..274068258956..
0..3..405514..1384776332..280773094747324..280773094747324..
0..4..405514..2769433858..561522100746506..561522100746506..
1..2..675854..1351708..1351708..561522100746506..
1..3..675854..1384776332..467953311543764..561522100746506..
1..4..675854..2769433858..935866475332366..935866475332366..
2..3..1351708..1384776332..467953311543764..935866475332366..
2..4..1351708..2769433858..1871732950664732..1871732950664732..
3..4..1384776332..2769433858..1917523229798924428..1917523229798924428..
*/





牛客網暑期ACM多校訓練營(第六場)J Heritage of skywalkert