D. Soldier and Number Game
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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.

Examples
Input
2
3 1
6 3
Output
2
5 题意: t组数据 给你a b 两个数(a>=b)
两个人进行一个游戏 初始时第一个人挑选一个数n给第二个人 第二个人选择一个n的因子k使得n/k尽可能的大
使得n=n/k 并得1分 继续选择当前n的因子 继续游戏 当n==1时游戏结束 问第二个人的最大得分为多少?
这里n=a!/b!
题解: “数学上来先打表” 这句话堪称精辟
首先a b的范围很大 暴力处理是不可能的
分解开来n=a!/b!=(b+1)*(b+2).....*a; 为了使得n被多次分解 可以很容易的考虑到求每一项的质因子分解
(只有是质因子分解,才能够使得分解后的因子个数最多 不做解释)并且统计所有的分解后的因子的个数之和就是答案
对于询问1e6不可能每次都遍历去求
考虑打表求1~5000000的每个数的质因子分解的个数 并且暴力去求每个数的质因子必然超时 考虑记忆化
对于已经找过质因子个数的数进行存储 方便之后的数直接使用 免于重复计算
并且只需要找到当前数的一个因子就可以 其他的已经被计算过 这就是记忆化的优化所在点
并且记录前缀和
 对于询问直接输出 sum[a]-sum[b]
 //code by  drizzle
#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<vector>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int t;
int a,b;
int sum[];
int have[];
void fun()
{
sum[]=;
sum[]=;
have[]=;
for(int i=;i<=;i++)
{
if(i%==)//只需要找到当前数的一个因子就可以 其他的已经被计算过
{
have[i]=+have[i/];
sum[i]=sum[i-]+have[i];
continue;
}
int flag=;
for(int j=;j*j<=i;j+=)
{
if(i%j==)
{
have[i]=+have[i/j];
sum[i]=sum[i-]+have[i];
flag=;
break;
}
}
if(flag==)
{
have[i]=;
sum[i]=sum[i-]+have[i];
}
}
}
int main()
{
fun();
scanf("%d",&t);
for(int i=;i<=t;i++)
{
scanf("%d %d",&a,&b);
printf("%d\n",sum[a]-sum[b]);
}
return ;
}

Codeforces Round #304 (Div. 2) D 思维/数学/质因子/打表/前缀和/记忆化的更多相关文章

  1. Codeforces Round #601 (Div. 2)E(寻找质因子,DP)

    先分解质因数,对于当前a[i],假设当前的质因数为x,这个位置要满足能被k整除,有两个可能,要么是它向后一个转移x%k个,要么是后一个向它转移k-x%k个. 对于每一个a[i]满足后,因为只会对下一个 ...

  2. 数学+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;再来一个前缀和 */ /***************** ...

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

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

  4. queue+模拟 Codeforces Round #304 (Div. 2) C. Soldier and Cards

    题目传送门 /* 题意:两堆牌,每次拿出上面的牌做比较,大的一方收走两张牌,直到一方没有牌 queue容器:模拟上述过程,当次数达到最大值时判断为-1 */ #include <cstdio&g ...

  5. 贪心 Codeforces Round #304 (Div. 2) B. Soldier and Badges

    题目传送门 /* 题意:问最少增加多少值使变成递增序列 贪心:排序后,每一个值改为前一个值+1,有可能a[i-1] = a[i] + 1,所以要 >= */ #include <cstdi ...

  6. 水题 Codeforces Round #304 (Div. 2) A. Soldier and Bananas

    题目传送门 /* 水题:ans = (1+2+3+...+n) * k - n,开long long */ #include <cstdio> #include <algorithm ...

  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 Round #304 (Div. 2) B. Soldier and Badges【思维/给你一个序列,每次操作你可以对一个元素加1,问最少经过多少次操作,才能使所有元素互不相同】

    B. Soldier and Badges time limit per test 3 seconds memory limit per test 256 megabytes input standa ...

  9. Codeforces Round #304 (Div. 2) -----CF546

    A. Soldier and Bananas   A soldier wants to buy w bananas in the shop. He has to pay k dollars for t ...

随机推荐

  1. 最近面试前端岗位,汇总了一下前端面试题(JS+CSS)

    JavaScript 运行机制 1. 单线程(用途决定,需要与用户互动以及操作DOM) 2. 分同步任务(主线程)与异步任务(任务队列),只有任务队列通知主线程某个任务可以执行了,该 任务才会进入主线 ...

  2. UNC路径格式

    \\192.168.3.66\c$  访问本地网内计算机

  3. Javascript显示提示信息加样式

    #region JS提示============================================ /// <summary> /// 添加编辑删除提示 /// </s ...

  4. 随机数生成器java实现

    /** 设计一个随机数生成器,可以产生给定平均概率的随机证书序列. 即输入一个概率比如:0.9 然后输入要求的概率样本个数比如:1000 输出一个接近所输入的0.9的概率数(要求样本数越大越接近输入的 ...

  5. JDBC的连接mySql的基本知识

    这只是我自己的随笔博客~,用于偶尔回忆知识,可能存在一些错误,如有错误,欢迎指正~ 首先对于JDBC连接MySQL,要了解基本的框架结构 画的比较烂,大约就是这样的结构 然后看一下具体实现的 代码:: ...

  6. python 基础 for else

    for one in many_list: if "k" in one: print "在里面" break else: print "没有在里面&q ...

  7. git 命令汇总

    本地库处理 git init 初始化仓库 git clone [地址] 下载项目 git status 查看当前暂存等状态 git add 添加暂存 cat .git/config 查看git配置 l ...

  8. 记python版本管理--pyenv

    随记: 众所周知,python2.x版本与3.x版本有比较大的区别,如果你是2.x版本的使用者,突然接了3.x版本的项目,或者反过来,遇到这种情况该怎么办呢?重新安装自己电脑上的python,来匹配对 ...

  9. python中生成器对象和return 还有循环的区别

    python中生成器对象和return 还有循环的区别 在python中存在这么一个关键字yield,这个关键字在项目中经常被用到,比如我写一个函数不想它只返回一次就结束那我们就不能用return,因 ...

  10. [转载]C#中使用ADO.NET连接SQL Server数据库,自动增长字段用作主键,处理事务时的基本方法

    问题描述: 假设在数据库中存在以下两张数据表: User表,存放用户的基本信息,基本结构如下所示:   类型 说明 ID_User int 自动增长字段,用作该表的主键 UserName varcha ...