链接:https://ac.nowcoder.com/acm/contest/11254/E
来源:牛客网
Given n, count the number of pairs of positive integers (x, y), such that xy+1|x2+y2,1≤x≤y≤n,1≤n≤1018
解题思路:
经过计算得知:任何x,y都会满足:对任意k>=2,a[i] = k2 * a[i-1] - a[i-2],其中a[0]=0,a[1]=k。
所以只要枚举其中较大的那个数即y,然后打表后二分查找,由于a[2]=k3,因此只需要枚举到1e6,时间复杂度1e6+Tlogn
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll inf=1e18,N=1e6;
ll cnt,T,yy[2*N],n;
int main(){
for(ll i=2;i<=N;i++){
ll k=i*i,pre=0,y=i,t;
while(y*k-pre<=inf){
t=y;
y=yy[++cnt]=y*k-pre;
pre=t;
}
}
sort(yy+1,yy+cnt+1);
for(cin>>T;T;T--){
cin>>n;
cout<<upper_bound(yy+1,yy+1+cnt,n)-yy<<endl;
}
return 0;
}