Uva 11401 - Triangle Counting
来源:互联网 发布:mac主板坏了维修费用 编辑:程序博客网 时间:2024/05/04 11:54
题目地址: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2396
思路:
这里肯定不能用O(n^3)的算法,只能找规律
设最大的数为x,另外两条为z,y,所以有z+y>x,变形得x-y<z<x
当y=1时,x无解。
当y=2时,x有一个解。
..........
当y=x-1时,x有x-2个解。
所以有(x-1)*(x-2)/2个解,然后去掉重复的
y=z的情况,y的取值从x/2+1开始到x-1
而且每个三角形算了两变。
所以c(i)=((i-1)*(i-2)/2-(i-1)/2)/2.
f[i]=f[i-1]+c(i)
代码如下:
#include<iostream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<cmath>using namespace std;const int N=1000010;typedef long long LL;int n;LL xh[N];int main(){ xh[3]=0; for(LL i=4;i<=1000000;i++) xh[i]=xh[i-1]+((i-1)*(i-2)/2-(i-1)/2)/2; while(scanf("%d",&n)) { if(n<3) break; printf("%lld\n",xh[n]); } return 0;}
- UVA 11401 Triangle Counting
- Uva 11401 - Triangle Counting
- UVA 11401 Triangle Counting
- UVA 11401 - Triangle Counting
- UVA 11401 - Triangle Counting
- uva 11401 - Triangle Counting
- UVA - 11401 Triangle Counting
- UVA 11401 Triangle Counting
- UVA - 11401 Triangle Counting
- uva 11401 Triangle Counting
- UVa 11401 Triangle Counting
- uva 11401 Triangle Counting
- UVa 11401 - Triangle Counting
- UVA 11401Triangle Counting
- UVA 11401:Triangle Counting
- UVa 11401 Triangle Counting
- UVA 11401 Triangle Counting
- UVA 11401-Triangle Counting
- ARC forbids explicit message send of'release'
- backtrace-----打印进程异常退出时的堆栈信息
- serialVersionUID加深理解(例子)
- 杭电2576
- 1MB = 1024 KB还是 1MB = 1000 K
- Uva 11401 - Triangle Counting
- group by如何保留count为0的结果?
- vc/atlmfc/include/afx.h(24) : fatal error C1189: #error : Building MFC application with /MD[d] (CRT
- qdialog 不能关闭
- 网络编程之SOCKET
- opencv学习之路【四】——opencv文件结构介绍
- 关于URL编码(get/post)
- Android中Intent用法总结
- qwt 编程使用(一)