1 条题解
信息
- ID
- 1167
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- 递交数
- 75
- 已通过
- 9
- 上传者
考察点:前缀和
x[i],并维护前缀和数组s[i](s[i]表示 1 到 i 的圈圈总数)。查询时,通过s[b] - s[a-1]即可在 O (1) 时间内得到区间和。// 律己则安,越是执着,便是越苦;
// 安下心来,看该看的风景,做该做的事。
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e6;
int c[10] = {1, 0, 0, 0, 1, 0, 1, 0, 2, 1}; // 将0 ~ 9的圈圈数打表列出
int x[N + 1], s[N + 1];
void init(){
for(int i = 1; i <= N; i ++){
int t = i;
while(t){
x[i] += c[t % 10];
t /= 10;
}
s[i] = s[i - 1] + x[i];
}
}
void solve() {
int a,b;cin >> a >> b;
cout << s[b] - s[a - 1] << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int T = 1;
init();
cin >> T;
while (T--)
solve();
return 0;
}