zoukankan      html  css  js  c++  java
  • Codeforces Round #304 (Div. 2) D 思维/数学/质因子/打表/前缀和/记忆化

    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 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.

    Examples
    Input
    2
    3 1
    6 3
    Output
    2
    5

    题意: t组数据 给你a b 两个数(a>=b)
    两个人进行一个游戏 初始时第一个人挑选一个数n给第二个人 第二个人选择一个n的因子k使得n/k尽可能的大
    使得n=n/k 并得1分 继续选择当前n的因子 继续游戏 当n==1时游戏结束 问第二个人的最大得分为多少?
    这里n=a!/b!
    题解: “数学上来先打表” 这句话堪称精辟
    首先a b的范围很大 暴力处理是不可能的
    分解开来n=a!/b!=(b+1)*(b+2).....*a; 为了使得n被多次分解 可以很容易的考虑到求每一项的质因子分解
    (只有是质因子分解,才能够使得分解后的因子个数最多 不做解释)并且统计所有的分解后的因子的个数之和就是答案
    对于询问1e6不可能每次都遍历去求
    考虑打表求1~5000000的每个数的质因子分解的个数 并且暴力去求每个数的质因子必然超时 考虑记忆化
    对于已经找过质因子个数的数进行存储 方便之后的数直接使用 免于重复计算
    并且只需要找到当前数的一个因子就可以 其他的已经被计算过 这就是记忆化的优化所在点
    并且记录前缀和
     对于询问直接输出 sum[a]-sum[b]
     1 //code by  drizzle
     2 #include<bits/stdc++.h>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<algorithm>
     7 #include<vector>
     8 #define ll __int64
     9 #define PI acos(-1.0)
    10 #define mod 1000000007
    11 using namespace std;
    12 int t;
    13 int a,b;
    14 int sum[5000005];
    15 int have[5000005];
    16 void fun()
    17 {
    18     sum[0]=0;
    19     sum[1]=0;
    20     have[1]=0;
    21     for(int  i=2;i<=5000000;i++)
    22     {
    23         if(i%2==0)//只需要找到当前数的一个因子就可以 其他的已经被计算过
    24         {
    25           have[i]=1+have[i/2];
    26           sum[i]=sum[i-1]+have[i];
    27           continue;
    28         }
    29         int flag=0;
    30         for(int j=3;j*j<=i;j+=2)
    31         {
    32             if(i%j==0)
    33             {
    34                 have[i]=1+have[i/j];
    35                 sum[i]=sum[i-1]+have[i];
    36                 flag=1;
    37                 break;
    38             }
    39         }
    40         if(flag==0)
    41         {
    42             have[i]=1;
    43             sum[i]=sum[i-1]+have[i];
    44         }
    45     }
    46 }
    47 int main()
    48 {
    49     fun();
    50     scanf("%d",&t);
    51     for(int i=1;i<=t;i++)
    52     {
    53         scanf("%d %d",&a,&b);
    54         printf("%d
    ",sum[a]-sum[b]);
    55     }
    56     return 0;
    57 }
  • 相关阅读:
    邮件系列3 --- 安全传输
    配置postfix支持虚拟域和虚拟用户
    Postfix+Sasl+Courier-authlib+Dovecot+MySQL+extmail 邮件系统部署
    在switch中的case语句中声明变量编译出错的解决方案
    关于自控力
    PUTTY使用Ctrl+s僵死的问题
    sqlite3里类似top的用法
    二维数组、行指针、指针数组、二级指针
    sqlite3_exec函数的使用
    基于s5pv210嵌入式linux使用其他动态、静态库文件程序的交叉编译
  • 原文地址:https://www.cnblogs.com/hsd-/p/5695462.html
Copyright © 2011-2022 走看看