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题目就讲的很清楚了,题意不再赘述. 直接讲我的分析过程:刚开始拿到手有点蒙蔽,看起来很难,然后......然后 ...
随机推荐
- 如何给澳洲路局写信refound罚金,遇到交通罚款怎么办
在澳洲,100%的司机收到过罚单,包括停车,超速,闯红灯等等,其罚金一般都在200-500之间,当然其单位是AUD.所以,对大多数留学生来说,收到罚金意味着一个礼拜要吃吐了. 本人就收到过一次超速罚单 ...
- 套题 codeforces 360
A题:Opponents 直接模拟 #include <bits/stdc++.h> using namespace std; ]; int main() { int n,k; while ...
- 重拾java之路之webservice
Web service平台是一套标准,它定义了应用程序如何在Web上实现互操作性.你可以用任何你喜欢的语言,在任何你喜欢的平台上写Web service ,只要我们可以通过Web servi ...
- [转]全面理解Unity加载和内存管理
[转]全面理解Unity加载和内存管理 最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Resources.Load,一是通过AssetBundle,其实两者本质 ...
- JAVA学习博客---2015-6
JAVA核心技术卷一第一遍看得差不多了,应该是五月初开始看的,用了两个月的中午时间看完的,一共七百多页,接下来还是需要再看一遍,不懂的还是有很多. JAVA和C++一样是面向对象OOP的语言,不同于命 ...
- 图片下载缓存防止OOM
一 ImageManager ImageMemoryCache(内存缓存).ImageFileCache(文件缓存) 关于Java中对象的软引用(SoftReference),如果一个对象具有 ...
- PyCharm 3.0 发布,提供免费开源版本
PyCharm 发布最新的 3.0 版本,该版本新特性详见: http://www.jetbrains.com/pycharm/whatsnew/index.html 该版本最主要的是提供了免费开源的 ...
- android oauth 微博客户端 架构一
最近研究oauth协议,为了进一步 的巩固自己的学习成果,顾完成了android的新浪客户端.他的架构如下: UI层微博中的各个窗体 就是所谓的各个activitylogic层程序的核心控制调度模块 ...
- Asp.Net MVC中使用ACE模板之Jqgrid
第一次看到ACE模板,有种感动,有种相见恨晚的感觉,于是迅速来研究.它本身是基于bootstrap和jqueryui,但更nice,整合之后为后台开发节省了大量时间. 发现虽然不是完美,整体效果还是不 ...
- java输出任意两个日期之间有多少天
package JingDian; import java.text.ParseException; import java.text.SimpleDateFormat; import java.ut ...