1611: 大四下之回来看看
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 142 Solved: 25
[ Submit][ Status][ Web Board]
Description
今天你回来了,看到了菜鸟学弟在算一个函数,函数是F(x):表示x的所有因子和。G(L,R):表示区间[L,R]的f(x)的和《L<=x<=R》。你看不下去学弟一个一个的算,于是你写了个程序给你的学弟。
Input
第一行是n(-1结束输入)
接下来是n行 ,每行一对数L,R
<范围在[1,100000]>
Output
输出答案对520623取模
Sample Input
1
5 6
-1
Sample Output
18
【解析】
这道题的话我一开始模拟是超时的...其实还有个方法是个预处理,不过我并不知道..大佬点拨,才懂了一点。
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
int f[100100]={0};
f[1]=1;f[2]=4;
for (int i=3;i<=100000;i++)//这么多个数字
{
int sum=0;
int j;
for (j=1;j<sqrt(i);j++)
{
if (i%j==0)
sum+=(j+i/j)%520623;//求因子和
}
if (j*j==i) sum+=j;
f[i]=(f[i-1]+sum)%520623;
}
int n;
scanf("%d",&n);
while (n!=-1)
{
for (int i=0;i<n;i++)
{
int x,y;
scanf("%d %d",&x,&y);
int ans=(f[y]-f[x-1])+520623;题目的要求取模
ans%=520623;
printf("%d\n",ans);
}
scanf("%d",&n);
}
return 0;
}