--- title: "三角形数" created: 2025-11-28 tags: - 算法 --- # 三角形数 ## 题目 [三角形数](https://www.acwing.com/problem/content/description/5048/) ![[image-10b5f78a.png]] ## 思路分析 ![[image-b3d473de.png]] 在$10^9$内这样的数大概有4~5万个 不算很多 可以把1~N里所有的三角形数直接预处理出来 那么问题就转变成了 给定一个数x 要$a[i]+a[j]==x$ 双指针模板题2 ## 代码实现 ```cpp #include using namespace std; #define endl '\n' typedef long long LL; const int N=1e5+10; LL a[N]; int x; int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cin>>x; for(int i=1;i=1 && a[i]+a[j]>x) j--; if(j>=1 && a[i]+a[j]==x){ cout<<"YES"<