Problem:1511 Solution:2829984 Language:G++ Result: Accepted Time:31MS Memory:10320KB
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAX 1001000
bool isprime[MAX];
int prime[MAX];
long long sum[MAX];
int cnt=0;
void ini(){
memset(isprime, 1, sizeof isprime);
isprime[1] = 0;
for (int i = 2; i < MAX; i++){
if (isprime[i])prime[cnt++] = i;
for (int j = 0; prime[j]*i < MAX && j < cnt; j++) {
isprime[prime[j]*i] = 0;
if (i % prime[j] == 0)break;
}
}
for (int i = 1; i <= MAX; i++){
if(isprime[i])
sum[i]+=sum[i-1]-i;
else
sum[i]+=sum[i-1]+i;
}
}
int main() {
ini();
int T,a,b;scanf("%d",&T);
while (T--){
scanf("%d%d",&a,&b);
printf("%I64d\n",sum[b]-sum[a-1]);
}
return 0;
}
XTU OJ 1511求和
于 2023-12-13 14:54:50 首次发布