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?>?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,n开始是a!/b!,每次用一个x去整除n得到新的n,最后当n变成1的时候经过了几轮得分就是这个轮数,要求最大的分数是多少

思路:
很明显,就是一个求整数质因子个数的题目,阶乘我们不需要算,我们知道在a>b的时候,b!都约掉了,那么我们只需压迫计算出每个数的质因数有几个,然后计算出1~n的质因子之和,那么就可以迅速得到答案了

#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-07-30 13:25:48

Codeforces546D:Soldier and Number Game(求质因子个数)的相关文章

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

CodeForces-546D Soldier and Number Game 筛法+动态规划

题目链接:https://cn.vjudge.net/problem/CodeForces-546D 题意 抱歉,我给忘了,现在看题目又看不懂: P 思路 筛法+dp 话说这个函数应该是积性函数,然后就想到了动态规划优化筛法. 提交过程 AC 代码 #include <cstdio> #include <cstring> const int maxp=1e6, maxn=5e6+20; int primes[maxn], psize; long long ans[maxn], pr

Easy Number Challenge(暴力,求因子个数)

Easy Number Challenge Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 236B Appoint description:  System Crawler  (2016-04-26) Description Let's denote d(n) as the number of divisors of a

数学+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 ******************************

Divisors_组合数因子个数

Description Your task in this problem is to determine the number of divisors of Cnk. Just for fun -- or do you need any special reason for such a useful computation? Input The input consists of several instances. Each instance consists of a single li

Java小例子——穷举质数,求平方和,求质因子。

求平方和 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public static void main(String[] args) throws IOException     {         int n;         String s;         BufferedReader buf;         buf=new BufferedReader(new In

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>

Almost All Divisors(求因子个数及思维)

---恢复内容开始--- We guessed some integer number xx. You are given a list of almost all its divisors. Almost all means that there are all divisors except 11and xx in the list. Your task is to find the minimum possible integer xx that can be the guessed nu

因子个数与因子和

题目:LightOJ:1341 - Aladdin and the Flying Carpet(因子个数) It's said that Aladdin had to solve seven mysteries before getting the Magical Lamp which summons a powerful Genie. Here we are concerned about the first mystery. Aladdin was about to enter to a m