牛客第六场 J.Heritage of skywalkert(On求前k大)

题目传送门:https://www.nowcoder.com/acm/contest/144/J

题意:给一个function,构造n个数,求出其中任意两个的lcm的最大值。

分析:要求最大的lcm,大概分析一下,差不多就在里面的最大的k个里,k^2求出答案。

因为n(1e7),sort会tle,需要一个效率更低的排序来求出前k大。

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e7+10;
 4 typedef unsigned ui;
 5 typedef unsigned long long ull;
 6 ui x,y,z;
 7 ui A,B,C;
 8 ui a[maxn],b[maxn];
 9 int n;
10 ui tang(){
11     ui t;
12     x^=x<<16;
13     x^=x>>5;
14     x^=x<<1;
15     t=x;x=y;y=z;z=t^x^y;
16     return z;
17 }
18 ull gcd(ull x,ull y){
19     if (y==0) return x;
20     else return gcd(y,x%y);
21 } 
22 void _sort(int k,int l,int r){
23     ui kk=a[(l+r)/2];
24     int i=l,j=r;
25     while (i<j){
26         while (a[j]>kk) j--;
27         while (a[i]<kk) i++;
28         if (i<=j){
29             swap(a[i],a[j]);
30             i++;j--;
31         }
32     }
33     if (r-i+1==k) return ;
34     else if (r-i+1>k) _sort(k,i+1,r);
35     else _sort(k-(r-i+1),l,i-1);
36 }
37 int main(){
38     ios::sync_with_stdio(false);
39     cin.tie(0);cout.tie(0);
40     int t;
41     cin >> t;
42     for (int id=1;id<=t;id++){
43         cin >> n >> A >> B >> C;
44         x=A;y=B;z=C;
45         for (int i=0;i<n;i++){
46             a[i]=tang();
47         }
48         ull ans=0;
49         int k=min(n,200);
50         _sort(k,0,n-1);
51         int cnt=0;
52         for (int i=n-1;i>=n-k;i--)
53             b[cnt++]=a[i];
54         for (int i=0;i<cnt;i++){
55             for (int j=i+1;j<cnt;j++){
56                 ull _gcd;
57                 if (b[i]>b[j]) _gcd=gcd(b[i],b[j]);
58                 else _gcd=gcd(b[j],b[i]);
59                 ans=max(ans,b[i]/_gcd*b[j]);
60             }
61         }
62         cout << "Case #" << id << ": " << ans << endl;
63     }
64     return 0;
65 }

 

posted @ 2018-08-05 15:54  Changer-qyz  阅读(189)  评论(0编辑  收藏  举报