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. SSM+DUBBO/ZK异常

    1.Could not find resource(mybatis本身不支持通配符,是spring提供的ant匹配法) 2.ClassNotFoundException: org.I0Itec.zkc ...

  2. 【随记】Hello World小记

    今天装Python,如下: 突然想到,到现在,我已经数不清写过多少遍Hello World了. 最早是初一学VB的时候,用Label1在Form1上画一个,然后修改Caption属性为“Hello W ...

  3. js 监听输入框输入事件兼容ie7

    $(element).bind("input propertychange",function(){});

  4. Python-变量

    1.Python的变量是什么 变量是用来存储计算机程序中的信息,唯一的目的是将数据存储在内存中. 2.Python变量的组成 变量由字母.数字.下划线组成: 变量的第一位不能是数字,可以是字母或下划线 ...

  5. 简单的OkHttp使用介绍

    Android系统提供了两种HTTP通信类,HttpURLConnection和HttpClient.关于HttpURLConnection和HttpClient的选择>>官方博客尽管Go ...

  6. js学习笔记之一

    一.Javascript 中的对象 1. 建立自定义对象 方法1:对象={属性1:属性值1,属性2:属性值2……属性n:属性值n} 方法2:先定义构造函数,再new创建对象实例. 如: functio ...

  7. html5实战2

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <meta http-e ...

  8. Bootstrap CDN推荐

    Bootstrap CDN推荐 本站实例采用的是百度的静态资源库(http://cdn.code.baidu.com/)上的Bootstrap资源. 百度的静态资源库的 CDN 服务,访问速度更快.加 ...

  9. Attic 0.8.1 发布,备份程序

    Attic 0.8.1 修复了昨天刚发布的 Attic 0.8 的一个段错误. Attic 是一个可以保证文件不重复的备份程序.Attic 的主要目标是提供一个高效和安全的方法来备份数据.重复数据删除 ...

  10. Xamarin.Android中使用ResideMenu实现侧滑菜单

    上次使用Xamarin.Android实现了一个比较常用的功能PullToRefresh,详情见:Xamarin. Android实现下拉刷新功能 这次将实现另外一个手机App中比较常用的功能:侧滑菜 ...