• Codeforce 546D


    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 ", res[a] - res[b]); } }

    另一种做法是:充分利用已经得到的结果, 如 res[100] = res[2] + res[100 / 2] 其中由于 2 和 50 都小于 100, 故已经算出来了

      

  • 相关阅读:
    适合新手小白的UI学习路线完整版
    UI设计课程教程分享:Banner的设计和技巧
    UI设计:C4D作品案例分享
    还在凭实力单身吗,那是因为你还没学会这项技术
    PS故障风海报制作技术分享
    你真的了解标签栏设计吗?
    来看看N多设计师笔下的Spider Man
    羡慕女设计师啊,天生色感好!
    43. Multiply Strings
    40. Combination Sum II
  • 原文地址:https://www.cnblogs.com/orchidzjl/p/4668706.html
Copyright © 2020-2023  润新知