Help Hanzo(区间求素数) LightOJ
来源:互联网 发布:韦德上赛季场均数据 编辑:程序博客网 时间:2024/06/05 08:34
Amakusa, 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!
Input
Input 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).
Output
For each case, print the case number and the number of safe territories.
Sample Input
3
2 36
3 73
3 11
Sample Output
Case 1: 11
Case 3: 4
Hint
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!
Input
Input 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).
Output
For each case, print the case number and the number of safe territories.
Sample Input
3
2 36
3 73
3 11
Sample Output
Case 1: 11
Case 2: 20
Case 3: 4
A number is said to be prime if it is divisible by exactly two different integers. So, first few primes are 2, 3, 5, 7, 11, 13, 17, ...
#include <bits/stdc++.h>using namespace std;const int maxn =1e6+15;typedef long long ll;bool vis[maxn];int prime[maxn];bool vis_two[maxn];int step=0;ll a,b;void get_prime(){ ll i,j,k; memset(vis,0,sizeof(vis)); for (i=2;i<maxn;i++) { if(!vis[i]) { prime[step++]=i; for (j=i<<1;j<maxn;j+=i) vis[j]=1; } }}ll solve (){ ll result =0; ll i,j, k; memset(vis_two,0,sizeof(vis_two)); for (i=0;i<step;i++) { if ((ll)prime[i]*prime[i]>b) break; ll t=a/prime[i]; while (t*prime[i]<a||t<=1) t++; t*=prime[i]; for (;t<=b;t+=prime[i]) { vis_two[t-a]=1; } } for (i=0;i<=b-a;i++) if(!vis_two[i]) result++; return result ;}int main (){ int t; cin >>t; get_prime(); int cnt =1; ll i,j,k; while (t--) { ll ans =0; cin>>a>>b;// if (b<maxn)// {// for (i=a;i<=b;i++)// if(!vis[i])// ans++;// }// else ans =solve(); if(a==1) ans--; printf("Case %d: %lld\n",cnt++,ans); } return 0;}
阅读全文
0 0
- Help Hanzo(区间求素数) LightOJ
- LightOJ 1197 Help Hanzo 求区间内素数的个数
- lightOJ 1197 Help Hanzo (区间找素数)
- LightOJ 1197 Help Hanzo(区间素数筛)
- LightOJ 1197 Help Hanzo(区间素数筛选)
- LightOJ 1197Help Hanzo (区间素数筛选法)
- LightOJ 1197 - Help Hanzo (模拟筛素法区间筛素)
- LightOJ - 1197 Help Hanzo 素数筛
- 【区间筛法】 LightOJ 1197 Help Hanzo
- LOJ - 1197 - 《Help Hanzo》【区间素数】
- Help Hanzo (素数筛+区间枚举)
- Help Hanzo 区间的素数的个数
- E - Help Hanzo(LightOJ 1197)
- LightOJ 1197 Help Hanzo
- Help Hanzo LightOJ
- LightOJ 1197 Help Hanzo
- lightOJ 1197 Help Hanzo 两阶段素数筛选
- Help Hanzo(素数筛)
- nyoj 119 士兵杀敌(三)线段树
- MySQL数据表基本
- 进程间通信方式(二)信号量
- 【Android】获取所有jpg和png图片
- 2017-10-22 HDU 2082 普通型母函数
- Help Hanzo(区间求素数) LightOJ
- 10.20校内ACM G题站队
- 476. Number Complement
- JS制作简单的三级联动
- stm32F4系列出现HardFault_Handler解决方法
- nitoj_202_二分——将区间分为三个部分
- [python爬虫] 正则表达式使用技巧及爬取个人博客实例
- 洛谷 P2024 食物链
- 101 单调性及极值复习 型三:方程根