CF 546D Soldier and Number Game

Describe

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

23 16 3

Output

25

Div2 D, 分解质因数个数,求前缀和CODE:
#include <iostream>
#include <cstdio>
#include <cstring>
#define REP(i, s, n) for(int i = s; i <= n; i ++)
#define REP_(i, s, n) for(int i = n; i >= s; i --)
#define MAX_N 5000000 + 10

using namespace std;

int T, pri[MAX_N], sum[MAX_N];
bool check[MAX_N];

void Prime(){
    memset(check, 0, sizeof(check));
    memset(sum, 0, sizeof(sum));
    int tot = 0; check[1] = 1;
    REP(i, 2, MAX_N){
        if(!check[i]) pri[++ tot] = i, sum[i] = 1;
        REP(j, 1, tot){
            if(i * pri[j] > MAX_N) break;
            check[i * pri[j]] = 1; sum[i * pri[j]] = sum[i] + 1;
            if(i % pri[j] == 0) break;
        }
    }

    REP(i, 2, MAX_N) sum[i] = sum[i - 1] + sum[i];
}

int main(){
    scanf("%d", &T);
    Prime();
    while(T --){
        int a, b; scanf("%d%d", &a, &b);
        printf("%d\n", sum[a] - sum[b]);
    }
    return 0;
}        
 
时间: 2024-11-07 01:39:08

CF 546D Soldier and Number Game的相关文章

Codeforces 546D Soldier and Number Game(数论)

类似筛素数的方法--求出前缀和.然后直接O(1)回答即可. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i,a,b) for(int i(a); i <= (b); ++i) 6 7 const int N = 10000000 + 10; 8 9 int num[N]; 10 bool p[N]; 11 int T, n, m; 12 13 int main(){ 14 15 memset(p,

Codefo 546D. Soldier and Number Game

题目链接:http://codeforces.com/problemset/problem/546/D 题意:输入a,b,n=a!/b!,求n 最多除以多少次变为1. 分析:相当于求n有多少个质因子.即求从b+1到a之间的数字质因子和为多少. #include <iostream> #include <cstring> #include <cstdio> using namespace std; int v[5000005];//v[i]记录i是否为质数 int pri

codeforces 546D Soldier and Number Game

题目链接 这个题, 告诉你a, b的值, 那么只需要求出b到a之间的数, 每个数有多少个因子就可以. 具体看代码, 代码里面有解释 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 const int maxn = 5000005; 5 int p[maxn], c[maxn]; 6 int main() 7 { 8 memset(p, 0, sizeof(p)); 9 memset(

CodeForces 546D Soldier and Number Game 打表(求质因子个数)

题目:戳我 题意:题意看懂了上面的一大串英文之后其实很简单,就是给你一个正整数n,问你n有多少个质因子,不过这里n是通过a!/b!给定的,也就是说n=(a!/b!); 分析:由于这里1 ≤ b ≤ a ≤ 5 000 000,数据很大,所以简单暴力一定超时,具体看代码. 1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int M = 5e6+5; 5 6 int t; //测试组数 7

数学+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)——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

cf 546C Soldier and Cards

题目链接:C. 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 numb

Codeforces546D: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