HDU 6069 Counting Divisors (约数个数定理)

78 篇文章 0 订阅
77 篇文章 8 订阅

Counting Divisors

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


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(i^k) = (kc1 + 1) * (kc2 + 1) * ... * (kcj + 1),其中i = p1^c1*p2^c2*...*pj^cj,容易想到枚举每个素数对区间每个数字的贡献值(即幂指数),素数只需枚举到sqrt(r),最多只有7万多个,大于 sqrt(r)部分的幂指数不会超过1,计算答案的时候如果存在大于sqrt(r)的素约数,直接乘一个(k + 1)即可。

import java.util.*;

public class Main {
    
    public static final int MAX = 100005;
    public static final int MAXP = 1000005;
    public static final int MOD = 998244353;

    public static int pnum = 0, n;
    public static Scanner in = new Scanner(System.in);
    public static long l, r, k;
    public static boolean[] noprime = new boolean[MAXP];
    public static int p[] = new int[MAX];
    public static long a[] = new long[MAXP];
    public static long num[] = new long[MAXP];

    public static void getPrime() {
        for (int i = 2; i < MAXP; i ++) {
            if (!noprime[i]) {
                p[pnum ++] = i;
            }
            for (int j = 0; j < pnum && i * p[j] < MAXP; j ++) {
                noprime[i * p[j]] = true;
                if (i % p[j] == 0) {
                    break;
                }
            }
        }
    }

    public static long cal(long l, long r, long k) {
        long ans = 0;
        for (long i = l; i <= r; i ++) {
            int idx = new Long(i - l).intValue();
            a[idx] = 1;
            num[idx] = i;
        }
        for (int i = 0; i < pnum && p[i] * p[i] <= r; i ++) {
            long val = (l + p[i] - 1) / p[i] * p[i];
            for (; val <= r; val += p[i]) {
                int cnt = 0, idx = new Long(val - l).intValue();
                while (num[idx] % p[i] == 0) {
                    num[idx] /= p[i];
                    cnt ++;
                }    
                a[idx] = (a[idx] * (k * cnt + 1)) % MOD;
            }
        }
        for (long val = l; val <= r; val ++) {
            int idx = new Long(val - l).intValue();
            ans = (num[idx] == 1) ? (ans + a[idx]) % MOD : (ans + a[idx] * (k + 1)) % MOD;
        }
        return ans;
    }

    public static void main(String[] args) {
        getPrime();
        n = in.nextInt();
        for (int ca = 1; ca <= n; ca ++) {
            l = in.nextLong();
            r = in.nextLong();
            k = in.nextLong();
            System.out.println(cal(l, r, k));
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值