codeforces 546D Soldier and Number Game

题目链接

这个题, 告诉你a, b的值, 那么只需要求出b到a之间的数, 每个数有多少个因子就可以。

具体看代码, 代码里面有解释

 #include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
const int maxn = ;
int p[maxn], c[maxn];
int main()
{
memset(p, , sizeof(p));
memset(c, , sizeof(c));
for(int i = ; i<=maxn; i++) {
if(!p[i]) {
p[i] = i;
for(int j = i+i; j<=maxn; j+=i) {
p[j] = i; //求出一个数的最大素因子
}
}
}
for(int i = ; i<=maxn; i++) {
p[i] = p[i/p[i]]+; //这里, p[4]就等于p[2]+1, p[8] = p[8/2]+1 = p[4]+1这样类推就可以求出答案
c[i] = c[i-]+p[i];
}
int t, a, b;
cin>>t;
while(t--) {
scanf("%d%d", &a, &b);
printf("%d\n", c[a]-c[b]);
}
}
上一篇:Thread类中start()方法喝run()方法有什么不同?


下一篇:洛谷P1186 玛丽卡 spfa+删边