CodeForces 546D Soldier and Number Game 打表(求质因子个数)

  题目:戳我

  题意:题意看懂了上面的一大串英文之后其实很简单,就是给你一个正整数n,问你n有多少个质因子,不过这里n是通过a!/b!给定的,也就是说n=(a!/b!);

  分析:由于这里1 ≤ b ≤ a ≤ 5 000 000,数据很大,所以简单暴力一定超时,具体看代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int M = 5e6+5;
 5
 6 int t;  //测试组数
 7 int num[M] = {0};  //打表,将n的质因子个数存到num[n]中
 8 int sum[M] = {0};  //sum[i]表示从num[1]到num[i]的和
 9 void getsum( )  {
10     num[1] = 1;
11     num[2] = 1;
12     for( int i=2; i<M; i++ )    {
13         if( num[i] > 1 )  //判断i是否为素数
14             continue;
15         num[i] = 1;
16         for( int j=2; j*i<M; j++ )  {
17             num[j*i] = num[j] + 1;
18         }
19     }
20     for( int i=1; i<M; i++ )
21         sum[i] = sum[i-1] + num[i];
22 }
23
24 int main()
25 {
26     getsum();
27     scanf("%d", &t );
28     while( t-- )    {
29         int a, b;
30         scanf("%d %d", &a, &b );
31         printf("%d\n", sum[a] - sum[b] );
32     }
33 }
时间: 2024-08-03 16:51:09

CodeForces 546D Soldier and Number Game 打表(求质因子个数)的相关文章

Codeforces 546D Soldier and Number Game(数论)

类似筛素数的方法--求出前缀和.然后直接O(1)回答即可. 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i,a,b) for(int i(a); i <= (b); ++i) 6 7 const int N = 10000000 + 10; 8 9 int num[N]; 10 bool p[N]; 11 int T, n, m; 12 13 int main(){ 14 15 memset(p,

codeforces 546D Soldier and Number Game

题目链接 这个题, 告诉你a, b的值, 那么只需要求出b到a之间的数, 每个数有多少个因子就可以. 具体看代码, 代码里面有解释 1 #include<bits/stdc++.h> 2 using namespace std; 3 #define pb(x) push_back(x) 4 const int maxn = 5000005; 5 int p[maxn], c[maxn]; 6 int main() 7 { 8 memset(p, 0, sizeof(p)); 9 memset(

Codeforces546D: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

Easy Number Challenge(暴力,求因子个数)

Easy Number Challenge Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 236B Appoint description:  System Crawler  (2016-04-26) Description Let's denote d(n) as the number of divisors of a

CF 546D Soldier and Number Game

Describe 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

Codefo 546D. Soldier and Number Game

题目链接:http://codeforces.com/problemset/problem/546/D 题意:输入a,b,n=a!/b!,求n 最多除以多少次变为1. 分析:相当于求n有多少个质因子.即求从b+1到a之间的数字质因子和为多少. #include <iostream> #include <cstring> #include <cstdio> using namespace std; int v[5000005];//v[i]记录i是否为质数 int pri

数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:这题就是求b+1到a的因子个数和. 3 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 4 */ 5 /************************************************ 6 Author :Running_Time 7 Created Time :2015-8-1 14:08:34 8 File Name :B.cpp 9 ******************************

DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game

题目传送门 1 /* 2 题意:b+1,b+2,...,a 所有数的素数个数和 3 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 4 最后改为i之前所有素数个数和,那么ans = dp[a] - dp[b]: 5 详细解释:http://blog.csdn.net/catglory/article/details/45932593 6 */ 7 #include <cstdio> 8 #include <algorithm>

HDU 4952 Number Transformation 打表规律

点击打开链接 Number Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 495    Accepted Submission(s): 248 Problem Description Teacher Mai has an integer x. He does the following operation