数论(毕达哥拉斯定理):POJ 1305 Fermat vs. Pythagoras
Time Limit: 2000MS | Memory Limit: 10000K | |
Total Submissions: 1493 | Accepted: 865 |
Description
This problem deals with computing quantities relating to part of
Fermat's Last Theorem: that there are no integer solutions of a^n + b^n =
c^n for n > 2.
Given a positive integer N, you are to write a program that computes
two quantities regarding the solution of x^2 + y^2 = z^2, where x, y,
and z are constrained(驱使)
to be positive integers less than or equal to N. You are to compute the
number of triples (x,y,z) such that x < y < z, and they are
relatively prime, i.e., have no common divisor(除数)
larger than 1. You are also to compute the number of values 0 < p
<= N such that p is not part of any triple (not just relatively prime
triples).
Input
input consists of a sequence of positive integers, one per line. Each
integer in the input file will be less than or equal to 1,000,000. Input
is terminated by end-of-file
Output
each integer N in the input file print two integers separated by a
space. The first integer is the number of relatively prime triples (such
that each component of the triple is <=N). The second number is the
number of positive integers <=N that are not part of any triple whose
components are all <=N. There should be one output line for each
input line.
Sample Input
10
25
100
Sample Output
1 4
4 9
16 27
题意:给定一个n,输出三元组(a,b,c)其中GCD(a,b,c)=1,且a²+b²=c²,以及1~n中没有在任何一个三元组中出现过的数的个数。
这道题需要知道勾股数的性质。
最最开始GCD(a,b,c)=1 ==> a,b,c两两互质,通过a²+b²=c²易证。
首先,对于一组勾股数,①a与b的奇偶性不同,②c一定为奇数。
证明①:若a与b同为偶数,则c也为偶数,与GCD(a,b,c)=1矛盾;若a与b同为奇数,c一定为偶数,设a=2*i+1,b=2*j+1,c=2*k -> a²+b²=c²->2*i²+2*i+2*j²+2*j+1=2*k²,这个式子是矛盾的。
证明②:a,b一奇一偶,显然。
然后将 a²+b²=c² 变形,b为偶数时,a²=(c+b)*(c-b),容易发现c-b与c+b互质,所以c-b与c+b都是平方数,设x²=c-b,y²=c+b,得a=x*y,b=(y²-x²)/2,c=(y²+x²)/2.
易得x,y都为奇数,直接枚举x,y就好了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn=;
bool vis[maxn];
long long Gcd(long long a,long long b){
return b?Gcd(b,a%b):a;
}
int main(){
int n,m,ans,tot;
while(~scanf("%d",&n)){
m=(int)sqrt(n+0.5);ans=tot=;
memset(vis,,sizeof(vis));
for(int t=;t<=m;t+=)
for(int s=t+;(s*s+t*t)/<=n;s+=)
if(Gcd(s,t)==){
int a=s*t;
int b=(s*s-t*t)/;
int c=(s*s+t*t)/
ans++;
for(int k=;k*c<=n;k++){
vis[k*a]=true;
vis[k*b]=true;
vis[k*c]=true;
}
}
for(int i=;i<=n;i++)
if(!vis[i])
tot++;
printf("%d %d\n",ans,tot);
}
}
数论(毕达哥拉斯定理):POJ 1305 Fermat vs. Pythagoras的更多相关文章
- POJ 1305 Fermat vs. Pythagoras (毕达哥拉斯三元组)
设不定方程:x^2+y^2=z^2若正整数三元组(x,y,z)满足上述方程,则称为毕达哥拉斯三元组.若gcd(x,y,z)=1,则称为本原的毕达哥拉斯三元组. 定理:正整数x,y,z构成一个本原的毕达 ...
- UVa 106 - Fermat vs Pythagoras(数论题目)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- CPC23-4-K. 喵喵的神数 (数论 Lucas定理)
喵喵的神∙数 Time Limit: 1 Sec Memory Limit: 128 MB Description 喵喵对组合数比較感兴趣,而且对计算组合数很在行. 同一时候为了追求有后宫的素养的生活 ...
- acm数论之旅--数论四大定理
ACM数论之旅5---数论四大定理(你怕不怕(☆゚∀゚)老实告诉我) (本篇无证明,想要证明的去找度娘)o(*≧▽≦)ツ ----------数论四大定理--------- 数论四大定理: 1.威 ...
- Fermat vs. Pythagoras POJ - 1305 (数论之勾股数组(毕达哥拉斯三元组))
题意:(a, b, c)为a2+b2=c2的一个解,那么求gcd(a, b, c)=1的组数,并且a<b<c<=n,和不为解中所含数字的个数,比如在n等于10时,为1, 2, 7,9 ...
- 【威佐夫博奕】 betty定理 poj 1067
Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...
- 数论问题(1) : poj 1061
最近,本人发现了一个新网站poj(不算新) 当然了,上面的资源很好...... 就是还没搞清楚它的搜索该怎么弄,如果有大佬能教教我怎么弄,请在下方留言 闲话少说,回归我们的正题 题目转自poj 106 ...
- poj1305 Fermat vs. Pythagoras(勾股数)
题目传送门 题意: 设不定方程:x^2+y^2=z^2若正整数三元组(x,y,z)满足上述方程,则称为毕达哥拉斯三元组.若gcd(x,y,z)=1,则称为本原的毕达哥拉斯三元组. 定理:正整数x,y, ...
- 组合数学 - 波利亚定理 --- poj : 2154 Color
Color Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7873 Accepted: 2565 Description ...
随机推荐
- 10.5 noip模拟试题
2bc*cosA=b^2+c^2-a^2 数学题QAQ 开始π精度不够40分 怪我喽~ #include<iostream> #include<cstdio> #include ...
- asp.net 的状态管理
状态管理 (state management) 在Web应用程序中,一向是很重要的课题,良好的状态管理可以帮助开发人员发展出具有状态持续能力的应用程序(像是工作流程型应用程序或是电子商务应用程序),但 ...
- 转--Oracle DB 服务器系统时间修改问题与 SCN 关系的深入研究
论坛里一个朋友说将DB 服务器系统时间往往后修改了3个月(从11年改成10年),启动DB报600的错误. 一. 先做个测试 1.1 关闭DB SQL> shutdown immediate Da ...
- win7、xp下Meclipse SVN用户名修改
Meclipse SVN用户名修改,在网上查找后发现如下方法: 1.查看你的Eclipse中使用的是什么SVNInterface windows>preference>Team>SV ...
- HTML5常用标签
section 板块,用于划分页面的不同区域或者划分文章里不同的节 ↓ header 页面头部或者板块section头部 ↓ footer 页面底部或者section底部 ↓ nav 导航(包含 ...
- HTML5 WebAudioAPI-实例(二)
简单播放实例1: var url='../content/audio/海阔天空.mp3'; if (!window.AudioContext) { alert('您的浏览器不支持AudioContex ...
- 查询可用的Nuget服务地址
解决访问Nuget源失败问题 查询IP址址 nslookup nuget.org 如失败,通过google 的dns服务器查询 nslookup nuget.org 8.8.8.8 将得到的Ip地址加 ...
- Datatables+Bootstrap
http://sandbox.runjs.cn/show/thwac3ec 运行效果 <!DOCTYPE html> <html lang="en"> &l ...
- css media
/* media */ /* 横屏 */ @media screen and (orientation:landscape){ } /* 竖屏 */ @media screen and (orient ...
- Servlet(三)
重定向 服务器向浏览器发送一个302状态码以及一个Location消息头(该消息头的值是一个地址,称之为重定向地址),浏览器收到后会立即向重定向的地址发出请求,使用相应对象的API方法实现(respo ...