一些前置知识可以看一下我的联赛前数学知识 如何判断一个数是否为质数 方法一:试除法 扫描\(2\sim \sqrt{n}\)之间的所有整数,依次检查它们能否整除\(n\),若都不能整除,则\(n\)是质数,否则\(n\)是合数. 代码 bool is_prime(int n){ if(n<2) return 0; int m=sqrt(n); for(int i=2;i<=m;i++){ if(n%i==0) return 0; } return 1; } 方法二.线性筛 用 \(O(n)\)…
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the num…
GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9756Accepted: 1819 Description Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b.…
Prime Test Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 29193 Accepted: 7392 Case Time Limit: 4000MS Description Given a big integer number, you are required to find out whether it's a prime number. Input The first line contains the num…
Eddy's research I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6664    Accepted Submission(s): 3997 Problem Description Eddy's interest is very extensive, recently he is interested in prime…
这绿题贼水...... 原理我不讲了,随便拿张草稿纸推一下就明白了. #include <cstdio> using namespace std; ; int su[N],ans,top; bool vis[N]; void shai(int b) { ;i<=b;i++) { if(!vis[i]) { su[top++]=i; } ;j<top && i*su[j]<=b;j++) { vis[su[j]*i]=; ) break; } } return;…
嗯.... 埃氏筛和欧拉筛的思想都是相似的: 如果一个数是素数,那么它的所有倍数都不是素数.... 这里主要介绍一下欧拉筛的思路:(欧拉筛的复杂度大约在O(n)左右... 定义一个prime数组,这个数组被称为“素数表”,里面的数都为素数:然后用一个vis数组,如果一个数不是素数,则标记为1. 然后把i从2到n进行枚举,如果它没被访问过,则将其加入素数表中:然后for循环素数表,如果i % prime[j] == 0,则break即可, 因为prime[j]作为i的一个质因数,在某一种情况下,它…
Description 如题,给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内) Input&Output Input 第一行包含两个正整数N.M,分别表示查询的范围和查询的个数. 接下来M行每行包含一个不小于1且不大于N的整数,即询问该数是否为质数. Output 输出包含M行,每行为Yes或No,即依次为每一个询问的结果. Solution 欧拉筛法的优势在于,在当前i mod 当前素数为0时就退出,保证了每个合数一定只被它的最小素因子筛掉,从而在O(n)时间内…
题目:给定一个范围N,你需要处理M个某数字是否为质数的询问(每个数字均在范围1-N内).(N<=10000000,M<=100000) 解法:1.欧拉筛O(n),数组近乎100KB:2.(我这题copy了数据范围肯定是有原因滴......)欧拉函数判断素数O(m log n),m 比 n 小啊,可以分解质因数求欧拉函数. 2种写法我在这篇博文里都有写:[poj 2407]Relatives(数论--欧拉函数 模版题) 下面的代码是第一种方法的, 1 #include<cstdio>…
#include<iostream> #include<cstdio> #include<queue> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<ctime> #define lson l, m, rt<<1 #define rson m+1, r, rt<<1|1 #…