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;
    }


  • 相关阅读:
    JavaScript 正则表达式
    android源代码提示文本框还能输入多少个字符
    js实现鼠标点击input框后里面的内容就消失代码
    使用prompt输入一句英文句子和排序方式(升/降),将所有单词按排序方式排序后在网页上输出
    Javascript输出表格
    flutter 按键监听
    3.声明
    2.基础类型
    1.安装TypeScrpit
    苹果app证书
  • 原文地址:https://www.cnblogs.com/herumw/p/9464717.html
Copyright © 2011-2022 走看看