zoj1842 Prime Distance
2006-11-11 17:30
330 查看
//zoj1842 Prime Distance
//Accepted 1842 C++ 00:00.13 1852K
//Sieve of Eratosthenes
//Sieve is the best prime generator algorithm
//http://www.comp.nus.edu.sg/~stevenha/programming/prog_mathematics.html#Prime%20Numbers
#include <iostream>
#include <string>
using namespace std;
typedef long long elem;
elem plist[4800],pcnt;
void getprime()
{
plist[0] = 2, plist[1] = 3, pcnt = 2;
for (long n=5,i; n<=47000; n+=2) {
for (i=1; plist[i]*plist[i]<=n && n%plist[i]; i++);
if (plist[i]*plist[i] > n) plist[pcnt++] = n;
}
}
bool flag[1000010];
void sieve (elem L, elem U)
{
elem c[2],d[2],first;
if (L == 1) L++;
memset(flag, 1, sizeof(flag[0])*(U-L+1));
for (elem i=0,j; plist[i]*plist[i]<=U; i++) {
j = L/plist[i]+(L%plist[i]>0);
if (j == 1) j++;
for (j*=plist[i]; j<=U; j+=plist[i]) flag[j-L] = 0;
}
c[0] = c[1] = d[0] = d[1] = 0;
first = 0;
for (elem i=L; i<=U; i++) {
if (flag[i-L]) {
if (first) {
if (!c[0] || c[0] && i-first<c[1]-c[0])
c[0] = first, c[1] = i;
if (!d[0] || d[0] && i-first>d[1]-d[0])
d[0] = first, d[1] = i;
}
first = i;
}
}
if (c[0]) cout << c[0] << "," << c[1] << " are closest, " << d[0] << "," << d[1] << " are most distant." << endl;
else cout << "There are no adjacent primes." << endl;
}
int main()
{
elem L,U;
getprime();
while (cin >> L >> U)
sieve (L,U);
return 0;
}
//Accepted 1842 C++ 00:00.13 1852K
//Sieve of Eratosthenes
//Sieve is the best prime generator algorithm
//http://www.comp.nus.edu.sg/~stevenha/programming/prog_mathematics.html#Prime%20Numbers
#include <iostream>
#include <string>
using namespace std;
typedef long long elem;
elem plist[4800],pcnt;
void getprime()
{
plist[0] = 2, plist[1] = 3, pcnt = 2;
for (long n=5,i; n<=47000; n+=2) {
for (i=1; plist[i]*plist[i]<=n && n%plist[i]; i++);
if (plist[i]*plist[i] > n) plist[pcnt++] = n;
}
}
bool flag[1000010];
void sieve (elem L, elem U)
{
elem c[2],d[2],first;
if (L == 1) L++;
memset(flag, 1, sizeof(flag[0])*(U-L+1));
for (elem i=0,j; plist[i]*plist[i]<=U; i++) {
j = L/plist[i]+(L%plist[i]>0);
if (j == 1) j++;
for (j*=plist[i]; j<=U; j+=plist[i]) flag[j-L] = 0;
}
c[0] = c[1] = d[0] = d[1] = 0;
first = 0;
for (elem i=L; i<=U; i++) {
if (flag[i-L]) {
if (first) {
if (!c[0] || c[0] && i-first<c[1]-c[0])
c[0] = first, c[1] = i;
if (!d[0] || d[0] && i-first>d[1]-d[0])
d[0] = first, d[1] = i;
}
first = i;
}
}
if (c[0]) cout << c[0] << "," << c[1] << " are closest, " << d[0] << "," << d[1] << " are most distant." << endl;
else cout << "There are no adjacent primes." << endl;
}
int main()
{
elem L,U;
getprime();
while (cin >> L >> U)
sieve (L,U);
return 0;
}
相关文章推荐
- ZOJ 1842 Prime Distance(素数筛选法2次使用)
- zoj 1842 Prime Distance
- POJ 2689/ ZOJ 1842: Prime Distance[两次筛法/筛选法平移]
- ZOJ 1842 Prime Distance(素数筛选法2次使用)
- zoj1457 Prime Ring Problem DFS
- poj 2689 Prime Distance 二次筛素数
- ZOJ 1457 Prime Ring Problem @Z
- POJ 2689 Prime Distance(筛选两次素数)
- 【cqbzoj】:1330 Prime DP(Ahio2001 质数和分解)
- poj 2689 Prime Distance 筛法+区间筛素数
- POJ 2689 Prime Distance 素数筛选 -
- poj 2689 Prime Distance(大区间素数)
- poj 2689 Prime Distance
- poj 2689 Prime Distance
- CC Prime Distance On Tree (树的点分治 + FFT)
- poj 2689 Prime Distance 二次筛法,找出给定区间所有约数
- ZOJ 3911 Prime Query
- ZOJ 2723 Semi-Prime ||ZOJ 2060 Fibonacci Again 水水水!
- POJ 2689 Prime Distance(大区间素数筛法,两次筛法)
- POJ 2688 Prime Distance