首页 > 代码库 > 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, true, sizeof p);
16     memset(num, 0, sizeof num);
17 
18     p[1] = false;
19     rep(i, 2, N - 6) if (p[i]){
20         for (int j = i; j <= N; j += i){
21             int tmp = j;
22             while (tmp % i == 0){
23                 ++num[j];
24                 tmp /= i;
25             }
26             p[j] = false;
27         }
28     }
29     
30     rep(i, 2, N - 6) num[i] += num[i - 1];
31 
32     scanf("%d", &T);
33     while (T--){
34         scanf("%d%d", &n, &m);
35         printf("%d\n", num[n] - num[m]);
36     }
37 
38     return 0;
39 
40 }

 

Codeforces 546D Soldier and Number Game(数论)