UVa 11401 Triangle Counting(计数问题)
思路:
1.我们设最长边为的三角形有个,另外两条边长为和,我们将从取到,那么,对应的取值种数分别为0,1,2,...,x-2,我们将它们相加;
2.随后减去和相同的种数,因为每一对和,它们的值如果互换那也是成立的,因此现在的种数有一半是重复的,除以即可;
3.随后使用前缀和思想,先用时间将结果保存在数组中;
代码:
#include<iostream>
using namespace std;
typedef long long LL;
const int maxn=1e6+50;
LL sum[maxn];
#define c(x) (((x-1)*(x-2)/2-(x-1)/2)/2)
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
// freopen("Arutoria.txt","r",stdin);
for(LL i=4;i<maxn;i++) sum[i]=sum[i-1]+c(i);
int n;
while(cin>>n,n>=3) cout<<sum[n]<<'\n';
return 0;
}

浙公网安备 33010602011771号