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.

    Sample test(s)
    input
    2
    3 1
    6 3
    
    output
    2
    

    5

    这题用到了一个公式,primefactors[a] = primefactors[a / primediviser[a]] + 1,primefactor[a]表示a因数中素数的个数。

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

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #include<math.h>
    #include<vector>
    #include<map>
    #include<queue>
    #include<stack>
    #include<string>
    #include<algorithm>
    using namespace std;
    #define maxn 5000050
    int c[maxn],prime[maxn];
    void init()
    {
    	memset(c,0,sizeof(c));
    	int i,j,t;
    	t=1;
    	for(i=2;i<=maxn;i++){
    		if(c[i]==0){
    			for(j=2;j*i<=maxn;j++){
    				c[j*i]=c[j]+1;
    			}
    		}
    	}
    	for(i=3;i<=maxn;i++){
    		c[i]+=c[i-1];
    	}
    }
    
    int main()
    {
    	int a,b,i,j,T;
    	init();
    	scanf("%d",&T);
    	while(T--)
    	{
    		scanf("%d%d",&a,&b);
    		printf("%d
    ",c[a]+a-c[b]-b);
    	}
    	return 0;
    }


  • 相关阅读:
    .Net -- NLog日志框架配置与使用
    Prism -- 简介
    Prism.WPF -- Prism框架使用(下)
    Prism.WPF -- Prism框架使用(上)
    Win32Api -- 关闭当前应用
    WPF -- 一种圆形识别方案
    C#语言特性及发展史
    WPF -- 一种实现本地化的方法
    DEV中右键菜单如何只在非空单元格上显示?
    打开一个窗体,其他窗体处于不可编辑状态
  • 原文地址:https://www.cnblogs.com/herumw/p/9464717.html
Copyright © 2011-2022 走看看