JZOJ 5578 猜数游戏

猜数游戏

Description

给定正整数 n ,m, a ,b和实数 p ,生成一个满足以下条件的数列:
1、数列长度为n
2、对于数列中的每个元素,它有 p 的概率为arand(),有 1 -p的概率为 brand() rand() 是一个在 [1,m] 均匀随机 的整数。
给定正整数 n ,m以及一个按以上方式生成的数列,求出 a b的值。

Data Constraint

n= 5103 m =109 102 <= p <=1102

Solution

随机选出两个数 x ,y,我们知道有 pp+(1p)(1p)1 的概率他们是由同一个数生成的(记为 P ),显然有P>= 50%
假设这两个数是由 a 生成的,设x= ax y =ay,由于是随机生成的,所以
x y 互质的概率是大于 6π2 ,所以我们就索性当 a gcd(x,y)

接下来就将一定不能由 a 生成的数求gcd,当成 b ,如果所有的数都能被a b 生成,我们就当做找到了答案,否则再重复一次整个过程。

时间复杂度期望O( n log m )

Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>

#define fo(i,j,l) for(int i=j;i<=l;++i)
#define fd(i,j,l) for(int i=j;i>=l;--i)

using namespace std;
typedef long long ll;
const ll N=52e2;

int type,t,n,j,k,l,i;
ll a[N],m;

inline ll read()
{
    ll o=0; char ch=' ';
    for(;ch<'0'||ch>'9';ch=getchar());
    for(;ch>='0'&&ch<='9';ch=getchar())o=o*10+ch-48;
    return o;
}

ll gcd(ll a,ll b)
{return (b)?gcd(b,a%b):a;}

int main()
{
    srand(1237654);
    cin>>type>>t;
    fo(tt,1,t){
        cin>>n>>m;
        fo(i,1,n)a[i]=read();
        ll d=a[1]; int alright=1;
        fo(i,1,n)d=gcd(a[i],d);
        fo(i,1,n)a[i]=a[i]/d,alright&=(a[i]<=m);
        if(alright){
            printf("%lld %lld\n",d,d);
            continue;
        }
        fo(i,1,n/4){
            int k=rand()%n+1,p=rand()%n+1;
            swap(a[p],a[k]);
        }
        ll ans1,ans2;
        while(true){
            int k=rand()%n+1,v=rand()%n+1;
            while(v==k)v=rand()%n+1;
            ll w1=gcd(a[v],a[k]),w2=0,ok=0;
            if(w1*d>m)continue;
            ll con=w1*m;
            fo(i,1,n)if((a[i]%w1)||a[i]>con)w2=gcd(a[i],w2);
            w2=w2==0?w1:w2;
            if(w2*d>m)continue;
            ll uyt=w2*m;
            fo(i,1,n)if(((a[i]%w1)||(a[i]>con))&&(a[i]>uyt))
            {ok=1; break;}
            if(!ok){
                ans1=w1; ans2=w2; break;
            }
        }
        printf("%lld %lld\n",ans1*d,ans2*d);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值