日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

UVA 11401 - Triangle Counting

發布時間:2023/12/13 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UVA 11401 - Triangle Counting 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Problem G
Triangle Counting

Input:?Standard Input

Output:?Standard Output

?

You are given?n?rods of length 1, 2…, n. You have to pick any 3 of them & build a triangle. How many distinct triangles can you make? Note that, two triangles will be considered different if they have at least 1 pair of arms with different length.

?

Input

?

The input for each case will have only a single positive integer?n?(3<=n<=1000000). The end of input will be indicated by a case with?n<3. This case should not be processed.

?

Output

?

For each test case, print the number of distinct triangles you can make.

?

Sample Input??????????????????????????????????????????????????Output for Sample Input

5

8

0

3

22

?


Problemsetter: Mohammad Mahmudur Rahman


解題思路,分段法,f [n] 記錄最長的那條邊不超過n的放法數, 假設c[n] 為最長邊為n的放法數 ?f [n] = f [n-1] + c[n] 。


假設最長邊為z,其它為 x,y;

則 ? z-x < y < z

當 ?x=1 時, z-1<y<z ?0 種方案,

當 ?x=2 時, z-2<y<z ?1 種方案,

......................................................

當 ?x=z-1時, 1<y<z ?z-2種方案

所以總計 (z-1)*(z-2)/2 種方案

但是其中包含了x與y想等的情況,因為 x取值為 [ z/2+1 , z-1 ] 區間內可能 x,y相等,共 (z-1)- (z/2+1)+1=z/2-1 種方案

而且x,y與 y,x認為為兩個,重復計算了,所以除以2

所以 c[n]= [?(z-1)*(z-2)/2 -(z/2-1) ] / 2


#include <iostream> using namespace std;const int maxn=1000000; unsigned long long f[maxn+10]; int n;void ini(){f[3]=0;for(long long z=4;z<=maxn;z++){f[z]=f[z-1] + ( (z-1)*(z-2)/2 - (z/2 -1) )/2;} }int main(){ini();while(cin>>n && n>=3){cout<<f[n]<<endl;}return 0; }

轉載于:https://www.cnblogs.com/toyking/p/3797375.html

總結

以上是生活随笔為你收集整理的UVA 11401 - Triangle Counting的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。