2 条题解
-
1
预处理加有一点儿前缀和的知识,问题在于知不知道欧拉筛和埃筛,挺细节的,千万记得别i等N 会j
#include <iostream> using namespace std; const int N = 1e7 + 9; int a[N], p[N], q[N]; inline void sieve(int n) { for (int i = 0; i <= n; i++) p[i] = 1; p[0] = p[1] = 0; for (int i = 2; i * i <= n; i++) if (p[i]) for (int j = i * i; j <= n; j += i) p[j] = 0; } inline int d(int x) { int s = 0; while (x) { s += x % 10; x /= 10; } return s; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); sieve(N - 1); for (int i = 2; i < N; i++) q[i] = p[d(i)]; for (int i = 1; i < N; i++) { a[i] = a[i - 1]; if (p[i] && q[i]) a[i]++; } int t, l, r; cin >> t; while (t--) { cin >> l >> r; cout << a[r] - a[l - 1] << endl; } return 0; }
- 1
信息
- ID
- 1115
- 时间
- 2000ms
- 内存
- 256MiB
- 难度
- 9
- 标签
- (无)
- 递交数
- 170
- 已通过
- 18
- 上传者