Codeforce 546D

Soldier and Number Game

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

Submit Status Practice CodeForces 546D

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

23 16 3

Output

25
#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, 故已经算出来了

  

时间: 2025-01-04 11:46:25

Codeforce 546D的相关文章

CodeForce 448C 木片填涂问题

题目大意:有多片木片需要填涂,可以每次横着涂一行,也可以一次涂一列,当然你涂一行时遇到中间长度不够高的木片,填涂到此中断 这题目运用dfs能更容易的解出,虽然还是十分不容易理解 1 #include <iostream> 2 3 using namespace std; 4 5 #define N 5010 6 int a[N],n; 7 8 int Min(int c,int d) 9 { 10 return c<d?c:d; 11 } 12 int dfs(int c,int d,i

Codeforce 水题报告(2)

又水了一发Codeforce ,这次继续发发题解顺便给自己PKUSC攒攒人品吧 CodeForces 438C:The Child and Polygon: 描述:给出一个多边形,求三角剖分的方案数(n<=200). 首先很明显可能是区间dp,我们可以记f[i][j]为从i到j的这个多边形的三角剖分数,那么f[i][j]=f[i][k]*f[j][k]*(i,j,k是否为1个合格的三角形) CodeForces 438D:The Child and Sequence 描述:给一个序列,要求支持区

codeforce 285 div2 D 题解

codeforce 285 div2 D 题解 说明 这道题目是看了思路分析才知道的,关键问题在于数据量过大,需要快速检索的同时不能辅助空间过大. 因此利用了下面3种方法结合解决该问题 康拓展开与逆康拓展开 树状数组 二分查找 代码 /** * @brief Codeforces Round #285 (Div. 2) d * @file d.cpp * @author mianma * @created 2015/01/27 18:18 * @edited 2015/01/29 22:45 *

codeforce Pashmak and Buses(dfs枚举)

1 /* 2 题意:n个同学,k个车, 取旅游d天! 3 要求所有的学生没有两个或者两个以上的在同一辆车上共同带d天! 输出可行的方案! 4 5 对于d行n列的矩阵,第i行第j列表示的是第i天第j个同学所在的车号! 6 也就是保证所有行不全相同,即每一列都是不相同的! 7 如果每一列都不相同就是表示第j个同学(第j列)在这d天中不会和其他同学(列)在这d天中 都在同一辆车中! 8 9 思路:对于每一列我们枚举d天该学生所在的车号!它的下一列只保证有一个元素和它不同就行了!依次下去! 10 11

codeforce 605BE. Freelancer&#39;s Dreams

题意:给你n个工程,做了每个工程相应增长x经验和y钱.问你最少需要多少天到达制定目标.时间可以是浮点数. 思路:杜教思路,用对偶原理很简易.个人建议还是标准解题法,凸包+线性组合. 1 #include<iostream> 2 #include<string> 3 #include<algorithm> 4 #include<cstdlib> 5 #include<cstdio> 6 #include<set> 7 #include&

Codeforce 214 Div 2 B.Hometask

题目描述: Description Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you? Y

Codeforce 22B Bargaining Table

B. Bargaining Table Bob wants to put a new bargaining table in his office. To do so he measured the office room thoroughly and drew its plan: Bob's office room is a rectangular room n?×?m meters. Each square meter of the room is either occupied by so

Codeforce 9C - Hexadecimal&#39;s Numbers

One beautiful July morning a terrible thing happened in Mainframe: a mean virus Megabyte somehow got access to the memory of his not less mean sister Hexadecimal. He loaded there a huge amount of n different natural numbers from 1 to n to obtain tota

codeforce 128C Games with Rectangle 排列组合

Games with Rectangle Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description In this task Anna and Maria play the following game. Initially they have a checkered piece of paper with a painted n?×?m rect