Codeforces546D:Soldier and Number Game
2016-08-14 20:55
351 查看
Description
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.
Sample Input
Input
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.
Sample Input
Input
2 3 1 6 3
Output
2 5 虽然做出来了,但用了一个多小时时间思考,过后想想其实思路很简单的,还是自己太渣,唉 这道题其实是还是用到了素数筛选的思想,注意打表就行了,不然会超时,详细见代码:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #define N 5000100 #define INF 999999999 using namespace std; int a ,f ; void flag() { int i,j; for(i=1; i<=N; i++) a[i]=1; int m=sqrt(N); for(i=2; i<=m; i++)//这里注意循环到根号N就行了; for(j=i*i; i<=N; j+=i)//按照倍数进行筛选,类似素数筛选的方法 { if(j>N) break; a[j]=a[i]+a[j/i];//这里是关键 } int sum=0; for(i=1;i<=N;i++)//注意这里将和打表,不要一个个的进行循环计算,会超时的; { sum+=a[i]; f[i]=sum; } } int main() { int m,n,t,ans; flag(); scanf("%d",&t); while(t--) { ans=0; scanf("%d%d",&n,&m);//(n!/m!等于n*(n-1)*(n-2)*.....*m+1) ans=f -f[m];//ans等于f -f[m+1-1]; printf("%d\n",ans); } return 0; }
相关文章推荐
- Codeforces546D:Soldier and Number Game(求质因子个数)
- CodeForces546D:Soldier and Number Game(筛区间素数因子个数和)
- [Codeforces546D]Soldier and Number Game[dp][实现][素数筛][分解整数][数学]
- Soldier and Number Game--筛素数
- codeforces Soldier and Number Game(dp+素数筛选)
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
- Codeforces Round #304 (Div. 2)-D. Soldier and Number Game,素因子打表!
- D. Soldier and Number Game
- CodeForces 828D Soldier and Number Game(数学)
- Soldier and Number Game
- codeforces-546D-Soldier and Number Game【思维】(求质因子个数)
- 【codeforces 546D】Soldier and Number Game
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数学 质因数个数
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game
- 【CodeForces】546D - Soldier and Number Game(线性筛法)
- Soldier and Number Game---cf546D(打表求n的素因子个数)
- B - Soldier and Number Game
- Codeforces D546:Soldier and Number Game
- Codeforces Round #304 (Div. 2) D - Soldier and Number Game
- D-Soldier and Number Game(CF546D) Codeforces Round #304 (Div. 2)