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(c, 0, sizeof(c));
10     for(int i = 2; i<=maxn; i++) {
11         if(!p[i]) {
12             p[i] = i;
13             for(int j = i+i; j<=maxn; j+=i) {
14                 p[j] = i;                //求出一个数的最大素因子
15             }
16         }
17     }
18     for(int i = 2; i<=maxn; i++) {
19         p[i] = p[i/p[i]]+1;             //这里, p[4]就等于p[2]+1, p[8] = p[8/2]+1 = p[4]+1这样类推就可以求出答案
20         c[i] = c[i-1]+p[i];
21     }
22     int t, a, b;
23     cin>>t;
24     while(t--) {
25         scanf("%d%d", &a, &b);
26         printf("%d\n", c[a]-c[b]);
27     }
28 }    
时间: 2024-08-26 14:15:13

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 打表(求质因子个数)

题目:戳我 题意:题意看懂了上面的一大串英文之后其实很简单,就是给你一个正整数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

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>

Codeforces 464C Substitutes in Number(高效+快速幂)

题目链接:Codeforces 464C Substitutes in Number 题目大意:给定一个字符串,以及n中变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一 个数,取模1e9+7. 解题思路:将操作倒过来处理,这样维护每个数来的val,len两个,val表示对应数值取模1e9+7,len表示对应有多少 位,再计算的过程中要使用. #include <cstdio> #include <cstring> #include <vector>

CodeForces 546D (求素因子个数)

/***************************** CodeForces 546D Author:herongwei Created Time: 2016/5/31 13:00:00 File Name : main.cpp 给出一个n,n开始是a!/b!,每次用一个x去整除n得到新的n, 最后当n变成1的时候经过了几轮得分就是这个轮数, 要求最大的分数是多少 [solve] 就是一个求整数质因子个数的题目, 阶乘我们不需要算,我们知道在a>b的时候, b!都约掉了,那么我们只需计算出

Codeforces Round #304 (Div. 2)——D素数筛+dp——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