Heritage of skywalkert(思维 + STL)

针对一组随机生成的大整数,本篇介绍了一种高效的算法来找出任意两数间最大最小公倍数(LCM),通过筛选最大数值并使用数据结构如集合进行优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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,a,b,c,按照题目给的式子求a[1]~a[n]。最后求所有(i,j)的最大LCM。

分析:数据量巨大。这个题真的是各路神仙都来了,STL一通乱搞的,然后大胆暴力的。大佬做这种题应该都是有经验了,不能说人家胆子大,只能说是老江湖有经验,知道这么做看似不稳实则稳的一匹。

1、由于数据量太大,而题目让我们求的就是最小公倍数。那么两个数都尽量大,它俩最小公倍数才有可能是那个最大值。

2、所以从大到小排序,暴力前100个就ok了(好像只用暴力20个就行了)。

3、这里不能用sort O(nlogn)会超时。看了大佬的有用set的,也有用nth_element的。set不多说,自带排序。

4、nth_element这个排序就是只排序第n大的元素。处理完之后,默认排在它前面的元素都不比它大,排在它后面的元素都不比它小。nth_element(a,a+max(0,n-100),a+n);

代码就只贴set的那一种了。

#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e7+10;
const int mod=1e9+7;
typedef long long ll;
typedef unsigned ui;
typedef unsigned long long ull;
using namespace std;

inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );

int n;
ui A,B,C;
ui x,y,z;
ull a[MAX],b[MAX];
ull gcd(ull a,ull b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
ull lcm(ull a,ull b)
{
    return a/gcd(a,b)*b;
}
ui tang()
{
    ui 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,p=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%u%u%u",&n,&A,&B,&C);
        x=A;y=B;z=C;
        set<ull>s;
        for(int i=1;i<=n;i++)
        {
            a[i]=(ull)tang();

            if(i==1)
                s.insert(a[1]);

            if(s.size()<100 || a[i]>*s.begin())
                s.insert(a[i]);
            if(s.size()>100)
                s.erase(s.begin());
        }

        int cut=0;
        set<ull> :: iterator it;
        for(it=s.begin();it!=s.end();it++)
            b[++cut]=*it;

        ull ans=0;
        for(int i=1;i<=cut;i++)
            for(int j=i+1;j<=cut;j++)
                ans=max(ans,lcm(b[i],b[j]));

        printf("Case #%d: %llu\n",++p,ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值