UVa 11401 Triangle Counting(计数问题)

思路:

1.我们设最长边为xx的三角形有c(x)c(x)个,另外两条边长为yyzz,我们将yy11取到x1x-1,那么zz,对应的取值种数分别为0,1,2,...,x-2,我们将它们相加;
2.随后减去yyzz相同的种数,因为每一对yyzz,它们的值如果互换那也是成立的,因此现在的种数有一半是重复的,除以22即可;
3.随后使用前缀和思想,先用O(n)O(n)时间将结果保存在数组中;

代码:

#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;
}
posted @ 2019-12-03 19:14  YuhanのBlog  阅读(133)  评论(0编辑  收藏  举报