codeforces Soldier and Number Game(dp+素数筛选)
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+素数筛选)的更多相关文章
- Codeforces J. Soldier and Number Game(素数筛)
题目描述: Soldier and Number Game time limit per test 3 seconds memory limit per test 256 megabytes inpu ...
- CodeForces 546 D. Soldier and Number Game(素数有关)
Description Two soldiers are playing a game. At the beginning first of them chooses a positive integ ...
- FZU2179/Codeforces 55D beautiful number 数位DP
题目大意: 求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
- 数学+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;再来一个前缀和 */ /***************** ...
- 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 ...
- 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 ...
- Codeforces D546:Soldier and Number Game
题目链接 输入t对数 a, b 求(b,a]内的每个数拆成素因子的个数和 这里每个数都可以写成素数的乘积,可以写成几个素数的和就有几个素因子,这里求的是(b,a]内的素因子和 思路: 素数的素因子个数 ...
- 51nod 1536不一样的猜数游戏 思路:O(n)素数筛选法。同Codeforces 576A Vasya and Petya's Game。
废话不多说,先上题目. 51nod Codeforces 两个其实是一个意思,看51nod题目就讲的很清楚了,题意不再赘述. 直接讲我的分析过程:刚开始拿到手有点蒙蔽,看起来很难,然后......然后 ...
随机推荐
- (混合背包 多重背包+完全背包)The Fewest Coins (poj 3260)
http://poj.org/problem?id=3260 Description Farmer John has gone to town to buy some farm supplies. ...
- 04、AngularJS的ng-bind、多个控制器和apply
这篇,讲一下angularjs的ng-bind指令,多个控制器,以及手动触发angularjs的脏检查,我直接把代码贴,顺着代码讲. <!DOCTYPE html> <html> ...
- 看到shape文件可以加载到GOOGLE EARTH上的方法,有空可以试试
引用 Shape文件转为KMZ并在Google Earth中显示 (1)在ArcGIS中加载一个Shape文件,笔者加载的是某个地区的道路(双线道路)图层 (2)在ArcToolbox中,依次展开Co ...
- 树莓派3B安装pybluz
按如下流程: apt-get install python-dev apt-get install python-pip apt-get install libbluetooth-dev pip in ...
- SHELL实现同时操作多个服务器:服务器批量管理
引言: 1.如果你想知道你所管理的几万台服务器的/home分区使用率是多少. 2.如果你想为你所管理的几万台服务器添加同一个计划任务你该怎么办? 3.如果你想让你所管理的几万台 ...
- 开源的EtherCAT Master简介
EtherCAT的主站开发是基于EtherCAT机器人控制系统的开发中非常重要的环节.目前常见开源的主站代码为的RT-LAB开发的SOEM (Simple OpenSource EtherCAT Ma ...
- java 动态代理示例,带主要注释
Java proxy是基于反射,仅仅支持基于接口的动态代理. java 动态代理是一切架构的基础,必须了解. 废话少说,先上代码获得感性认识. 示例代码有主要注释. 接口: public interf ...
- Nodejs学习路线图
前言 用Nodejs已经1年有余,陆陆续续写了48篇关于Nodejs的博客文章,用过的包有上百个.和所有人一样,我也从Web开发开始,然后到包管 理,再到应用系统的开发,最后开源自己的Nodejs项目 ...
- Windows的bat脚本中for循环
转载至 http://123304258.blog.163.com/blog/static/12354702012621103256608/ [删除目录下某种格式的文件 ] for /r f:\ ...
- Android下集成Paypal支付
近期项目需要研究paypal支付,官网上的指导写的过于复杂,可能是老外的思维和中国人不一样吧.难得是发现下面这篇文章: http://www.androidhive.info/2015/02/andr ...