1197 - Help Hanzo
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.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.
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 |
Output for Sample Input |
32 363 733 11 | Case 1: 11Case 2: 20Case 3: 4 |
Note
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, ...
题目类型:素数
算法分析:由于问题的规模比较大,不可能将所有的范围内的素数都打表出来,只能将1~sqrt (2 ^ 31 - 1)内的素数先打表出来,然后再对于每个给定的区间(区间长度小于1000000)进行求解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 50000 + 66; bool prime[maxn*20]; long long primelist[maxn], prime_len; void GetPrime () { memset (prime, true, sizeof (prime)); prime_len = 0; long long i; for (i = 2; i <= maxn; i++) { if (prime[i]) primelist[prime_len++] = i; long long j; for (j = 0; j < prime_len; j++) { if (i * primelist[j] > maxn) break; prime[i*primelist[j]] = false; if (i % primelist[j] == 0) break; } } } void Solve (long long a, long long b) { memset (prime, true, sizeof (prime)); for (long long i = 0; i < prime_len; i++) { long long temp = a / primelist[i]; while (temp * primelist[i] < a || temp <= 1) temp++; for (long long j = temp * primelist[i]; j <= b; j += primelist[i]) { if (j >= a) prime[j-a] = false; } } if (a == 1) prime[0] = false; long long tot = 0; for (long long i = a; i <= b; i++) if (prime[i-a]) tot++; printf ("%lld\n", tot); } int main() { GetPrime (); // freopen ("aaa.txt", "r", stdin); int t, flag = 1; scanf ("%d", &t); while (t--) { printf ("Case %d: ", flag++); long long a, b; scanf ("%lld%lld", &a, &b); Solve (a, b); } return 0; } |
- « 上一篇:lightoj1183
- lightoj1198:下一篇 »