【codeforces 546D】Soldier and Number Game

来源:互联网 发布:彩色烟雾棒淘宝 编辑:程序博客网 时间:2024/06/05 22:19

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
【题目链接】:http://codeforces.com/contest/546/problem/D

【题解】

每个数字如果想让他除的次数更多,必然是每次都选择最小的质因子去除;
想想如果不是 除质因子,那个数x总是可以再分解成几个质因子的形式.
这样想之后;
a!/b!
=(b+1)(b+2)···*a
把每个数字都分解一下质因数就可以了
这个可以在做筛法求素数的时候搞定;
然后求一下前缀和就可以了;
输出的时候直接O(1)输出

【完整代码】

#include <bits/stdc++.h>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long long#define rep1(i,a,b) for (int i = a;i <= b;i++)#define rep2(i,a,b) for (int i = a;i >= b;i--)#define mp make_pair#define pb push_back#define fi first#define se second#define rei(x) scanf("%d",&x)#define rel(x) scanf("%I64d",&x)typedef pair<int,int> pii;typedef pair<LL,LL> pll;const int MAXN = 5000000;const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};const double pi = acos(-1.0);LL v[MAXN+10],sum[MAXN+10];int main(){    //freopen("F:\\rush.txt","r",stdin);    for (int i = 2;i <= MAXN;i++)        if (v[i]==0)        {            for (int j = i;j <=MAXN ;j+=i)            {                int J = j;                while (J%i==0)                {                    v[j]++;                    J/=i;                }            }        }    rep1(i,1,MAXN)        sum[i] = sum[i-1]+v[i];    int T;    rei(T);    while (T--)    {        int a,b;        rei(a);rei(b);        printf("%I64d\n",sum[a]-sum[b]);    }    return 0;}
0 0
原创粉丝点击