zoukankan      html  css  js  c++  java
  • CodeForces 546 D. Soldier and Number Game(素数有关)

    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

    题目链接:http://codeforces.com/problemset/problem/546/D

    ********************************************

    分析:用到一个公式

    primefactors[a] = primefactors[a / primediviser[a]] + 1,

    primefactor[a]表示a因数中素数的个数。

    只要把a,b的primefactor[]算出来,减一下就行了。

    AC代码:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <algorithm>
     5 #include <cmath>
     6 #include <stack>
     7 #include <map>
     8 #include <vector>
     9 using namespace std;
    10 
    11 #define N 5000000
    12 #define INF 0x3f3f3f3f
    13 
    14 int c[N];
    15 
    16 void p()
    17 {
    18     memset(c,0,sizeof(c));
    19     int i,j;
    20     for(i=2;i<=N;i++)
    21     {
    22         if(c[i]==0)
    23         {
    24             for(j=2;j*i<=N;j++)
    25                 c[j*i]=c[j]+1;
    26         }
    27     }
    28     for(i=3;i<=N;i++)
    29         c[i]+=c[i-1];
    30 }
    31 
    32 int main()
    33 {
    34     int T,a,b;
    35     p();
    36     scanf("%d", &T);
    37 
    38     while(T--)
    39     {
    40         scanf("%d%d", &a,&b);
    41         printf("%d
    ", c[a]+a-c[b]-b);
    42     }
    43     return 0;
    44 }
  • 相关阅读:
    HDU 3415 Max Sum of Max-K-sub-sequence 最长K子段和
    Android Fragment 真正彻底的解决(下一个)
    【数据分析面试题】一个 面试题,我的回答
    Swift初体验(两)
    MyEclipse10.0 集成 SVN
    CFileDialog 打开文件夹文件 保存文件夹文件
    基于thinkphp的uploadify上传图功能
    近20家银行手机银行签名被非法滥用风险分析
    设计模式【6】:适配器模式【接口适配】
    【学习笔记】编译原理-有限自己主动机
  • 原文地址:https://www.cnblogs.com/weiyuan/p/5773024.html
Copyright © 2011-2022 走看看