三角形数
题目 三角形数
思路分析
在\(10^9\)内这样的数大概有4~5万个
不算很多 可以把1~N里所有的三角形数直接预处理出来
那么问题就转变成了 给定一个数x 要\(a[i]+a[j]==x\)
双指针模板题2
代码实现
#include<bits/stdc++.h>
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<N;i++)
a[i]=(LL)i*(i+1)/2;
for(int i=1,j=N-1;i<N;i++){
while(j>=1 && a[i]+a[j]>x)
j--;
if(j>=1 && a[i]+a[j]==x){
cout<<"YES"<<endl;
return 0;
}
}
cout<<"NO"<<endl;
return 0;
}
💬 评论