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. com.netflix.zuul.exception.ZuulException: Forwarding error

    一.问题描述 在使用Spring Cloud的zuul组件,做路由转发时,每次重新启动后端服务,头几次调用都会出现com.netflix.zuul.exception.ZuulException: F ...

  2. windos 开启openssl

    前面我使用的是wampserver百度提示的软件,然后我卸载了,自己重新再官网上下载了一个比较新的版本,然后我按照的时候用默认路径,他的的都不用怎么配置,新版本都给你弄好了. 低版本的要在httped ...

  3. ubuntu server 启用mysql日志

    1.要启动mysql日志,你就要找到mysql 核心的文件my.cnf  (路径:/etc/mysql) 在命令窗口输入:cd /etc/mysql 在命令窗口输入:ls 你就可以看到my.cnf文件 ...

  4. 关于移动端rem适配

    var num = 1 / window.devicePixelRatio; var fontSize = document.documentElement.clientWidth / 10; doc ...

  5. Shell里面获取路径的方式

    1. $0 #!/bin/sh echo $0 2.shFile=$(readlink -f $0) #!/bin/sh shFile=$() shDir=$(dirname ${shFile})ec ...

  6. nginx 隐藏 index.php 和 开启 pathinfo 模式的配置

    nginx 通过 location 的规则匹配将 php 转发给 php-fpm 处理后获取结果然后返回给客户端,转发模式可以通过 unix sock 或 tcp socket 方式.百度了好多文章我 ...

  7. mac bash 下使用vi 快捷方式——因为没有alt键 所以没有办法 用vi模式也非常方便的

    set -o emacs ##切到emacs模式 set -o vi ##切到vi模式 set -o ## 查看当前选项的设置状态 所以你只需要在.bashrc下加入 set -o vi 然后,使用E ...

  8. 【Jmeter_WebService接口】对项目中的GetProduct接口生成性能脚本

    一.环境信息 https://xxx.xxx.svc?wsdl 用户名:username 密码:password 对其中的GetProduct接口进行测试 二.通过soapui获取soup请求信息 1 ...

  9. laravel Eloquent 查询数据库判断获取的内容是否为空

    原文地址:https://www.cnblogs.com/love-snow/articles/7205338.html 在使用 Laravel Eloquent 模型时,我们要判断取出的结果集是否为 ...

  10. SQL优化过程中常见Oracle HINT

    在SQL语句优化过程中,我们经常会用到hint,现总结一下在SQL优化过程中常见Oracle HINT的用法: 1. /*+ALL_ROWS*/ 表明对语句块选择基于开销的优化方法,并获得最佳吞吐量, ...