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 内核高手的四个方法
    专访CEO何朝曦:深信服高速成长的秘诀
    世界上最让人痛苦的是无所事事(年纪轻轻当项目经理、研发总监真的不好)
    2015 8月之后"云计算"学习计划
    程辉:创造云计算的第四种商业模式(送源码的托管云)
    配置restful webservice 框架restkit
    跟我一起玩转Sencha Touch 移动 WebApp 开发1
    数据更新最佳实践
    addEventListener和attachEvent以及element.onclick的区别
    java线程池:ThreadPoolExecutor
  • 原文地址:https://www.cnblogs.com/ToTOrz/p/7486627.html
Copyright © 2011-2022 走看看