UVA 11401 Triangle Counting [递推]
题目链接:VJudge
题意:给定N条边,边长分别为1~N,从这N条边中,选出三条边,问能构成三角形的情况有多少种。
思路:dp[i]表示i条边的情况,dp[i]包含了dp[i-1]与最长边为i这两类情况构成。当最长边为i时,另外两条边范围是[2, i-1],我们假定这两条边为a,b。满足条件1
#include <cmath>
#include <queue>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w",stdout)
#define fst first
#define snd second
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
// typedef __int64 LL;
typedef long long LL;
typedef unsigned int uint;
const int INF = 0x3f3f3f3f;
const double eps = 1e-6;
const int MAXN = 1000000 + 5;
const int MAXM = 10000 + 5;
LL dp[MAXN];
int N;
void init() {
dp[3] = 0;
for (uLL i = 4; i < MAXN; i++) {
uLL t = (i - 2) >> 1, x;
if (i & 1) {
x = (1 + t) * t;
} else {
x = t * t;
}
dp[i] = dp[i - 1] + x;
}
}
int main() {
#ifndef ONLINE_JUDGE
FIN;
#endif // ONLINE_JUDGE
init();
while (~scanf("%d", &N) && N >= 3) {
printf("%lld\n", dp[N]);
}
return 0;
}