Soldier and Number Game

Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 Input

Input
2
3 1
6 3
Output
2
5
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <string>
#include <queue>
#include <map>
#include <stack>
using namespace std; typedef long long LL;
const LL INF = 1 << 31;
const int MAXN = 5000005;
int pri[5000], vis[MAXN], res[MAXN], c = 1;
void getpri()//刷选2500以内的素数, 同时vis[]判断 500w以内的数是否是素数
{
memset(vis, 0, sizeof vis);
vis[0] = vis[1] = 1;
for(int i = 2; i < 2500; ++i) if(!vis[i]) {
pri[c++] = i;
for(int j = i * i; j < MAXN; j += i)
vis[j] = 1;
}
}
int main()
{
int ans, g, flag;
getpri();
res[1] = res[0] = 0;
for(int i = 2; i < MAXN; ++i)
{
g = i;
ans = 0;
if(!vis[i]) ans = 1; //素数的结果不用除, 只有自己本身一个因子
else {
flag = 0;
for(int j = 1; g != 1 && j < c; ++j) { //要想把 500w以内的合数都分解为质数,
                            //只需要一直尝试除于 sqrt(500w)内的质数就好了
//因为对于 一个合数n, 它总有一个 质因子小于或等于 sqrt(n)
while(g % pri[j] == 0) { g /= pri[j]; ans++;
if(!vis[g]) { ans++;flag = 1; break; }//此步必须要有, 在试除的过程中, 若已为质数, 及时推出
//另一方面, 如n = 13 * 11111, 由于只刷选到 2500内的质数, 11111不能检测到
}
if(flag) break;
}
}
res[i] = res[i - 1] + ans;
}
int t, a, b;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &a, &b);
printf("%d\n", res[a] - res[b]);
}
} 另一种做法是:充分利用已经得到的结果, 如 res[100] = res[2] + res[100 / 2] 其中由于 2 和 50 都小于 100, 故已经算出来了

  

Codeforce 546D的更多相关文章

  1. Codeforce - Street Lamps

    Bahosain is walking in a street of N blocks. Each block is either empty or has one lamp. If there is ...

  2. Codeforce Round #216 Div2

    e,还是写一下这次的codeforce吧...庆祝这个月的开始,看自己有能,b到什么样! cf的第二题,脑抽的交了错两次后过了pretest然后system的挂了..脑子里还有自己要挂的感觉,果然回头 ...

  3. Codeforce 水题报告(2)

    又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数( ...

  4. codeforce 375_2_b_c

    codeforce 375_2 标签: 水题 好久没有打代码,竟然一场比赛两次卡在边界条件上....跪 b.题意很简单...纯模拟就可以了,开始忘记了当字符串结束的时候也要更新两个值,所以就错了 #i ...

  5. codeforce 367dev2_c dp

    codeforce 367dev2_c dp 标签: dp 题意: 你可以通过反转任意字符串,使得所给的所有字符串排列顺序为字典序,每次反转都有一定的代价,问你最小的代价 题解:水水的dp...仔细想 ...

  6. 三维dp&codeforce 369_2_C

    三维dp&codeforce 369_2_C 标签: dp codeforce 369_2_C 题意: 一排树,初始的时候有的有颜色,有的没有颜色,现在给没有颜色的树染色,给出n课树,用m种燃 ...

  7. 强连通分量&hdu_1269&Codeforce 369D

    强连通分量 标签: 图论 算法介绍 还记得割点割边算法吗.回顾一下,tarjan算法,dfs过程中记录当前点的时间戳,并通过它的子节点的low值更新它的low,low值是这个点不通过它的父亲节点最远可 ...

  8. 【树状数组】区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D

    [树状数组]区间出现偶数次数的异或和(区间不同数的异或和)@ codeforce 703 D PROBLEM 题目描述 初始给定n个卡片拍成一排,其中第i个卡片上的数为x[i]. 有q个询问,每次询问 ...

  9. 解题报告:codeforce 7C Line

    codeforce 7C C. Line time limit per test1 second memory limit per test256 megabytes A line on the pl ...

随机推荐

  1. 【python】入门学习(十)

    #入门学习系列的内容均是在学习<Python编程入门(第3版)>时的学习笔记 统计一个文本文档的信息,并输出出现频率最高的10个单词 #text.py #保留的字符 keep = {'a' ...

  2. IOS - 响应者链条

    简单来说就是:一级一级的找到响应的视图,如果没有就传给UIWindow实例和UIApplication实例,要是他们也处理不了,就丢弃这次事件... 对于IOS设备用户来说,他们操作设备的方式主要有三 ...

  3. IOS- 快速排序,冒泡排序,直接插入排序和折半插入排序,希尔排序,堆排序,直接选择排序

    /*******************************快速排序 start**********************************///随即取 当前取第一个,首先找到第一个的位置 ...

  4. Servlet题库

    一.    填空题 Servlet中使用Session对象的步骤为:调用  HttpServletRequest.getSession()  得到Session对象,查看Session对象,在会话中保 ...

  5. 搞笑世界杯(codevs 1060)

    题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...

  6. 关于each

    1种 通过each遍历li 可以获得所有li的内容 <!-- 1种 --> <ul class="one"> <li>11a</li> ...

  7. c语言字符集

    一.字符常量 'A', 'B','\n','\'','1' 二.字符类型变量的赋值 char c1='A'; char c2='b'; char c3=65; c2='\''; c2='\n'; 三. ...

  8. EF – 3.EF数据查询基础(下)数据关联

    5.5.1 <关于“数据关联”,你不一定清楚的事> 这讲视频比较全面地介绍了“一对一”.“一对多”和“多对多”三种数据关联类型在关系数据库和Entity Framework数据模型中的实现 ...

  9. Android中Service 使用详解(LocalService + RemoteService)

    Service 简介: Service分为本地服务(LocalService)和远程服务(RemoteService): 1.本地服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另外L ...

  10. Extjs ComboBox 动态选中第一项

    有时候我们希望通过Store加载过来的数据,ComboBoxItem能够选择第一条数据作为默认数据,我们可以这么操作: var storeinfo = Ext.create('Ext.data.Sto ...