Codeforces Round #538 (Div. 2) E 随机数生成

https://codeforces.com/contest/1114/problem/E

题意

交互题,需要去猜一个乱序的等差数列的首项和公差,你能问两种问题
1. 数列中有没有数比x大
2. 数列的第i项是什么
最多只能问60次

题解

  • 首先用第一种问题+二分问出数列最大的数是多少,最多二十次
  • 然后用第二种问题尽可能分散的询问第i项,然后将问出的数组排序,对相邻两个数的差求gcd

随机数生成链接
https://codeforces.com/blog/entry/61587
https://codeforces.com/blog/entry/61675

#include<bits/stdc++.h>
#define M 1000000
#define P 23333
#define X 23
#define ll long long 
using namespace std;
int vi[M+5];
ll n,cnt,x,sz,a[100],l,r,mid,gcd,i,mul;
int main(){
	cin>>n;
	mul+=P;x=((ll)rand()*X%n+mul)%n+1;vi[x]=1;
	cout<<"? "<<x<<endl;cnt++;cout.flush();
	cin>>a[sz++];
	l=a[0];r=1e9;
	while(l<r){
		mid=(l+r)/2;
		cout<<"> "<<mid<<endl;cnt++;cout.flush();
		cin>>x;
		if(x)l=mid+1;
		else r=mid;
	}
	for(;cnt<=60;cnt++){
		if(sz==n)break;
		mul+=P;
		x=((ll)rand()*X%n+mul)%n+1;
		while(vi[x]){
			mul+=P;
			x=((ll)rand()*X%n+mul)%n+1;
		}
	    vi[x]=1;
	    cout<<"? "<<x<<endl;cnt++;cout.flush();
	    cin>>a[sz++];
	}
	sort(a,a+sz);
	gcd=a[1]-a[0];
	for(i=2;i<sz;i++){
		gcd=__gcd(gcd,a[i]-a[i-1]);
	}
	cout<<"! "<<l-(n-1)*gcd<<" "<<gcd<<endl;
	cout.flush();
}	
原文地址:https://www.cnblogs.com/VIrtu0s0/p/10516100.html