筛法是一种很快的方法,贴代码纪念一下。 做法很像筛法
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algorithm> 7 #include <string> 8 #include <queue> 9 #include <stack> 10 #include <vector> 11 #include <map> 12 #include <set> 13 #include <functional> 14 #include <time.h> 15 16 using namespace std; 17 18 const int INF = 1<<30; 19 const int MAXN = (int) 1e6+5; 20 21 int a[MAXN]; 22 23 inline int myAbs(int x) { 24 return x>0 ? x : -x; 25 } 26 27 int main() { 28 #ifdef Phantom01 29 freopen("HNU12876.in", "r", stdin); 30 #endif //Phantom01 31 32 memset(a, 0, sizeof(a)); 33 for (int i = 1; i < MAXN; i++) 34 for (int j = i<<1; j < MAXN; j += i) 35 a[j]+=i; 36 for (int i = 1; i < MAXN; i++) 37 a[i] = myAbs(a[i]-i); 38 39 int l, r, k; 40 int T = 1; 41 while (scanf("%d%d%d", &l, &r, &k)!=EOF) { 42 if (l==r&&r==k&&k==0) break; 43 printf("Test %d: ", T++); 44 int ans = 0; 45 for (int i = l; i <= r; i++) 46 if (a[i]<=k) 47 ans++; 48 printf("%d ", ans); 49 } 50 51 return 0; 52 }