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
  • 相关阅读:
    Linux操作篇之配置Samba
    Chrome扩展实现网页图片右键上传(以E站图片搜索为例)
    Linux开机自动挂载NFS配置的一个误区
    ffmpeg指令解读海康威视摄像头
    linux服务器性能调优之tcp/ip性能调优
    多线程程序设计中的8条简单原则
    初识文件系统
    socket中的listen到底干了哪些事情?
    ip面向无连接?TCP面向连接?HTTP连接方式?
    网络层和数据链层的区别
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7486627.html
Copyright © 2011-2022 走看看