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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

nyoj 982

發布時間:2025/3/16 编程问答 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nyoj 982 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Triangle Counting

時間限制:1000?ms ?|? 內存限制:65535?KB 難度:3 描述
You are given n rods of length 1, 2…, n. You have to pick any 3 of them and 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. 輸入
The input for each case will have only a single positive integer n(1<=n<=1000000). The end of input will be indicated by a case with n<1. This case should not be processed.
輸出
For each test case, print the number of distinct triangles you can make.
樣例輸入
5 8 0
樣例輸出
3

22


解題思路:這道題目的題意很簡單,給出n條線段,長度為1-n,問有多少種方法可以從這n條線段中取出3條不同的線段,使得以它們為三邊長可以組成三角形。這是一個純數學推理問題。我們首先假設最大邊為x,其余兩邊為y,z那么有不等式y+z>x。那么z的范圍是x-y<z<x,接下來的工作就是枚舉y的范圍,然后看z能夠取多少。

假設C(x)為最大邊為x時,能得到的三角形個數,假設y從1-(x-1)枚舉一遍,那么z所能取到的個數分別為0,1,2,......,x-2;所以總共有(x-1)(x-2)/2個,但是還沒完,題目中的三條邊都不能相等,但這里面出現了y=z的情況,所以要將這種情況去掉,并且每個三角形重復了一次,所以最后還要除以2。。要保證y=z的情況出現,那么y只能從最小的x/2+1開始,一直到x-1為止,總共出現了(x-1)-(x/2+1)+1=x/2-1,考慮到x的奇偶性,有 (x-1)/2個y=z的情況出現。。那么C(x)=((x-1)*(x-2)/2-(x-1)/2)/2。f(n) = C(1)+C(2)+......+C(n) = f(n-1)+C(n)。。

AC:

#include<iostream> using namespace std;long long f[1000005]; int main() {f[1] = f[2] = f[3] = 0;for(int i = 4; i <= 1000000; i++)f[i] = f[i-1] + ((i-1)*(i-2)/2 - (i-1)/2)/2;int n;while(cin>>n){if(n < 1) break;cout<<f[n]<<endl;}return 0; }

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

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

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