HDU 6069 Counting Divisors(求因子)

Counting Divisors

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


Problem Description
In mathematics, the function  d(n)  denotes the number 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  and  k , your task is to calculate the following thing :

(i=lrd(ik))mod998244353

 

Input
The first line of the input contains an integer  T(1T15) , denoting the number of test cases.

In each test case, there are  3  integers  l,r,k(1lr1012,rl106,1k107) .
 

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
 

Source
 
题意:
d(n)=n的因子数。 给你l r k,求如上的表达式的值。

POINT:
如何求每个(l-r)^k的因子我就不说了。
之前遍历每个l-r求出各个的因子数会TLE。
那么遍历他们可能存在的质因数,即从2 3 5……开始遍历。
遍历后的质数就不用考虑了,在过程中保存下答案就行了。
还有一个重点就是,大质数的因子数是k+1没错,但是要考虑到这个大质数有可能是别的树除下来得到的,所以要更新sum数组。这个在代码里注释。//大质数即大于1e6即没有打到表的。
思维还不够灵活,总是没有想到从反面考虑。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <map>
using namespace std;
#define LL long long
const LL MAXN=1e6+9;
const LL p = 998244353;
LL prime[MAXN+1];
LL sum[MAXN],num[MAXN];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2;i<=MAXN;i++)
    {
        if(!prime[i])prime[++prime[0]]=i;
        for(int j=1;j<=prime[0]&&prime[j]<=MAXN/i;j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0)
                break;
        }
    }
}
int main()
{
    getPrime();
    int cnt=(int)prime[0];
    int T;
    scanf("%d",&T);
    while(T--)
    {
        LL l,r,k;
        scanf("%lld %lld %lld",&l,&r,&k);
        for(LL i=l;i<=r;i++)
        {
            sum[i-l]=1;
            num[i-l]=i;
        }
        for(int i=1;i<=cnt;i++)
        {
            //求出l-r内第一个可以整除prime[i]的。
            LL flag=l%prime[i];
            LL fir;
            if(!flag) fir=l;
            else fir=(l-flag)+prime[i];
            for(LL j=fir;j<=r;j+=prime[i])
            {
                int ci=0;
                while(num[j-l]%prime[i]==0) ci++,num[j-l]/=prime[i];
                (sum[j-l]*=ci*k+1)%=p;
            }
        }
        LL ans=0;
        for(LL i=l;i<=r;i++)
        {
            if(num[i-l]!=1) sum[i-l]=(sum[i-l]*(k+1))%p;//大质数不绝对是k+1,sum[i-l]可能大于1.这个wa了蛮久的.
            (ans+=sum[i-l])%=p;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值