D. Soldier and Number Game
                          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. Sample test(s)
input
2
3 1
6 3
output
2
5

  

/*
dp求解 n!的质因子个数。
1.首先利用素数筛选法求出素数的集合
2.dp[i] = dp[i/prime[j]]+1, i%prime[j]==0; prime[j]是第一个能整除i的数
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 5000005
using namespace std; int prime[N];
bool isprime[N];
int dp[N];
void init(){
int top = ;
for(int i=; i<N; ++i){
if(!isprime[i])
prime[top++] = i;
for(int j=; j<top && i*prime[j]<N; ++j){//素数筛选
isprime[i*prime[j]] = true;
if(i%prime[j] == )
break;
}
}
for(int i=; i<N; ++i){
if(!isprime[i])
dp[i] = ;
else{
for(int j=; j<top; ++j)
if(i%prime[j]==){
dp[i] = dp[i/prime[j]] + ;
break;
}
}
}
for(int i=; i<N; ++i)//前n个数的质因数个数的和
dp[i]+=dp[i-];
} int main(){
int t;
scanf("%d", &t);
init();
while(t--){
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", dp[a] - dp[b]);
}
return ;
}

codeforces Soldier and Number Game(dp+素数筛选)的更多相关文章

  1. Codeforces J. Soldier and Number Game(素数筛)

    题目描述: Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes inpu ...

  2. CodeForces 546 D. Soldier and Number Game(素数有关)

    Description Two soldiers are playing a game. At the beginning first of them chooses a positive integ ...

  3. FZU2179/Codeforces 55D beautiful number 数位DP

    题目大意: 求  1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...

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

    题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...

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

    题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...

  6. Codeforces Round #304 (Div. 2) D. Soldier and Number Game 素数打表+质因数分解

    D. Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  7. Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数学 质因数个数

    D. Soldier and Number Game Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...

  8. Codeforces D546:Soldier and Number Game

    题目链接 输入t对数 a, b 求(b,a]内的每个数拆成素因子的个数和 这里每个数都可以写成素数的乘积,可以写成几个素数的和就有几个素因子,这里求的是(b,a]内的素因子和 思路: 素数的素因子个数 ...

  9. 51nod 1536不一样的猜数游戏 思路:O(n)素数筛选法。同Codeforces 576A Vasya and Petya's Game。

    废话不多说,先上题目. 51nod Codeforces 两个其实是一个意思,看51nod题目就讲的很清楚了,题意不再赘述. 直接讲我的分析过程:刚开始拿到手有点蒙蔽,看起来很难,然后......然后 ...

随机推荐

  1. 关于Certificate、Provisioning Profile、App ID的介绍及其之间的关系

    1.概念介绍 如果你拥有一个开发者账户的话,在iOS Dev Center打开Certificates, Indentifiers & Profiles,你就可以看到如下的列表: Profil ...

  2. linux琐碎命令学习

    kill -l会把linux的信号都列出来.1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP6) SIGABRT 7) SIGBUS 8) SIG ...

  3. Boost 安装

    1.下载boost Windows版,如:boost_1_55_0: 2.运行boostrap.bat,会生成bjam.exe: 3.运行bjam.exe(时间会比较长),会生成一个stage目录里面 ...

  4. Runtime 交换方法

    创建UIImage分类UIImage+Image.h #import<UIKit/UIKit.h> @interfaceUIImage (Image) + (__kindof UIImag ...

  5. 【腾讯Bugly干货分享】微信Tinker的一切都在这里,包括源码(一)

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/57ecdf2d98250b4631ae034b 最近半年以来,Android热补 ...

  6. 分享一个U3D在Runtime显示碰撞盒的插件

    有些时候,我们需要在Game视图显示碰撞盒,比如格斗游戏我要开发碰撞配置的工具,我经常需要看到碰撞盒,今天找了一下,没有发现合适的插件,我还花5美金买了一个插件,结果也只是在scene视图显示,这里我 ...

  7. linux expect详解(ssh自动登录)

    shell脚本实现ssh自动登录远程服务器示例: #!/usr/bin/expect spawn ssh root@192.168.22.194 expect "*password:&quo ...

  8. 锋利的JQuery —— 选择器

    图片猛戳链接

  9. Eclipse 工程迁移到 Android Studio

    目标:迁移成功,并成功正常运行! 附加:同步视频在文章后面! 两种方式: 1. 用Gradle导出,在Android Studio中用Gradle导入 (不推荐) 2. 用Android Studio ...

  10. H5常用代码:适配方案4

    前面有分享了4种适配方案,但始终是通过手动缩放或者视口缩放来实现,用来做一些专题页,或者功能相对简单的项目来说也是完全能应付的,但整体来说感觉还是一种缩放,说不上是真正的适配,言外之意就是即将分享真正 ...