质数距离
l,r,都非常大,但l−rl-rl−r很小所以晒出1至sqrt(r)1至sqrt(r)1至sqrt(r)的质数,再用倍数法求解
注意:质数为iii时在数组中村i−li-li−l,不然数组会爆
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 100006, L = 1000006, M = 46340, INF = 0x7fffffff;
bool v[L];
vector<int> p, ans;
int main() {
memset(v, 1, sizeof(v));
for (int i = 2; i <= M; i++)
if (v[i]) {
p.push_back(i);
for (int j = 2; j <= M / i; j++) v[i*j] = 0;
}
unsigned int l, r;
while (cin >> l >> r) {
memset(v, 1, sizeof(v));
ans.clear();
if (l == 1) v[0] = 0;
for (unsigned int i = 0; i < p.size(); i++)
for (unsigned int j = (l - 1) / p[i] + 1; j <= r / p[i]; j++)
if (j > 1) v[p[i]*j-l] = 0;
for (unsigned int i = l; i <= r; i++)
if (v[i-l]) ans.push_back(i);
int minn = INF, maxx = 0, x1, y1, x2, y2;
for (unsigned int i = 0; i + 1 < ans.size(); i++) {
int num = ans[i+1] - ans[i];
if (num < minn) {
minn = num;
x1 = ans[i];
y1 = ans[i+1];
}
if (num > maxx) {
maxx = num;
x2 = ans[i];
y2 = ans[i+1];
}
}
if (!maxx) puts("There are no adjacent primes.");
else printf("%d,%d are closest, %d,%d are most distant.\n", x1, y1, x2, y2);
}
return 0;
}
阶乘分解
#include<bits/stdc++.h>
using namespace std;
inline long long read(){
long long num=0;int z=1;char c=getchar();
if(c=='-') z=-1;
while((c<'0'||c>'9')&&c!='-') c=getchar();
if(c=='-') z=-1,c=getchar();
while(c>='0'&&c<='9') num=(num<<1)+(num<<3)+(c^48),c=getchar();
return z*num;
}
#define _for(i,a,b) for(int i=a;i<=b;i++)
int n;
const int maxx=1000005;
int v[maxx],s[maxx];
int tot;
void find()
{
_for(i,2,n)
{
if(v[i])continue;
++tot;
s[tot]=i;
_for(j,2,n/i)v[i*j]=1;
}
}
void work()
{
_for(i,1,tot)
{
int p=s[i];int c=0;
for(int j=n;j;j=j/p)
{
c+=j/p;
}
printf("%d %d\n",s[i],c);
}
}
int main(){
n=read();
find();
work();
return 0;
}