uva 106 Fermat vs. Pythagoras(勾股数求法)
来源:互联网 发布:linux vi怎么保存退出 编辑:程序博客网 时间:2024/06/05 00:08
题意:
求1到n中互质的勾股数 a ^ 2 + b ^ 2 = c ^ 2的个数,和不为互质勾股数的个数。
解析:
知道勾股数可以表示为:(j * j - i * i) ^ 2 + (2 * i * j)^ 2 = (i * i + j * j) ^ 2.
其中 j > i , 且 j 与 i 不同奇且不同偶。
然后直接暴力就行了。
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long longusing namespace std;const int maxn = 1e6;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = 4 * atan(1.0);const double ee = exp(1.0);int n;int cnt;int num[maxn];int gcd(int a, int b){ return b == 0 ? a : gcd(b, a % b);}void fun(){ int m = sqrt(n + 0.5); for (int i = 1; i <= m; i++) { for (int j = i + 1; j <= m; j++) { int c = i * i + j * j; if (n < c) break; int a = j * j - i * i; int b = 2 * i * j; if (gcd(gcd(a, b), c) == 1) { num[a] = num[b] = num[c] = true; cnt++; for (int k = 2; k * c <= n; k++) { num[k * a] = num[k * b] = num[k * c] = true; } } } }}int main(){ #ifdef LOCAL freopen("in.txt", "r", stdin); #endif // LOCAL while (scanf("%d", &n) == 1) { memset(num, false, sizeof(num)); cnt = 0; fun(); int ans = 0; for (int i = 1; i <= n; i++) { if (!num[i]) ans++; } printf("%d %d\n", cnt, ans); } return 0;}
0 0
- uva 106 Fermat vs. Pythagoras(勾股数求法)
- UVA 106 Fermat vs. Pythagoras(勾股数)
- Fermat vs. Pythagoras - UVa 106 勾股数
- UVA - 106 Fermat vs. Pythagoras 勾股数
- uva 106 - Fermat vs. Pythagoras(素勾股数)
- Uva 106-Fermat vs. Pythagoras(勾股数性质)
- uva 106 - Fermat vs. Pythagoras
- UVA 106 - Fermat vs. Pythagoras
- UVA 106 Fermat vs. Pythagoras
- UVa 106 - Fermat vs. Pythagoras
- UVa 106 - Fermat vs. Pythagoras
- UVA - 106 Fermat vs. Pythagoras
- UVa:106 Fermat vs. Pythagoras
- uva 106 Fermat vs. Pythagoras
- UVA 106 Fermat vs. Pythagoras
- UVa 106 - Fermat vs. Pythagoras
- Uva 106 Fermat vs. Pythagoras
- uva 106 Fermat vs. Pythagoras
- Struts2下载及简介
- c#string 方法
- 招投标阶段的内部控制要点
- 手势 view tag
- vimdiff比较两个文件
- uva 106 Fermat vs. Pythagoras(勾股数求法)
- java中重载与重写的区别
- 非师范生的
- struts2的运行流程
- Java关键字介绍之this与super
- opencv2用反投影实现标定颜色的识别
- Eclipse搭建Struts2环境
- 竞品分析报告的几个错误
- Java中HashMap遍历的两种方式