UVA 11401Triangle Counting

题意:求在1~n这n个数字中选出3个不同的数字能组成三角形的方案数。

分析:《算法竞赛入门经典训练指南》数学基础例题2。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<bitset>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=1000010;
const int MAX=151;
const int MOD1=1000007;
const int MOD2=100000009;
const double EPS=0.00000001;
typedef long long ll;
const ll MOD=1000000007;
const ll INF=10000000010;
typedef unsigned long long ull;
ll f[N];
ll get(ll x) {
    return (x+1)*x/2;
}
ll C(ll x) {
    return x*(x-1)/2;
}
int main()
{
    int i,n;
    f[3]=0;
    for (i=4;i<=1000000;i++) f[i]=f[i-1]+get(i/2-1)+C(i-i/2-1);
    while (scanf("%d", &n)&&n>=3) printf("%lld\n", f[n]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值