跳到正文

题解

题解:P17070 [ICPC 2017 Shenyang R] Heron and His Triangle

_CuteRalsei_约 4 分钟
原文
文章信息与版本

文章信息

收录
2026年7月8日
更新
2026年7月10日
篇幅
约 4 分钟
获赞
2
题目
P17070

版本

  1. v1当前

这把不会 Pell 方程。不过看场上过的人一车,遂找规律。

于是考虑如下暴力:

暴力
C++
#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;
}

打表得到:

C++
4 14 52 194 724 2702 10084 37634 140452

于是直接使用 OEIS 得到 A003500。

得到 a(n)=4a(n−1)−a(n−2)a(n)=4a(n-1)-a(n-2) 的递推式。

直接计算即可。

关于复杂度

考虑 O(a(n))=xnO(a(n))=x^n。

那么 xn=4xn−1−xn−2x^n=4x^{n-1}-x^{n-2}。

x2=4x−1x^2=4x-1 解得 x1=2+3,x2=2−3x_1=2+\sqrt3,x_2=2-\sqrt3。

所以 O(a(n))=O(C1(2+3)n+C2(2−3)n)=O((2+3)n)O(a(n))=O\left(C_1(2+\sqrt3)^n+C_2(2-\sqrt3)^n\right)=O((2+\sqrt3)^n)。

那么暴力计算 a(n)a(n) 就是 O(log⁡3.72V)O(\log_{3.72}V)。

我们直接预处理,可以这个复杂度做完。查询直接二分。O(log⁡log⁡3.72V)O(\log\log_{3.72}V)。(注:这里为了突出增长速度,使用 log⁡3.72\log_{3.72} 而非 log⁡\log。)

代码:

C++
#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;
}
原文