这把不会 Pell 方程。不过看场上过的人一车,遂找规律。
于是考虑如下暴力:
暴力
#include<bits/stdc++.h>
using namespace std;
#define int __int128
signed main(){
signed M;
cin>>M;
int x=M;
for(int n=1;n<=x;n++){
if(sqrt(n*n*n*n*3.0/16.0-n*n*3.0/4.0)-(int)sqrt(n*n*n*n*3.0/16.0-n*n*3.0/4.0)<1e-10)cout<<(long long)n<<' ';
}
return 0;
}
打表得到:
4 14 52 194 724 2702 10084 37634 140452于是直接使用 OEIS 得到 A003500。
得到 的递推式。
直接计算即可。
关于复杂度
考虑 。
那么 。
解得 。
所以 。
那么暴力计算 就是 。
我们直接预处理,可以这个复杂度做完。查询直接二分。。(注:这里为了突出增长速度,使用 而非 。)
代码:
#include<bits/stdc++.h>
using namespace std;
#define int __int128
const int maxn=453;
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#else
#define getchar _getchar_nolock
#define putchar _putchar_nolock
#endif
inline int read(){
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=(x<<1) + (x<<3)+ch-48;ch=getchar();}
return x*f;
}
inline void write(int x){
if(x<0){putchar('-');x=-x;}
if(x>9)write(x/10);
putchar(x%10+'0');
}
int a[maxn];
void init(){
a[0]=2;
a[1]=4;
for(int i=2;i<maxn;i++)a[i]=4*a[i-1]-a[i-2];
}
signed main(){
ios::sync_with_stdio(0);cin.tie(0);
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
int T;
T=read();
init();
while(T--){
int n;
n=read();
write(*lower_bound(a+1,a+maxn-1,n));
putchar('\n');
}
return 0;
}