E - Help Hanzo(LightOJ 1197)
发布日期:2021-08-26 15:47:32 浏览次数:2 分类:技术文章

本文共 2260 字,大约阅读时间需要 7 分钟。

Pssword: nefu

DescriptionAmakusa, the evil spiritual leader has captured the beautiful princess Nakururu. The reason behind this is he had a little problem with Hanzo Hattori, the best ninja and the love of Nakururu. After hearing the news Hanzo got extremely angry. But he is clever and smart, so, he kept himself cool and made a plan to face Amakusa.Before reaching Amakusa's castle, Hanzo has to pass some territories. The territories are numbered as a, a+1, a+2, a+3 ... b. But not all the territories are safe for Hanzo because there can be other fighters waiting for him. Actually he is not afraid of them, but as he is facing Amakusa, he has to save his stamina as much as possible.He calculated that the territories which are primes are safe for him. Now given a and b he needs to know how many territories are safe for him. But he is busy with other plans, so he hired you to solve this small problem!InputInput starts with an integer T (≤ 200), denoting the number of test cases.Each case contains a line containing two integers a and b (1 ≤ a ≤ b < 231, b - a ≤ 100000).OutputFor each case, print the case number and the number of safe territories.Sample Input32 363 733 11Sample OutputCase 1: 11Case 2: 20Case 3: 4

题目大意:

就是给定一个区间 让你求这个区间有多少个素数,数据范围(1 ≤ a ≤ b < 2^31, b - a ≤ 100000).

解题思路:

首先看到数据范围还是比较大的, 但是他们之间的区间差很小,所以我们就考虑用区间差值计算,因为我们不可能开一个 2^31次方的数组,根本开不了那么大,所以我们采用的方法就是筛法,首先进行第一次素数筛,将 1—1e6之间的素数筛出来,筛完之后我们要做的是在[0,b-a]区间进行筛,怎么筛呢,我就详细的说一下啦:
首先进行 for 循环,从0—b-a,而且得保证p[i]*p[i]<=b(p[i]是存的素数值)让 a 对每一个p[i]值进行取余,我们要筛的就是(a+(p[i]-a%p[i]))%p[i] == 0的数,然后就跟素数筛差不多啦…具体还得看我的代码 代码还是挺好理解的 ^_^
上代码:

#include 
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;#define MM(a) memset(a,0,sizeof(a))typedef long long LL;typedef unsigned long long ULL;const int MAXN = 1e6+5;const int mod = 1000000007;const double eps = 1e-7;bool prime[MAXN];LL p[MAXN];LL k;void isprime()///素数筛{ k = 0; prime[1] = false; memset(prime, true, sizeof(prime)); for(LL i=2; i
>T; for(int cas=1; cas<=T; cas++) { cin>>a>>b; ShaiXuan(); LL ret = 0; if(a == 1) ret = -1; for(int i=0; i<=tmp; i++) if(ok[i]) ret++; printf("Case %d: %lld\n",cas,ret); } return 0;}

转载地址:https://blog.csdn.net/weixin_33890499/article/details/90688855 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Cocos2d-x 3.0坐标系详解
下一篇:普通时间与Unix时间戳的转换方法

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2024年04月04日 21时26分29秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章