LightOJ - 1197

unordered_set有时候还是挺毒瘤的

const int N = 1e6 + 5;

int t;
int cnt, p[N];
bool book[N];
void x_x()
{
	book[1] = true;//细节
	f(i, 2, N)
	{
		if (!book[i])p[cnt++] = i;
		for (int j = 0;p[j] <= N / i;++j)
		{
			book[p[j] * i] = true;
			if (i%p[j] == 0)break;
		}
	}
}
int book2[100010];

int main()//数据区间达到2e9以上,直接筛爆掉,筛一半,然后对于大数据再用素数表数据筛
{		//区间素数筛
	int c = 0;
	x_x();
	cin >> t;
	ll a, b;
	while (t--)
	{
		ll ans = 0;
		scanf("%lld %lld", &a, &b);
		if (b <=1000005)
		{
			for(ll i = a;i <= b;++i)if (!book[i])ans++;
		}
		else {
			
			memset(book2, 0, sizeof book2);
			f(i, 0, cnt - 1)
			{
				ll k = a / p[i];
				if (k*p[i] < a)k++;//大于左端的第一个倍数
				for (ll j = k * p[i];j <= b;j += p[i])
				{
					book2[j - a] = true;//1 2 10
				}
			}
		
			for (ll i = a;i <= b;++i)if (!book2[i-a])ans++;
		}
		printf("Case %d: %lld\n", ++c, ans);
	}
	return 0;
}
发布了22 篇原创文章 · 获赞 2 · 访问量 368

猜你喜欢

转载自blog.csdn.net/qq_43543086/article/details/104672591