zoukankan      html  css  js  c++  java
  • Codeforces 546D. Soldier and Number Game

    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.

     设dp[i]为i这个数能最多除的次数

    易得

      dp[i] = 1 (i是素数) dp[i] = dp[i/p] + 1 (p是素数)

     所以在线性筛素数的时候处理就好了
     ps:
      由于数据很大需要用scanf
     
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    #define SIZE 30005
    
    bool isprime[5000001];
    int prime[500001];
    int dp[5000001],sum[5000001] = {0};
    int primesize = 0;
    void initPrime(){
      memset(isprime,1,sizeof(isprime));
      isprime[1] = false;
      dp[0] = 0;
      dp[1] = 0;
      int ListSize = 5000005;
      for (int i=2;i<= ListSize ; i++){
        if (isprime[i]) {
          dp[i] = 1;
          primesize ++; prime[primesize] = i;
        }
        for (int j=1;j<=primesize && i*prime[j] <= ListSize; j++){
          isprime[i*prime[j]] = false;
          if (i % prime[j] == 0) {
            dp[i] = dp[i/prime[j]] + 1;
            break;
          }
        }
        if (!dp[i]){
          for (int j=1;j<=primesize && prime[j]*prime[j] <= i; j++){
            if (i % prime[j] == 0) {
              dp[i] = dp[i/prime[j]] + 1;
              break;
            }
          }
        }
     }
    }
    
    int a,b,t;
    
    int main()
    {
      // freopen("test.in","r",stdin);
      // ios::sync_with_stdio(false);
    
      initPrime();
    
      for (int i=1;i<=5000000;i++){
        sum[i] = dp[i] + sum[i-1];
      }
      scanf("%d",&t);
      for (int i=1;i<=t;i++){
        scanf("%d %d",&a,&b);
        printf("%d
    ",sum[a]-sum[b]);
      }
    
      return 0;
    }
    View Code
  • 相关阅读:
    KMP字符串查找
    基数排序
    二分查找来查找旋转数组
    通过前序(后序)+中序创建树
    二维数组找最大值
    用字符串的空格替换其他字符
    桶排序
    字符串的排列
    和为S的两个数字
    整数中1出现的次数(从1到n整数中1出现的次数)
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7486627.html
Copyright © 2011-2022 走看看