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


  • 相关阅读:
    http://codeforces.com/problemset/problem/594/A
    http://codeforces.com/contest/610/problem/D
    http://codeforces.com/contest/612/problem/D
    http://codeforces.com/problemset/problem/712/D
    http://codeforces.com/problemset/problem/545/D
    18/10/26 周五集训队第二次比赛补题题解
    归并排序
    hdu 2510 符号三角形
    位运算符的小总结
    hdu 1584 蜘蛛纸牌
  • 原文地址:https://www.cnblogs.com/herumw/p/9464717.html
Copyright © 2011-2022 走看看