链接:戳这里
D. Soldier and Number Game
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Two 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?
Input
First 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.
Output
For each game output a maximum score that the second soldier can get.
Examples
input
2
3 1
6 3
output
2
5
题意:
统计区间内每个数所包含的素因子个数的总和
思路:
线性筛素数的时候,记录每个数的第一个素数因子
然后dp转移的时候dp[i]=dp[i/firstprime]+1 (很关键)
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 5000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int T;
int vis[MAX],prime[1000100],a[MAX];
ll sum[MAX],dp[MAX];
int main(){
int cnt=0;
for(int i=2;i<=MAX;i++){
if(!vis[i]){
prime[cnt++]=i;
for(int j=i;j<MAX;j+=i){
if(!a[j]) a[j]=i;
}
}
for(int j=0;j<cnt;j++){
if(i*prime[j]>MAX) break;
vis[i*prime[j]]=1;
if(i%prime[j]==0) break;
}
}
dp[2]=1;
for(int i=3;i<MAX;i++){
dp[i]=dp[i/a[i]]+1;
}
sum[2]=1;
for(int i=3;i<MAX;i++) {
sum[i]=sum[i-1]+dp[i];
}
scanf("%d",&T);
while(T--){
int a,b;
scanf("%d%d",&a,&b);
printf("%I64d\n",sum[a]-sum[b]);
}
return 0;
}