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. mongodb shell和Node.js driver使用基础

    开始: Mongo Shell 安装后,输入mongo进入控制台: //所有帮助 > help //数据库的方法 > db.help() > db.stats() //当前数据库的状 ...

  2. 启动mysql5.7异常The server quit without updating PID file [FAILED]sql/data/***.pi根本解决方案

    异常表现 mysql5.7启动时报错 Starting MySQL...The server quit without updating PID file [FAILED]sql/data/insta ...

  3. New task CodeForces - 788E (线段树优化dp)

    比较套路的一个题, 对每个数维护一颗线段树来转移就好了. #include <iostream> #include <algorithm> #include <cstdi ...

  4. 迭代FFT

    int reverse(int x, int len){ ; ; i < len; i <<= ){ t <<= ; ; } return t; } Complex A[ ...

  5. spring boot(二)web综合开发

    上篇文章介绍了Spring boot初级教程:spring boot(一):入门,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spring boot的其它特 ...

  6. MongoDB 教程(一):了解 NoSQL

    概述: MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介于关系数据库和非关系数据库之间的产品, ...

  7. InnoDB存储引擎介绍-(4)Checkpoint机制一

    检查点的工作机制: innodb会自动维护一个检查点的机制,叫做 fuzzy checkpointing(当然sharp checkpoint也是检查点之一),fuzzy checkpointing就 ...

  8. os模块,序列化模块,json模块,pickle模块

    一.os模块os.system("bash command") 运行shell命令,直接显示 os.popen("bash command).read() 运行shell ...

  9. [NOIP 2015TG D2T3] 运输计划

    题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...

  10. [LeetCode] 23. Merge k Sorted Lists ☆☆☆☆☆

    转载:https://leetcode.windliang.cc/leetCode-23-Merge-k-Sorted-Lists.html 描述 Merge k sorted linked list ...