If the table is typed, the memory will be exceeded. I think of a way to solve this problem. When the data given in the question is 3000000, I divide 3 million into 300 pieces of data, and store all pieces of data. When calculating the data, I first calculate the total data between x and y, then calculate the scattered data.
The idea is good, but why can't it work? In addition, we can see that the last two groups of data are stuck.
I am confused.
The comments in the Code are my previous code. In the end, this question turned out to have no sense of violence.
#include<stdio.h>#include<string.h>#define N 3000005int a[N];int b[N];__int64 c[310];int main(){ int i,j; for(i=2;i<N;i++) a[i]=i; for(i=2;i<N;i++) { if(b[i]==1) continue; a[i]=i-1; for(j=i+i;j<N;j=j+i) { b[j]=1; a[j]=a[j]/i*(i-1); } } a[1]=a[0]=0; c[1]=0; __int64 temp; c[0]=0; j=1; temp=0; for(i=2;i<N;i++) { temp+=a[i]; if(i%10000==0) c[j++]=temp; } int x,y; while(scanf("%d%d",&x,&y)!=EOF) { __int64 sum=0; /* int x1,y1; x1=x/10000; y1=y/10000; if(x1==y1) { for(i=x;i<=y;i++) sum+=a[i]; } else { sum+=c[y1]-c[x1]; for(i=x1*10000+1;i<x;i++) sum-=a[i]; for(i=y1*10000+1;i<=y;i++) sum+=a[i]; } */ for(i=x;i<=y;i++) sum+=a[i]; printf("%I64d\n",sum); } return 0;}