Problem F Codeforces Round #183 (Div. 2) A. Pythagorean Theorem II

來源:互聯網
上載者:User
A. Pythagorean Theorem IItime limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

In mathematics, the Pythagorean theorem — is a relation in Euclidean geometry among the three sides of a right-angled triangle. In terms of areas, it states:

In any right-angled triangle, the area of the square whose side is the hypotenuse (the side opposite the right angle) is equal to the sum of the areas of the squares whose sides are the two legs (the
two sides that meet at a right angle).

The theorem can be written as an equation relating the lengths of the sides ab and c,
often called the Pythagorean equation:

a 2 + b2 = c2

where c represents the length of the hypotenuse, and a and b represent
the lengths of the other two sides.

Given n, your task is to count how many right-angled triangles with side-lengths ab and c that
satisfied an inequality 1 ≤ a ≤ b ≤ c ≤ n.

Input

The only line contains one integer n (1 ≤ n ≤ 104) as
we mentioned above.

Output

Print a single integer — the answer to the problem.

Sample test(s)input
5
output
1
input
74
output
35

題目連結: http://codeforces.com/problemset/problem/304/A

題意:給出一個n 讓你找出有多少組 a,b,c  滿足 1≤abcn  而且 a2+b2=c2

感想:因為 n 最大也只有為10^4 而且時間有3000ms (感覺這題給的時間太寬了  不然很多隊都一次性過不了了 因為都是超過了 1s 的)可以直接用 O(n^2) 的暴力過  稍微最佳化了一點 最後390ms   不過LOR隊只有15ms  大家可以去參考下他們的代碼  他們是打表做的  就是用一個程式(一般是TML的程式)把結果算出來輸出到一個檔案裡  然後直接在另外一個程式中 搞個數組 ans[]={ 複製 } 就夠了  這種方法對時間要求小資料量小的題目很有用  表示又學習了一招 

思路:用了遞推的思想 對c迴圈 每次算出 c=i 時能組合成的 a,b,c 組合  然後存到一 num[] 數組中  然後要求的答案即為 num[] 的前n項和s[n]

下面貼My Code:

#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<string>#include<algorithm>#include<map>#define e sqrt(2.0)using namespace std;int num[10005],s[10005];   // num[i] 相當於數列的ai  s[i] 相當於si(前i項和)int main(){    int i,j,b,c,temp,t,n,t1,t2;    double d1,d2;    memset(num,0,sizeof(num));    // 初始化num[]    s[0]=s[1]=s[2]=s[3]=s[4]=0;   // s[]的前4項均為0     for(c=5;c<=10000;c++)         // 對c迴圈 每次計算出滿足條件的 a,b,c    {        t=c;        temp=(int)(t*1.0/e+0.5);  // e為根號2  由於b>a 故可得出b>=(c/根號2)  +0.5後取整是為了防止精度損失        for(b=c-1;b>=temp;b--)    // 對b迴圈 從c-1開始 因為a不能為0 故b不能等於c   ps:枚舉b要快一些  若枚舉a則要 從1到c/根號2  要慢一些        {            d1=d2=sqrt((c+b)*(c-b)*1.0);   // 計算c^2-b^2開根號 即計算a            if((int)(d2+0.5)==d1)          // 判斷a是否為整數 是則num[]++            {                num[c]++;            }        }        s[c]=s[c-1]+num[c];      // 計算num[]時同時計算 s[]    }    while(~scanf("%d",&n))    {        printf("%d\n",s[n]);    // 答案存好後直接輸出就夠了 這樣比每次進來算答案要快    }    return 0;}
    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.