2017 Multi-University Training Contest 4 && HDOJ 6069 Counting Divisors 【区间筛法】

来源:互联网 发布:魔兽争霸3 for mac 编辑:程序博客网 时间:2024/05/29 19:36


Counting Divisors

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit:524288/524288 K (Java/Others)
Total Submission(s): 707    Accepted Submission(s): 233

Problem Description

In mathematics,the function d(n) denotes thenumber of divisors of positive integer n.

For example,
d(12)=6 because 1,2,3,4,6,12 are all 12 's divisors.

In this problem, given
l,r andk, your task isto calculate the following thing :


 

 

Input

The first line of the input contains an integer T(1≤T≤15), denoting thenumber of test cases.

In each test case, there are 3 integers
l,r,k(1≤lr≤1012,rl≤106,1≤k≤107).

 

 

Output

For each test case, print a single line containing an integer, denoting the answer.

 

 

Sample Input

3

1 5 1

1 10 2

1 100 3

 

 

Sample Output

10

48

2302

 

【题意】


如图。


【思路】


根据约个数数定理,如果n可以分解质因数:n=p1^a1 × p2^a2 × p3^a3 * … * pk^ak ,那么n的约数的个数=(a1+1)(a2+1)(a3+1)…(ak+1)。


同理n^k的约数个数为(a1 * k+1)(a2 * k+1)(a3 * k+1)…(ak * k+1)。


于是问题的关键就在于[L,R]范围内的数的质因数分解,由于直接暴力算会超时,我们考虑用区间筛法进行优化。


由于10^12 大小的数如果能被质因子分解的话,那么质因子一定小于等于sqrt(10^12),所以我们先预处理出[1,1e6]范围内的素数,然后利用这些素数的倍数在[L,R]范围内筛选因子中含有该素数的数,然后利用上面的约数个数定理即可。


具体细节见代码,不理解的可以留言


#include <cstdio>#include <cmath>#include <cstring>#include <algorithm>using namespace std;#define mst(a,b) memset((a),(b),sizeof(a))#define rush() int T;scanf("%d",&T);while(T--)typedef long long  ll;const int maxn = 1000005;const ll mod = 998244353;const int INF = 0x3f3f3f3f;const double eps = 1e-6;int prime[maxn]= {0};int num_prime=0;bool isprime[maxn]= {0,0};ll num[maxn],temp[maxn];ll ans;ll mul(ll a,ll b)              //乘法取余{    return (a%mod)*(b%mod)%mod;}void init()                    //预处理[1,1e6]范围内的素数{    for(int i=2; i<maxn; i++)    {        if(!isprime[i])            prime[num_prime++]=i;        for(int j=0; j<num_prime&&i*prime[j]<maxn; j++)        {            isprime[i*prime[j]]=1;            if(i%prime[j]==0)                break;        }    }}void solve(ll l,ll r,ll k){    ll len=r-l+1;    for(ll i=0; i<len; i++)    {        num[i]=i+l;                             //区间里每一个数在筛的过程中的值,为了最后判断某个数是否为素数        temp[i]=1;                              //记录(l+i)的约数个数    }    for(int i=0; i<num_prime&&prime[i]<=r; i++) //枚举素数    {        ll j=(l-1+prime[i])/prime[i]*prime[i];  //找到区间里第一个能被该素数分解质因子的数        for(; j<=r; j+=prime[i])                //找因子中含有这个素数的数        {            ll cnt=0,tot=j;            while(tot%prime[i]==0)            {                cnt++;                tot/=prime[i];                num[j-l]/=prime[i];            }            temp[j-l]=mul(temp[j-l],cnt*k+1);   //分解质因子过程中某个素数有cnt个,那么约数个数应该乘上(cnt^k+1)        }    }    for(ll i=0; i<len; i++)    {        if(num[i]!=1)                           //没有被枚举的素数分解,说明l+i为素数,那么(l+i)^k的约数个数为(k+1)        {            ans=(ans+mul(temp[i],k+1))%mod;        }        else ans=(ans+temp[i])%mod;    }}int main(){    ll L,R,k;    init();    rush()    {        scanf("%I64d%I64d%I64d",&L,&R,&k);        ans=0;        solve(L,R,k);        printf("%I64d\n",ans);    }    return 0;}






阅读全文
1 0