点击打开题目
D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard outputTwo soldiers are playing a game. At the beginning first of them chooses a positive integer n and gives it to the second soldier. Then the second one tries to make maximum possible number of rounds. Each round consists of choosing a positive integer x > 1, such that n is divisible by x and replacing n with n / x. When n becomes equal to 1 and there is no more possible valid moves the game is over and the score of the second soldier is equal to the number of rounds he performed.
To make the game more interesting, first soldier chooses n of form a! / b! for some positive integer a and b (a ≥ b). Here by k! we denote the factorial of k that is defined as a product of all positive integers not large than k.
What is the maximum possible score of the second soldier?
InputFirst line of input consists of single integer t (1 ≤ t ≤ 1 000 000) denoting number of games soldiers play.
Then follow t lines, each contains pair of integers a and b (1 ≤ b ≤ a ≤ 5 000 000) defining the value of n for a game.
OutputFor each game output a maximum score that the second soldier can get.
Examples input 2 3 1 6 3 output 2 5
意思就是求a!/ b!有多少质因子。但是当时看题意真的是不懂。
看懂题后用一个线性筛把每个数的质因数的个数求出来即可。
代码如下:
#include <stdio.h> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define CLR(a,b) memset(a,b,sizeof(a)) #define INF 0x3f3f3f3f #define LL long long int num[5000011]; void getSum() { for (int i = 2 ; i <= 5000000 ; i++) { if (!num[i]) { for (int j = i ; j <= 5000000 ; j += i) { int t = j; while (t % i == 0) { num[j]++; t /= i; } } } } } int main() { int n; getSum(); for (int i = 2 ; i <= 5000000 ; i++) num[i] += num[i-1]; scanf ("%d",&n); int l, r; while (n--) { scanf ("%d%d",&r,&l); printf ("%d\n",num[r]-num[l]); } return 0; }