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

来源:互联网 发布:如何做一名程序员 编辑:程序博客网 时间:2024/05/16 10:07

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
31 5 11 10 21 100 3
 

Sample Output
10482302
 

Source
2017 Multi-University Training Contest - Team 4

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6069

题目分析:根据约数个数定理,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));        }    }}


 
原创粉丝点击