(第六場)Heritage of skywalkert 【玄學】
題目鏈接:https://www.nowcoder.com/acm/contest/144/J
標題:J、Heritage of skywalkert
| 時間限制:1 秒 | 內存限制:256M
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
題意概括:
按照給出的函數打出 N 個數, 求其中兩個數的最小公倍數的最大值。
官方題解:
由於數據看上去像是隨機?生成的,只需要選出前 100 大的數平方暴力即可。 隨機兩個正整數互質的概率為 6/(π^2) = 0.608...
解題思路:
nth_element()選出前100大的數,暴力前 100 大的數取最大的最小公倍數。
AC code:
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cmath> 5 #define INF 0x3f3f3f3f 6 #define ll unsigned long long 7 using namespace std; 8 9 const int MAXN = 1e7+10; 10 11 ll num[MAXN]; 12 unsigned int N, A, B, C; 13 unsigned int x, y, z; 14 15 ll gcd(ll x, ll y) 16 { 17 return y==0?x:gcd(y, x%y); 18 } 19 20 bool cmp(ll x, ll y) 21 { 22 return x>y; 23 } 24 25 unsigned tang() 26 { 27 unsigned t; 28 x ^= x<<16; 29 x ^= x>>5; 30 x ^= x<<1; 31 t = x; 32 x = y; 33 y = z; 34 z = t^x^y; 35 return z; 36 } 37 38 int main() 39 { 40 int T_case; 41 scanf("%d", &T_case); 42 int cnt = 0; 43 while(T_case--) 44 { 45 46 scanf("%u%u%u%u", &N, &A, &B, &C); 47 x = A, y = B, z = C; 48 for(int i = 0; i < N; i++) 49 { 50 num[i] = tang(); 51 } 52 unsigned int k = min(100u, N); 53 nth_element(num, num+k, num+N, cmp); 54 ll res = 0; 55 for(int i = 0; i < k; i++) 56 for(int j = i+1; j < k; j++) 57 { 58 res = max(res, num[i]*num[j]/gcd(num[i],num[j])); 59 } 60 printf("Case #%d: %llu\n", ++cnt, res); 61 } 62 return 0; 63 }View Code
(第六場)Heritage of skywalkert 【玄學】