Newcoder 144 J.Heritage of skywalkert(随机+nth_element)

本文探讨了生成随机整数序列的问题,旨在寻找序列中任意两数最小公倍数(LCM)的最大值。通过分析随机生成的数据特点,采用特定算法进行优化求解,并使用nth_element函数快速找到前100个最大元素。

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

Description

随机生成nnn个整数,问其中任意两个数的最小公约数最大值

Input

第一行一整数TTT表示用例组数,每组用例输入四个整数n,A,B,Cn,A,B,Cn,A,B,C表示数字个数和生成随机数的三个种子

(1≤T≤50,2≤n≤107,0≤A,B,C&lt;232)(1\le T\le 50,2\le n\le 10^7,0\le A,B,C&lt;2^{32})(1T50,2n107,0A,B,C<232)

Output

输出任意两个数的最小公约数的最大值

Sample Input

2
2 1 2 3
5 3 4 8

Sample Output

Case #1: 68516050958
Case #2: 5751374352923604426

Solution

由于数据的随机生成的,故两个数互质的概率很大,lcmlcmlcm最大的两个数很大概率本身的值也很大,故暴力枚举前100100100大的数字更新最优解即可,找前100100100大的元素可以用nth_elementnth\_elementnth_element函数

Code

#include<cstdio>
#include<algorithm>
using namespace std;
typedef unsigned long long ll;
const int INF=0x3f3f3f3f,maxn=10000007;
unsigned x,y,z,A,B,C,a[maxn];
unsigned tang()
{
	unsigned t;
	x^=x<<16;
	x^=x>>5;
	x^=x<<1;
	t=x;
	x=y;
	y=z;
	z=t^x^y;
	return z;
}
unsigned gcd(unsigned a,unsigned b)
{
	return b?gcd(b,a%b):a;
}
int T,n;
int main()
{
	int Case=1;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%d%u%u%u",&n,&A,&B,&C);
		x=A,y=B,z=C;
		for(int i=1;i<=n;i++)a[i]=tang();
		if(n>100)nth_element(a+1,a+n+1-100,a+n+1);
		ll ans=0;
		for(int i=n;i>=max(1,n-100);i--)
			for(int j=n;j>=max(1,n-100);j--) 
				if(i!=j)
					ans=max(ans,(ll)a[i]/gcd(a[i],a[j])*a[j]);
		printf("Case #%d: %llu\n",Case++,ans);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值