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 withn / 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 ofn 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

题目大意:

一开始N是a!/b!.每一次士兵选择一个数x,使得n变成n/x.

他希望玩游戏的轮数尽可能的多,问给出的N最多可以玩几轮。

思路:

给出的Na!/b!,其中保证a>=b.那么我们的N其实就是从b+1~a之间所有数去拆分素因子的问题。

那么我们处理出从2~5e6这些数都可以拆分出来多少素因子然后维护一个前缀和即可。

代码:

#include<stdio.h>
#include<string.h>
int s[5000050],vt[5000050];
int init()
{
  int i,j;
  memset(s,0,sizeof(s));
  memset(vt,0,sizeof(vt));
  for(i=2;i<=5000005;i++)// 乍一看这代码时间复杂度很高,但是细想一下代码运行次数只有2*5000000次数左右,但是我们的vt数组就将我们时间复杂度降了下来,避免了不必要的计算
  {
    if(!vt[i])//判断i是不是素数
    {
      for(j=i;j<=5000005;j+=i)//对于一个素数,只有它的倍数才能被他整除 ,节省时间
      {
        int a=j;
        while(a%i==0)//对j进行拆分
        {
          s[j]++;
          a=a/i;
        }
        vt[j]=1;//j已经经历过了或者j不是素数
      }
    }
  }
  for(i=2;i<=5000001;i++)
  s[i]=s[i-1]+s[i];
}
int main()
{
  init();
  int a,b,t;
  scanf("%d",&t);
  while(t--)
  {
    scanf("%d%d",&a,&b);
    printf("%d\n",s[a]-s[b]);
  }
    return 0;
}

乍一看这代码时间复杂度很高,但是细想一下代码运行次数只有2*5000000次数左右,

codeforces546D(从一个数中拆分素数)的更多相关文章

  1. Python3求m以内的素数、求m个数中最小的n个数

    [本文出自天外归云的博客园] 题1:求m以内的素数(m>2) def find_all_primes_in(m): def prime(num): for i in range(2, num): ...

  2. 如何判断一个数是否为素数(zt)

    怎么判断一个数是否为素数? 笨蛋的作法: bool IsPrime(unsigned n){    if (n<2)    { //小于2的数即不是合数也不是素数    throw 0;    ...

  3. javascript小实例,编写一个方法,实现从n-m个数中随机选出一个整数

    别怪我是一个闷葫芦,没那么多花哨的语言,废话不多说,先说说小实例的要求: 编写一个方法,实现从n-m个数中随机选出一个整数,要求:传递的参数不足两个或者不是有效数字,返回[0-1]之间的随机数,需要解 ...

  4. (Miller Rabin算法)判断一个数是否为素数

    1.约定 x%y为x取模y,即x除以y所得的余数,当x<y时,x%y=x,所有取模的运算对象都为整数. x^y表示x的y次方.乘方运算的优先级高于乘除和取模,加减的优先级最低. 见到x^y/z这 ...

  5. java判断一个数是否为素数[转]

    http://blog.csdn.net/lwcumt/article/details/8027586 import java.util.Scanner; //质数又称素数,是指在一个大于1的自然数中 ...

  6. javascript基础程序(算出一个数的平方值、算出一个数的阶乘、输出!- !- !- !- !- -! -! -! -! -! 、函数三个数中的最大数)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. ytu 1061: 从三个数中找出最大的数(水题,模板函数练习 + 宏定义练习)

    1061: 从三个数中找出最大的数 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 154  Solved: 124[Submit][Status][We ...

  8. SGU 275 To xor or not to xor 高斯消元求N个数中选择任意数XORmax

    275. To xor or not to xor   The sequence of non-negative integers A1, A2, ..., AN is given. You are ...

  9. 小易邀请你玩一个数字游戏,小易给你一系列的整数。你们俩使用这些整数玩游戏。每次小易会任意说一个数字出来,然后你需要从这一系列数字中选取一部分出来让它们的和等于小易所说的数字。 例如: 如果{2,1,2,7}是你有的一系列数,小易说的数字是11.你可以得到方案2+2+7 = 11.如果顽皮的小易想坑你,他说的数字是6,那么你没有办法拼凑出和为6 现在小易给你n个数,让你找出无法从n个数中选取部分求和

    小易邀请你玩一个数字游戏,小易给你一系列的整数.你们俩使用这些整数玩游戏.每次小易会任意说一个数字出来,然后你需要从这一系列数字中选取一部分出来让它们的和等于小易所说的数字. 例如: 如果{2,1,2 ...

随机推荐

  1. Vue音乐项目笔记(四)(搜索页面提取重写)

    1.如何通过betterScroll组件实现上拉刷新 https://blog.csdn.net/weixin_40814356/article/details/80478440 2.搜索页面跳转单曲 ...

  2. js动态修改title

    问题描述: 由于微信浏览器只在页面首次加载时初始化了标题title,之后就没有再监听 window.title的change事件.所以这里修改了title后,立即创建一个请求,加载一个空的iframe ...

  3. ArrayList详细

    (IList值的集合 索引访问 ArrayList类IDictionary:键/值对 HashTable类 可变的集合 长度自动增长) ICollection-----IEnumerable--- I ...

  4. AWR报告学习示例

    1.  参数1  AAS AAS讲解 elapsed 为 该AWR性能报告的时间跨度 DB_TIME =所有前台session花费在database调用上的总和时间. 注意:前台进程 foregrou ...

  5. WDA基础十一:Excel上载

    1.因为是做建单的例子,所以没有做结果显示,在加了两个属性 DATESOURCE  Type XSTRING FILENAME         Type STRING 2.在VIEW中新建elemen ...

  6. Eclipse集成Maven进行开发

    Eclipse上Maven环境配置使用 (全) Eclipse EE+MAVEN配置以及POM.XML的使用 Eclipse报错:Referenced classpath provider does ...

  7. 改写了禁用或启用oracle数据库的约束的存储过程

    改写了网上某位大侠(最开始的源头是哪位没记住)写的禁用或启用oracle数据库所有约束的存储过程,增加了异常控制,以使发生异常时也可以执行下去. –调用过程: 执行前先 set serveroutpu ...

  8. echarts-------饼形图

    首先echarts是一个可以提供给用户体验效果更好的一个图形界面, Canvas 类库 ZRender. 1.下载echarts的js,可以在官方网址进行下载echarts.min.js 2.将下载下 ...

  9. 微信小程序 无限加载 上拉加载更多

    加载更多,其实就是再次向接口发送请求,把返回的数据,追加到渲染页面的数组里的过程,具体实现实例如下: demo.js // pages/project/project.js const app = g ...

  10. jquery attr方法获取input的checked属性问题

    1.通过prop方法获取checked属性,获取的checked返回值为boolean,选中为true,否则为flase <input type="checkbox" id= ...