UVa 106/POJ 1305 Fermat vs. Pythagoras(数论&勾股数)

数论概论学习笔记(一)——勾股数 版权声明:本文为博主原创文章,未经博主允许不得转载。 1. Pythagoras theorem(勾股定理)一个直角三角形中,两个直角边边长的平方加起来等于斜边长的平方。 如果设直角三角形的两条直角边长度分别是a和b,斜边长度是c,那么可以用数学语言表达: a2+b2=c2a^{2}+b^{2}=c^{2}满足这个等式的三元数组(a,b,c)且数a、b、c互质的数称为勾股数。 可证a、 阅读详情

106 - Fermat vs. Pythagoras

Time limit: 3.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=42

http://poj.org/problem?id=1305


题意:

求≤N的本原勾股数组(PPT)的组数和≤N的非勾股数的个数。


思路:

参考《数论概论》P10的如下定理:


则对所有满足3≤s≤√(2n-1),1≤t≤max{ s-2 , √(2n-s^2) },gcd(s,t)=1的奇数s,t,求PPT和对应范围内的派生勾股数(本原勾股数组(a,b,c)的派生勾股数组为(ka,kb,kc),k>1),然后标记已出现过的数。

最后统计未标记的数的个数即可。

复杂度:O(NlogN)


完整代码:

/*UVa: 0.192s*/
/*POJ: 0ms,1136KB*/

#include <cstdio>
#include <cstring>
#include <cmath>
const int maxn = 1000001;

bool vis[maxn];

int gcd(int a, int b) {return b ? gcd(b, a % b) : a;}

int main()
{
	int n, s, t, maxs, maxt, i, a, b, c;
	while (~scanf("%d", &n))
	{
		int cnt = 0;//PPT的组数
		memset(vis, 0, sizeof(vis));

		//循环生成所有的s和t,根据公式,s的最大值maxs<=√(2n-1)
		maxs = (int)sqrt((double)((n << 1) - 1));
		for (s = 3; s <= maxs; s += 2)
		{
			//求出t的最大值maxt,maxt<=s-2 && maxt<=√(2n-s^2)
			maxt = (int)sqrt((double)((n << 1) - s * s));
			if (s - 2 < maxt) maxt = s - 2;
			for (t = 1; t <= maxt; t += 2)
			{
				if (gcd(s, t) == 1)
				{
					++cnt;
					a = s * t;
					b = (s * s - t * t) >> 1;
					c = (s * s + t * t) >> 1;

					//标记PPT及其派生勾股数
					for (i = 1; c * i <= n; ++i)
						vis[a * i] = vis[b * i] = vis[c * i] = true;
				}
			}
		}
		printf("%d ", cnt);

		//统计没有出现过的数的个数
		cnt = 0;
		for (i = 1; i <= n; ++i)
			if (!vis[i]) ++cnt;
		printf("%d\n", cnt);
	}
	return 0;
}

勾股数论文 勾股数的构造方法 勾股数论文 勾股数的构造方法 教你如何构造勾股数 立即下载

相关推荐

初等数论中勾股数的程序

初等数论中勾股数的程序初等数论中勾股数的程序初等数论中勾股数的程序初等数论中勾股数的程序

UVA 106(费马定理&毕达哥拉斯定理)

#include #include #include #include #include #define M 1000010 using namespace std; int gcd(int a,int b) { if(b==0)return a; return gcd(b,a%b); } bool flag[M]; int main() { int N,x,y,

u014682977的专栏 635

Openwifi 开源项目解读(一)

Openwifi wifi 80211 无线网卡 基带 FPGA linux 驱动 开源

2万+

uva 106(勾股定理)

题意:让你算数a^2+b^2=c^2在n内全部(a, b, c) = 1的解得个数,和除去所有(a, b, c)能满足方程的数字个数。 思路:暴力会超时所以需要使用勾股定理: 设 m &gt; n 、 m 和 n 均是正整数, a = m2 − n2,b = 2mn,c = m2 + n2 若 m 和 n 是互质,而且 m 和 n 至少有一个是偶数,计算出来的 a, b, c 就是素勾股数...

weixin_33794672的博客 139

Oracle数据库安装及配置(Oracle19c)

Windows 10安装Oracle 19c教程

weixin_69200200的博客 2万+

weblogic 集群搭建,双机部署,相关问题记录

4.5.集群搭建1 参考网址https://wenku.baidu.com/view/76365129cd1755270722192e453610661ed95aa4.html IP地址 端口 角色 192.168.1.160 9001 AdminServer ...

zl386119974的专栏 1669

uva 106 - Fermat vs. Pythagoras(素勾股数)

题目大意:uva 106 - Fermat vs. Pythagoras 题目大意:给出n,计算n以内有多少对素勾股数,并计算出n以内有多少数可以用来组成勾股数。 解题思路:暴力应该是会超时,本题肯定是考查勾股数的性质,上维基查了一下勾股数,上面讲的很清楚,只要将构造方法实现就好了。 #include #include #include const int

不慌不忙、不急不躁 2294

从半导体物理到实战:手把手教你测量MOS管体二极管的Vf和Trr

本文深入探讨了MOSFET体二极管的关键参数测量方法,重点讲解了正向压降(Vf)和反向恢复时间(Trr)的测量原理与实战步骤。通过搭建测试平台,使用万用表和示波器等工具,手把手指导如何量化这些影响开关损耗和效率的核心参数,并对比分析了硅基与碳化硅(SiC)器件在体二极管性能上的显著差异。

n7o8p的博客 340

UVA106 - Fermat vs. Pythagoras(素勾股数)

UVA106 - Fermat vs. Pythagoras(素勾股数) 题目链接 题目大意:给你一个数n,勾股数三元组(x,y,z)的定义:满足x 解题思路:先找出所有的素勾股数(x, y, z) ,那么便可以通过(kx, ky, kz)得到不是素勾股数的勾股数。接着要换种方式构造素勾股数,公式:x = m^2 - n^2; y = 2∗m∗n; z = m^2 + n^

1473

智能体异常行为检测-基于行为序列的异常模式识别技术【附代码】

在多智能体系统(Multi-Agent Systems, MAS)中,智能体之间的行为协调与自主决策是系统稳定运行的关键。然而,当某个智能体因策略偏移、环境变化或潜在攻击而出现**异常行为(Abnormal Behavior)**时,整个系统可能陷入混乱或崩溃。

一键难忘的博客 1143

YOLOv8【第十一章:视频追踪与流处理篇·第3节】匈牙利算法(Hungarian Algorithm)与 KM 算法匹配机制详解!

🏆 本文收录于 《YOLOv8实战:从入门到深度优化》 专栏。该专栏系统复现并梳理全网各类 YOLOv8 改进与实战案例(当前已覆盖分类 / 检测 / 分割 / 追踪 / 关键点 / OBB 检测等方向),坚持持续更新 + 深度解析,质量分长期稳定在 97 分以上,可视为当前市面上 覆盖较全、更新较快、实战导向极强 的 YOLO 改进系列内容之一。

**My Coding Family** 443

uva106 - Fermat vs. Pythagoras()

这道题暴力果断的不行啊, n会到达10^2,如果暴力的话,复杂度怎么也得O(n*n). 所以我们压根就不能对a,b,c中的任何数暴力, 但这里有个公式,我也是做了这道题才知道的。 (r*r-s*s)^2+(2*r*s)^2 = (r*r+s*s)^2; 我们只要枚举r和s即可。 效率大大提高了, 下面给出一位大神的证明: [解题方法]    该题可以归结为数论问题。

稻草人 1052

3D 结构光相机工作原理全解 | 从编码投射到三维重建,原理 + 代码 + 工业实战一站式掌握

本文从3D结构光相机的整体架构出发,层层拆解了编码投射、相位计算、相位解包裹、三维重建的核心原理,补充了工业实战所需的公式推导、代码实现、参数调优、设备选型和案例落地,核心总结如下:相位是3D结构光成像的“灵魂”,其精度、稳定性直接决定3D重建的效果,包裹相位与绝对相位的区分的是工业实战的基础;编码投射的核心是“正弦条纹+格雷码条纹”组合,正弦条纹负责高精度相位提取,格雷码条纹辅助解包裹,适配工业场景的精度与稳定性需求;

getusushu的博客 414

poj1305 Fermat vs. Pythagoras----勾股数

Fermat vs. Pythagoras Time Limit: 2000MS   Memory Limit: 10000K Total Submissions: 1026   Accepted: 593 Description Computer generated and assisted proofs and verification

Shirley 1342

8、对比 LightGBM、XGBoost 和深度学习

本文对比了LightGBM、XGBoost和深度学习模型(特别是TabTransformer)在处理表格数据时的性能差异。从模型复杂度、数据准备、训练时间、准确性和适用场景等方面进行分析,重点探讨了三者在人口普查收入预测任务中的表现。文章详细介绍了XGBoost与LightGBM的核心特性与区别,深入讲解了TabTransformer的架构与实现,并涵盖了分类特征编码、缺失值处理和类别不平衡等关键数据预处理技术,为选择合适的表格数据建模方法提供了实践指导。

julia4scientist的博客 120

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

UVa 106 && POJ 1305 - Fermat vs. Pythagoras(毕达哥拉斯三元组)

增加两个变量m,n; 令x=m^2-n^2;     y=2*m*n;     z=m^2+n^2;(m、n奇偶性不同) 对m、n进行枚举即可,注意控制上限,以免超时 #include #include #include using namespace std; int gcd(int a,int b) { return b==0?a:gcd(b,a%b); }

DC的ACM奋斗史 876

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

UVa 106 - Fermat vs. Pythagoras

题目:找到小于N的勾股数组的朴素解(三个数互质),并找到[1, N]中所有勾股数组中未出现过的数字个数。 分析:数论。这里直接利用《原本》中的解法即可。             x = 2st,y = s^2 - t^2,z = s^2 + t^2,             其中:1.s > t;(枚举顺序)                         2.s和t互质;(朴素解)  

小白菜又菜 1186

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain&#39;s apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = &quot;R. B. Palm&quot;, title = &quot;Prediction as a candidate for learning deep hierarchical models of data&quot;, year = &quot;2012&quot;,}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath(&#39;DeepLearnToolbox&#39;));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand(&#39;state&#39;,0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W&#39;); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand(&#39;state&#39;,0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = &#39;sigm&#39;;%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.10, &#39;Too big error&#39;);```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand(&#39;state&#39;,0)sae = saesetup([784 100]);sae.ae{1}.activation_function = &#39;sigm&#39;;sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)&#39;)% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;;nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.16, &#39;Too big error&#39;);```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x&#39;,28,28,60000))/255;test_x = double(reshape(test_x&#39;,28,28,10000))/255;train_y = double(train_y&#39;);test_y = double(test_y&#39;);%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you&#39;ll get around 1.2% errorrand(&#39;state&#39;,0)cnn.layers = { struct(&#39;type&#39;, &#39;i&#39;) %input layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 6, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %sub sampling layer struct(&#39;type&#39;, &#39;c&#39;, &#39;outputmaps&#39;, 12, &#39;kernelsize&#39;, 5) %convolution layer struct(&#39;type&#39;, &#39;s&#39;, &#39;scale&#39;, 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er&lt;0.12, &#39;Too big error&#39;);```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.08, &#39;Too big error&#39;);%% ex2 neural net with L2 weight decayrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex3 neural net with dropoutrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex4 neural net with sigmoid activation functionrand(&#39;state&#39;,0)nn = nnsetup([784 100 10]);nn.activation_function = &#39;sigm&#39;; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex5 plotting functionalityrand(&#39;state&#39;,0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = &#39;softmax&#39;; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand(&#39;state&#39;,0)nn = nnsetup([784 20 10]); nn.output = &#39;softmax&#39;; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er &lt; 0.1, &#39;Too big error&#39;);```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free &quot;Bitdeli Badge&quot;)

Uva 106-Fermat vs. Pythagoras(勾股数性质)

题目链接:点击打开链接 题意:给出N,x^2+y^2=z^2 小于等于N的解(互素)的个数以及小于N的个数除掉所有解(包括不互素)已经用掉的数。 度娘给出勾股数的定义:只考虑互素的解,给出勾股数公式 a=2*m*n ,b=m*m-n*n ,c=m*m+n*n;  枚举m,n ,复杂度 O(log(N)^2) #include #include #include #include #i

Orz~~Orz 979

uva106 - Fermat vs. Pythagoras 互素勾股数

Fermat vs. Pythagoras  Background Computer generated and assisted proofs and verification occupy a small niche in the realm of Computer Science. The first proof of the four-color probl

2011

UVA10954

题意:求出一数组相加的和,但要求相加时产生的数的总和最小,可以看下题目给出的1,2,3 的样例 思路:贪心,每次都取出数组最小的两个数进行相加,再把得到的数返回到数组中,直到相加结束,刚一开始做,在相加完的数组,直接用sort重新排序,结果就是超时了。。。。。。之后就重写了排序的函数,过了。。。 #include #include #include #include #define N

懒人一枚。 1091

UVA 106 (勾股数)

#include #include bool is[1000010]; int gcd(int a,int b) { int r=1; while(r){ r=a%b; a=b;b=r; } return a; } int main() { int n; while(scanf("%d",&n)!=EOF){ int ans1=0,ans2=0; for(int i

jinjiahao5299的专栏 602

UVA10602

题意:输入一些字符串,要求所需按键的次数最小 思路:先将所有字符串排序,然后再将前一个与后一个比较,找出要按键的次数,然后进行累加,刚开始WA是因为写成,计算两个字符串不同的字母,实际是要在出现第一个字符出现不同后,后面的字符全都要用按键输入了。 #include #include #include #define N 105 using namespace std; char

懒人一枚。 831

uva106(勾股数)

题目的意思就是给出一个数字.在小于等于这个数的范围内.找勾股数,即x^2 +y^2 = z^2; 问有多少互质的勾股数,然后除掉所有勾股数(包括不互质的),还剩几个数字. 根据公式 x^2 +y^2 = z^2则: x = i * i - j*j; y = 2*i* j; z  = i*i + j * j; 遍历i,j就行了.. 注意i,j只需遍历到sqrt(n); AC

潜水的程序猿 610

uva--10602+贪心

题意:    某公司开发了一个编辑器,支持两条语音命令:1.重复最后一个单词,2.删除最后一个单词的最后一个字母。给你一系列的单词,问利用编辑器的功能最少需要输入多少个字母就可以将所有单词输入;要求第一个单词一定要第一个输入,其余单词不限顺序。 思路:    输入第一个单词后,我们可以怎么样选择能使得输入的字母数最少?当然我们需要选择和第一个单词有最长公共前缀的单词;这就是贪心的策略:每次都

荒野孤鹰 702

uva 106

题意:给出一个n,问小于等于n的数中有多少组互质勾股数,除了所有勾股数外还有多少个数字。 题解:有一个勾股数的公式,其中a b c是勾股数。 int a = i * i + j * j; int b = 2 * i * j; int c = i * i - j * j; 然后将a、b、c扩大k倍(小于等于n),避免遗漏,然后写gcd函数判断是否互质。 #include #in

俯瞰风景 926
上一篇: 关于费马小定理的一个奇妙的组合证明
下一篇: UVa 10006 Carmichael Numbers(数论&卡米歇尔数&考塞特判别法)
synapse7
博客等级 码龄14年 432粉丝 701原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值