标签:
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 *************************************************/
10
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29
30 typedef long long ll;
31 const int MAXN = 5e6 + 10;
32 const int INF = 0x3f3f3f3f;
33 const int MOD = 1e9 + 7;
34 int cnt[MAXN];
35 int a[MAXN];
36 bool is_prime[MAXN];
37 int prime[MAXN/10];
38 ll dp[MAXN];
39 ll sum[MAXN];
40
41 void solve(void) {
42 memset (is_prime, true, sizeof (is_prime));
43 for (int i=2; i<=5000000; ++i) a[i] = i;
44 int tot = 0;
45 for (int i=2; i<=5000000; ++i) {
46 if (is_prime[i]) prime[++tot] = i;
47 for (int j=1; j<=tot && i*prime[j]<=5000000; ++j) {
48 int tmp = i * prime[j];
49 is_prime[tmp] = false;
50 if (a[tmp] > prime[j]) a[tmp] = prime[j];
51 if (i % prime[j] == 0) break;
52 }
53 }
54 dp[2] = 1;
55 for (int i=3; i<=5000000; ++i) {
56 dp[i] = dp[i/a[i]] + 1;
57 }
58 for (int i=2; i<=5000000; ++i) {
59 sum[i] = sum[i-1] + dp[i];
60 }
61 }
62
63 int main(void) { //Codeforces Round #304 (Div. 2) D. Soldier and Number Game
64 solve ();
65 int T; scanf ("%d", &T);
66 while (T--) {
67 int l, r; scanf ("%d%d", &r, &l);
68 if (l == r) {
69 puts ("0"); continue;
70 }
71 printf ("%I64d\n", sum[r] - sum[l]);
72 }
73
74 return 0;
75 }
数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game
标签:
原文地址:http://www.cnblogs.com/Running-Time/p/4695668.html