D-Soldier and Number Game(CF546D) Codeforces Round #304 (Div. 2)

D. Soldier and Number Game

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 test(s)

input

2
3 1
6 3

output

2
5

题意:n=a!/b!问你n的素数因子的个数。

思路:素数打表;

转载请注明出处:寻找&星空の孩子

题目链接:http://codeforces.com/contest/546/problem/D

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std;

#define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 5000005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7;
int p[N];
bool v[N];
int a[N];
int prime[N/10];
LL sum[N];
void init()
{
    for(int i=2; i<N; ++i)
        a[i] = i;
    int num=-1;
    for(int i=2; i<N; ++i)
    {
        if(!v[i]) prime[++num] = i;
        for(int j=0; j<=num && i*prime[j] < N; ++j)
        {
            int t = i*prime[j];
            v[t] =1;
            if(a[t] > prime[j]) a[t] = prime[j];
            if(i%prime[j] == 0) break;
        }
    }
    p[2] = 1;
    for(int i=3; i <N; ++i)
        p[i] = p[i/a[i]] + 1;
}
int main()
{
    int i,j,k;
    init();
    sum[1] = 0;
    for(i = 2; i<=5000000; i++)
    {
        sum[i] = sum[i-1]+p[i];
    }
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&i,&j);
        if(i == j)
            printf("0\n");
        else
            printf("%I64d\n",sum[i]-sum[j]);
    }

    return 0;
}

时间: 2024-10-05 14:24:29

D-Soldier and Number Game(CF546D) Codeforces Round #304 (Div. 2)的相关文章

数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:这题就是求b+1到a的因子个数和. 3 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1 14:08:34 8 File Name :B.cpp 9 ******************************

DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:b+1,b+2,...,a 所有数的素数个数和 3 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 4 最后改为i之前所有素数个数和,那么ans = dp[a] - dp[b]: 5 详细解释:http://blog.csdn.net/catglory/article/details/45932593 6 */ 7 #include <cstdio> 8 #include <algorithm>

水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas

题目传送门 1 /* 2 水题:ans = (1+2+3+...+n) * k - n,开long long 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <cmath> 8 using namespace std; 9 10 typedef long long ll; 11 12 int main(void) //Codeforces Roun

贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

题目传送门 1 /* 2 题意:问最少增加多少值使变成递增序列 3 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= 4 */ 5 #include <cstdio> 6 #include <cstring> 7 #include <algorithm> 8 using namespace std; 9 10 typedef long long ll; 11 12 const int MAXN = 3e3 + 10;

queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

题目传送门 1 /* 2 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 3 queue容器:模拟上述过程,当次数达到最大值时判断为-1 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <algorithm> 8 #include <cstring> 9 #include <string> 10 #include <stack> 11 #in

Codeforces Round #304 (Div. 2)——D素数筛+dp——Soldier and Number Game

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

Codeforces Round #304 (Div. 2) -----CF546

A. Soldier and Bananas A soldier wants to buy w bananas in the shop. He has to pay k dollars for the first banana, 2k dollars for the second one and so on (in other words, he has to pay i·k dollars for the i-th banana). He has n dollars. How many dol

Codeforces Round #304 (Div. 2)——Cdfs——Soldier and Cards

Two bored soldiers are playing card war. Their card deck consists of exactly n cards, numbered from 1 to n, all values are different. They divide cards between them in some manner, it's possible that they have different number of cards. Then they pla

Codeforces Round #304 (Div. 2) E. Soldier and Traveling 最大流 Dinic EK 算法

E. Soldier and Traveling time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output In the country there are n cities and m bidirectional roads between them. Each city has an army. Army of the i-th ci