Fermat’s Chirstmas Theorem

2023-11-08 11:33
文章标签 fermat theorem chirstmas

本文主要是介绍Fermat’s Chirstmas Theorem,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Fermat’s Chirstmas Theorem

Time Limit: 1000MS Memory limit: 65536K

题目描述

In a letter dated December 25, 1640; the great mathematician Pierre de Fermat wrote to Marin Mersenne that he just proved that an odd prime p is expressible as p = a2 + b2 if and only if p is expressible as p = 4c + 1. As usual, Fermat didn’t include the proof, and as far as we know, never
wrote it down. It wasn’t until 100 years later that no one other than Euler proved this theorem.
To illustrate, each of the following primes can be expressed as the sum of two squares:
5 = 2 2 + 1 2
13 = 3 2 + 2 2
17 = 4 2 + 1 2
41 = 5 2 + 4 2
Whereas the primes 11, 19, 23, and 31 cannot be expressed as a sum of two squares. Write a program to count the number of primes that can be expressed as sum of squares within a given interval.
 

输入

Your program will be tested on one or more test cases. Each test case is specified on a separate input line that specifies two integers L, U where L ≤ U < 1, 000, 000
The last line of the input file includes a dummy test case with both L = U = −1.

输出

L U x y
where L and U are as specified in the input. x is the total number of primes within the interval [L, U ] (inclusive,) and y is the total number of primes (also within [L, U ]) that can be expressed as a sum of squares.

示例输入

10 20
11 19
100 1000
-1 -1

示例输出

10 20 4 2
11 19 4 2
100 1000 143 69

 

 

 

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int M=1000005;
int prime[1000005];
int flag[1000005];
int p_num;
void getP()
{
int i, j;
p_num = 0;
memset(flag, 0, sizeof(flag) );
for ( i = 2; i < M; i++ )
{
if ( !flag[i] ) prime[p_num++] = i;
for ( j = 0; j < p_num && i * prime[j] < M; j++ )
{
flag[ i * prime[j] ] = 1;
}
}
}
int main()
{
int l,u,x,y;
getP();
while(scanf("%d%d",&l,&u))
{
x=0;
y=0;
if(l==-1&&u==-1)
break;
for(int i=0;i<p_num;i++)
{
if(prime[i]>=l&&prime[i]<=u)
{
x++;
if(prime[i]%4==1)
y++;
}
if(prime[i]>u)
break;
}
if(l<=2&&u>=2) y++;
printf("%d %d %d %d\n",l,u,x,y);
}
return 0;
}


 

这篇关于Fermat’s Chirstmas Theorem的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/369579

相关文章

Theorem,Proposition, Lemma 和 Corollary是什么 区别关系

文章中最重要的几个结论用 Prop 或 Thm. 其中有比较普遍意义的(可能被他人引用的)用Thm,比较特定、适用范围不大的用Prop。 用来推出这些 Thm 或 Prop 的引理用 Lemma. Theorem:定理。是文章中重要的数学化的论述,一般有严格的数学证明。 Proposition:可以翻译为命题,经过证明且interesting,但没有Theorem重要,比较常用。

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

UVA106 - Fermat vs. Pythagoras(素勾股数) 题目链接 题目大意:给你一个数n,勾股数三元组(x,y,z)的定义:满足x < y < z, x^2 + y^2 = z^2.现在问这里里面有多少个三元组是素勾股数即满足x,y, z两两互质。并且判断剩下的1-n的数有多少是没有出现在勾股数三元组中。 解题思路:先找出所有的素勾股数(x, y, z) ,那么便可

POJ 3006 Dirichlet's Theorem on Arithmetic Progressions

分析: 这道题要先用筛法求出10^6以内的素数。。。。我竟然觉得数据太多没用这种方式,然后写出来的代码就运行超时了,呜呜……最后还是用的筛法 Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing b

POJ1305 Fermat vs. Pythagoras【毕达哥拉斯三元组】

题目链接: http://poj.org/problem?id=1305 题目大意: 给一个整数N,求N范围内的本原的毕达哥拉斯三元组的个数,以及N以内毕达哥拉斯三元组不涉及 数的个数。 思路: 本原毕达哥拉斯三元组x^2 + y^2 = z^2 满足 x = m^2 - n^2,y = 2*m*n,z = m^2 + n^2,其 中m > n,且若m为奇数,则n为偶数

HDU1788 Chinese remainder theorem again【中国剩余定理】

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1788 题目大意: 题目前边的描述是多余的。。。一个正整N除以M1余M1-a,除以M2余M2-a,除以M3余M3-a, 即除以Mi余Mi-a(a < Mi < 100),求满足条件的最小的数。 思路: 这是一道中国剩余定理的基础题。由题目得出N % Mi + a = Mi,即

策梅洛定理 (博弈论): Zermelo's theorem

很有意思的一个定理。 转载地址为http://blog.sina.com.cn/s/blog_4b91d3b501010hcj.html 策梅洛定理(英语:Zermelo's theorem)是博弈论的一条定理,以恩斯特·策梅洛命名。定理表示在二人的有限游戏中,如果双方皆拥有完全的资讯,并且运气因素并不牵涉在游戏中,那先行或后行者当中必有一方有必胜/必不败的策略。若应用至国际象棋,则策梅

Codeforces Round 951 (Div. 2) F. Kostyanych‘s Theorem(思维题 交互好题)

题目 交互题,n(n<=1e5)个点的完全图,无向的,初始恰好删了n-2条边 每次询问可以输入一个d:? d 交互器会输出一个当前度>=d的点v, 如果有多个这样的点,输出度最小的,如果还有多个,输出点号最小的 还会输出一个和这个点v当前没有连边的点x,如果x有多个,也输出点号最小的x 如果x不存在,输出x=0 然后交互器会把v这个点和当前连的所有边都删了, 如果没有找到这样的v,

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

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

uva 11178 Morley's Theorem

题意: Morley定理:作三角形ABC每个内角的三等分线,相交成三角形DEF,则DEF是等边三角形。 你的任务是根据A、B、C3个点的位置确定D、E、F3个点的位置。 分析: 根据三点的坐标,我们可以确定每条三等分线的直线方程P = P0+tv,P0是直线上一点,v是方向向量,t为参数。两两求交点即可得到D、E、F的坐标,求交点的代码参考了刘汝佳的大白书,对于方程是怎么得到的不理解

UVA 11178 - Morley's Theorem(计算几何)

这是一道基础的计算几何,基本自己推推就能推出来了,基本思路就是根据3点,求出角度,就可以知道要旋转的角度,然后求出两个旋转后的向量求交点输出即可 代码: #include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;struct Point {double x,