-
題意:有\(n\)個龍珠在坐標軸上,你每次可以詢問一個坐標,返回給你距離你坐標最近的龍珠的距離的平方,當這個距離等於\(0\)時,說明找到一顆龍珠.
-
題解:這題主要有兩個點:
1.首先詢問\((0,0)\),可以確定一顆龍珠的距離
2.對於這個距離,我們枚舉\(x\)的可能坐標,然后因為距離固定,可以得到\(y\)的坐標,判斷\(y\)坐標是不是完全平方數,可以證明:\(x\)和\(y\)同為完全平方數的數的個數很少,然后我們去枚舉這些數即可,一定有一個坐標是我們要找的龍珠.
-
代碼:
#include <bits/stdc++.h> #define ll long long #define fi first #define se second #define pb push_back #define me memset #define rep(a,b,c) for(int a=b;a<=c;++a) #define per(a,b,c) for(int a=b;a>=c;--a) const int N = 1e6 + 10; const int mod = 1e9 + 7; const int INF = 0x3f3f3f3f; using namespace std; typedef pair<int,int> PII; typedef pair<ll,ll> PLL; ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} ll lcm(ll a,ll b) {return a/gcd(a,b)*b;} ll check(int k,ll l,ll r,ll x){ if(l==r) return l; ll mid=(l+r)>>1; ll tmp=1; rep(i,1,k) tmp*=mid; if(tmp<x) return check(k,mid+1,r,x); else return check(k,l,mid,x); } int main() { ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n; cin>>n; vector<PLL> pt; rep(i,1,n){ cout<<0<<' '<<0<<'\n'; cout.flush(); ll dis; cin>>dis; if(dis==0){ continue; } pt.clear(); for(ll i=0;i*i<=dis;++i){ ll x=i*i; ll y=dis-x; if(y<0) continue; ll tmp=check(2,0,1500000000,y); if(tmp*tmp==y){ pt.pb({i,tmp}); } } for(auto w:pt){ ll x=w.fi; ll y=w.se; if(x<0 || x>1000000 || y<0 || y>1000000) continue; cout<<x<<' '<<y<<'\n'; cout.flush(); ll now; cin>>now; if(now==0){ break; } } } return 0; }