UVa 106 Fermat vs. Pythagoras(毕达哥拉斯定理)
生活随笔
收集整理的這篇文章主要介紹了
UVa 106 Fermat vs. Pythagoras(毕达哥拉斯定理)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題意:
給一個正整數(shù)N,x2?+ y2?= z2
其中x,y和Z都限定為正整數(shù)且小于等于N。
你要計算出所有滿足x < y < z的三元組(x,y,z)的數(shù)量,并且使得x,y和z兩兩互質(zhì),
即沒有大于1的公因數(shù)。你還要計算出所有滿足下面條件的p的數(shù)量:0 < p ≤ N,且p沒有在所有這樣的三元組中出現(xiàn)(并不限定為兩兩互質(zhì)的三元組)
思路:
http://www.cnblogs.com/devymex/archive/2010/08/07/1799713.html
?
#include <cstdio> #include <cstring> #include <cstring> #include <cmath>bool hash[1000010];int gcd(int x, int y) {int k = x % y;while (k){x = y;y = k;k = x % y;}return y; }int main() {int n;while (scanf("%d", &n) != EOF){int count = 0;memset(hash, false, sizeof(hash));for (int i = 1; i <= (int)sqrt(n*1.0); ++i)for (int j = i + 1; j <= (int)sqrt(n*1.0); j += 2)if (gcd(i, j) == 1) {int a, b, c;a = j * j - i * i;b = 2 * i * j;c = i * i + j * j;if (c > n)break;for (int k = 1; k * c <= n; ++k)hash[k*a] = true, hash[k*b] = true, hash[k*c] = true;++count;}int ans = 0;for (int i = 1; i <= n; ++i)if (!hash[i])++ans;printf("%d %d\n", count, ans);}return 0; }?
?
轉(zhuǎn)載于:https://www.cnblogs.com/kedebug/archive/2012/12/03/2799665.html
總結(jié)
以上是生活随笔為你收集整理的UVa 106 Fermat vs. Pythagoras(毕达哥拉斯定理)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 论如何冲破小游戏流量变现的瓶颈?
- 下一篇: 莫比乌斯