2015东北四省赛 L题 线性筛+积性函数 求因子和

Given an interval [x, y], calculate the sum of all the factors of the whole integers from x to y (include x and y).

Let describe it officially. Let's definite f (i) is the sum of factors of i, you should work out S(x, y), here S(x, y) is:

输入

There are multiple test cases, the number of cases T (1 ≤ T ≤ 105) will be given in the first line of input data. 
Each test case will include two integer x and y (1 ≤ x < y ≤ 5 × 106).

 

输出

For the ith test case, output “Case $i:” in the first line.
And print the answer of S(x, y) in a single line. The answer may be very large, so moudule 10000007 before printing it.

样例输入

31 65 1212 300

样例输出

Case $1:33Case $2:112Case $3:74210

提示

F (1) = 1 = 1



f (2) = 1 + 2 = 3



f (3) = 1 + 3 = 4



f (4) = 1 + 2 + 4 = 7



f (5) = 1 + 5 = 6



f (6) = 1 + 2 + 3 + 6 = 12



So the answer is 1 + 3 + 4 + 7 + 6 + 12 = 33


 


来源

2015大连海事

#include <iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define ll long long
using namespace std;
const int maxn=5000000+10;
const int mod=10000007;
int vis[maxn];
ll prime[maxn];
ll fac[maxn];//因子和积性函数
int num[maxn];//i这个数的最小质因子的幂指数
int qpow(int a,int b)
{
    int res=1;
    while(b)
    {
        if(b&1)
            res=res*a;
        a=a*a;
        b=b/2;
    }
    return res;
}
void init()
{
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    int cnt=0;
    fac[1]=1;
    fac[0]=0;
    for(ll i=2;i<=maxn;i++)
    {
        if(!vis[i])
        {
            prime[cnt++]=i;
            fac[i]=1+i;
            num[i]=1;
        }
        for(ll j=0;j<cnt&&i*prime[j]<=maxn;j++)
        {
            vis[i*prime[j]]=1;//该数的最小质因子为prime[j]
          if(i%prime[j]==0)//若这个数为2*6 或者为2*4(2^3)
          {
            num[i*prime[j]]=num[i]+1;
            //判断这个数是否为p^k次方形式
              int res=i;
              while(res%prime[j]==0)
                {res=res/prime[j];}
              int ok=1;
               if(res==1)
                ok=0;
            //
              if(ok==0)//若为p^k次方形式,则需单独计算 f[p^k]=f[p^k-1]+p^k;
             fac[i*prime[j]]=fac[i]+qpow(prime[j],num[i]+1);
             else//若不为,则f[n]=f[p^k]*f[n/(p^k)];
               fac[i*prime[j]]=fac[i/qpow(prime[j],num[i])]*fac[qpow(prime[j],num[i]+1)];
             break;
          }
          else//若这个数为互质的数相乘
          {
                 num[i*prime[j]]=1;
                 fac[i*prime[j]]=fac[i]*fac[prime[j]];
          }
        }
    }
   // for(int i=1;i<=12;i++)
    //    printf("%d ",fac[i]);
    for(int i=2;i<=maxn;i++)
        fac[i]=(fac[i-1]+fac[i])%mod;

}
int main()
{
    init();
    int t;
    int cas=1;
    cin>>t;
    while(t--)
    {
           int x,y;
           scanf("%d%d",&x,&y);
        printf("Case $%d:\n",cas++);
        printf("%lld\n",(fac[y]-fac[x-1]+mod)%mod);
    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值