Exploding CPU
题目链接
题目大意
规定一种数 explosive number,假设x是这种数,则x需要满足
x=p0p1p2...pnp0=1pi=Api−1+B
现在给你一个范围,要你求出这个范围内的所有explosive number。
题解
这一题的关键是要推出A和B的范围,因为p最多只有10个,最小有3个,我们可以确定A和B的范围然后直接枚举质因子进行打表。而经过推算A和B有不是很大,所以此题采用打表。
POJ上有一段很经典的推理,这里给出链接:A和B范围的推算
代码
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std;
int p[60000],h,num[1005],t,T,low,high;
bool vis[600005];
void deal(int high)
{
h=0;
p[h++]=2;
for (int i=2;i<=high;i+=2) vis[i]=1;
for (int i=3;i<=high;i+=2) if (!vis[i])
{
p[h++]=i;
for (int j=i;j<=high;j+=i) vis[j]=1;
}
}
int ser(int low,int high,LL x,int p[])
{
int l=low,r=high,mid;
while(l!=r)
{
mid=(l+r)>>1;
if (x<=p[mid]) r=mid;
else l=mid+1;
}
return l;
}
void solve()
{
LL pri[15];
t=0;
for (int a=1;a<=600;a++)
for (int b=-700;b<=700;b++)
{
int i;
memset(pri,0,sizeof(pri));
pri[0]=1;
for (i=1;i<=10;i++)
{
pri[i]=pri[i-1]*a+b;
if (p[ser(0,h-1,pri[i],p)]!=pri[i]) break;
}
if (i<4) continue;
LL tmp=pri[1]*pri[2]*pri[3];
if (tmp>2000000000) continue;
num[t++]=tmp;
for (int j=4;j<i;j++)
{
tmp*=pri[j];
if (tmp<=2000000000) num[t++]=tmp;
else break;
}
}
sort(num,num+t);
}
int main()
{
deal(600000);
solve();
scanf("%d",&T);
while(T--)
{
int l,r;
scanf("%d%d",&low,&high);
l=ser(0,t-1,low,num);
r=ser(0,t-1,high,num);
if (num[r]>high) r--;
printf("%d\n",r-l+1);
}
return 0;
}