牛客网暑期ACM多校训练营(第六场)J Heritage of skywalkert
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
空间限制: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.
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.
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
输出
复制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.. */