跳到正文

题解

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

ACcepted917约 6 分钟
原文
文章信息与版本

文章信息

发布
2026年7月5日
收录
2026年7月8日
更新
2026年11月11日
篇幅
约 6 分钟
获赞
2
题目
P17070

版本

  1. v1当前

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

题目大意

给定 nn,找到最小的 t≥nt \ge n,使得边长为 t−1,t,t+1t-1,t,t+1 的三角形面积为整数。若无解输出 −1-1。

T≤30000T \le 30000,n≤1030n \le 10^{30}。

分析

纯数学题。

设三角形三边长为 t−1,t,t+1t-1,t,t+1,半周长 p=3t2p = \dfrac{3t}{2}。

由海伦公式,面积 SS 满足

S=p(p−(t−1))(p−t)(p−(t+1))=3t2⋅(t2+1)⋅t2⋅(t2−1)=t43(t2−4).\begin{aligned} S &= \sqrt{p(p-(t-1))(p-t)(p-(t+1))}\\ &= \sqrt{\frac{3t}{2} \cdot \left(\frac{t}{2}+1\right) \cdot \frac{t}{2} \cdot \left(\frac{t}{2}-1\right)} \\ &= \frac{t}{4}\sqrt{3(t^2-4)}. \end{aligned}

SS 为整数当且仅当 3(t2−4)\sqrt{3(t^2-4)} 为整数,且整个表达式结果为整数。

进一步,令 t=2xt=2x(x∈N+x \in \mathbb{N}^+),则

S=x3(x2−1).S = x\sqrt{3(x^2-1)}.

记 y=3(x2−1)y = \sqrt{3(x^2-1)},要求 yy 为正整数。两边平方得

y2=3(x2−1)⇒x2−3(y3)2=1.y^2 = 3(x^2-1) \quad\Rightarrow\quad x^2 - 3\left(\frac{y}{3}\right)^2 = 1.

因为 y2y^2 是 33 的倍数,故 yy 必为 33 的倍数,设 y=3zy = 3z,则

x2−3z2=1.x^2 - 3z^2 = 1.

这是一个标准的 Pell 方程。其最小正整数解为 (x,z)=(2,1)(x,z) = (2,1),对应 t=2x=4t = 2x = 4,面积 S=3xz=6S = 3xz = 6。

Pell 方程 x2−3z2=1x^2 - 3z^2 = 1 的所有正整数解可由基本解 (2,1)(2,1) 迭代得到:

{xk+1=2xk+3zk,zk+1=xk+2zk.\begin{cases} x_{k+1} = 2x_k + 3z_k, \\ z_{k+1} = x_k + 2z_k. \end{cases}

由此可推出 tt 的递推关系:

tk+1=2xk+1=4xk+6zk=4tk−2xk−1=4tk−tk−1.\begin{aligned} t_{k+1} &= 2x_{k+1} = 4x_k + 6z_k \\ &= 4t_k - 2x_{k-1}\\ & = 4t_k - t_{k-1}.\end{aligned}

验证前几项:t1=4t_1 = 4,t2=14t_2 = 14,t3=4×14−4=52t_3 = 4 \times 14 - 4 = 52,均正确。

于是我们可以递推生成所有合法的 tt 值,直到超过数据上限 103010^{30}。对于每个询问,在序列中二分查找第一个 ≥n\ge n 的 tt 即可。

重现赛时那个递推关系是我试出来的,后来才知道 Pell 方程。

实现细节

  • 因为 nn 高达 103010^{30},tt 的值也会超过 long long 范围。代码中使用了 GCC 的 128 位整数 __int128,读入和输出时需自行转换。
  • 当序列末尾超过 103010^{30} 时停止递推(可以适当多生成一两个)。
  • 多组数据 TT 可达 3000030000,预处理后在序列上二分,单次查询 O(log⁡len)O(\log \text{len}),总复杂度可以接受。

代码

C++
#include<bits/stdc++.h>
#ifdef __unix__
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
using namespace std;
typedef long long ll;
const int N=1e5+5;
#define int __int128
inline int read(){
    register int x=0,f=1;char c=gc();
    while(!isdigit(c)){if(c=='-')f=-1;c=gc();}
    while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=gc();
    return x*f;
}
string int128tos(int x){
    if(x==0)return "0";
    string s;
    while(x>0){
        s+=(char)('0'+(x%10));
        x/=10;
    }
    reverse(s.begin(),s.end());
    return s;
}
int t1=4,t2=14;
vector<int>seq;
int stoint128(string s){
    int res=0;
    for(char c:s)res=res*10+(c-'0');
    return res;
}
int t,n;
signed T;
signed main(){
    seq.push_back(t1);
    seq.push_back(t2);
    int lim=stoint128("1000000000000000000000000000000");
    while(seq.back()<=lim){
        t=4*t2-t1;
        seq.push_back(t);
        t1=t2;
        t2=t;
    }
    cin>>T;
    while(T--){
        n=read();
        auto it=lower_bound(seq.begin(),seq.end(),n);
        if(it!=seq.end())cout<<int128tos(*it)<<'\n';
        else cout<<"-1\n";
    }
    return 0;
}

哦,忘说了,我们导出的 tt 数列是无穷递增的,所以理论上不会输出 -1。

原文