簡要題意:
若干組數據,每組數據給出一個 ,求出 三邊均 且互不相等 的三角形個數。兩個三角形不同當且僅當至少有一邊長度不同。
首先我們應當考慮三角形的性質。設三邊爲 且 .
則:
可以得到 的範圍:
假設 表示 爲最長邊時的答案。
此時應存在:
顯然這是已知 的情況。,所以:
但是你會發現這並不正確。 的情況需要剔除,這是一個簡單的容斥思想。
當 時,顯然存在:
所以這種情況的方案數爲:
考慮原來可能把 和 重複計算,因此可得:
當 爲自然數時應向下取整。
因此,若 表示題目所求,則:
以此類推即可。
時間複雜度:. ( 爲數據組數)
實際得分:.
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef __int128 ll;
const int N=1e6+1;
inline int read(){char ch=getchar(); int f=1; while(ch<'0' || ch>'9') {if(ch=='-') f=-f; ch=getchar();}
int x=0; while(ch>='0' && ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar(); return x*f;}
inline void write(ll x) {
if(x<0) {putchar('-');write(-x);return;}
if(x<10) {putchar(char(x%10+'0'));return;}
write(x/10);putchar(char(x%10+'0'));
}
ll f[N]; int n;
int main() {
f[1]=f[2]=f[3]=0ll;
for(ll i=4;i<N;i++) f[i]=f[i-1]+(i-2)*(i-2)/4;
while(1) {
n=read(); if(n<3) return 0;
write(f[n]); putchar('\n');
}
return 0;
}