Counting Divisors

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

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(1≤T≤15), denoting the number of test cases.

In each test case, there are 3 integers l,r,k(1≤l≤r≤1012,r−l≤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
 
Source
 
思路:先将1000005以内的素数打表(素数筛法O(n)),然后将每个数拆成S=p1^a1*p2*^a2...*pn^an 的形式则 d(S)=(1+a1)*(1+a2)....*(1+an);
实现方法:用num[] 存下当前拆分后剩下的数字,用ans[i]存下第i个数字此时的已拆分的累乘结果,将素数从小到大扫一遍,对[l,r]每个数字进行拆分,逐一更新ans[i]的值
代码:
#include<bits/stdc++.h>

typedef long long ll;
using  namespace std;

;
const int inf=1e9;
;

ll l,r,k;
bool v[N];
ll ans[N];
ll num[N];
int pri[N];
int pcnt;
void init()
{
    pcnt = ;//素数筛
    ; i <= N; i++)
    {
        if(!v[i])
            pri[pcnt++] = i;
        ; j < pcnt && pri[j] <= N/i; j++)
        {
            v[i*pri[j]] = ;
            ) break;
        }
    }
}
void f()
{
    for(ll i=l;i<=r;i++)//初始化记录数组
        num[i-l]=i,ans[i-l]=;
}
int main()
{
    int t;
    init();
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld%lld",&l,&r,&k);
        f();
        ;i<pcnt&&pri[i]<=;i++){//限制质因子大小
            ll p=pri[i];
            ll d=l/p+(l%p>);
            ) d=;
            for(ll j=d*p;j<=r;j+=p){
                ll cnt=;
                ){
                    num[j-l]/=p;
                    cnt++;
                }
                ans[j-l]=(ans[j-l]%mod)*((+k*cnt)%mod)%mod;
            }
        }
        ll sum=;
        for(ll i=l;i<=r;i++){
            )//还存在一个大于1000000的因子,再乘上k+1
                sum=(sum+ans[i-l]*(k+))%mod;
            else
                sum=(sum+ans[i-l])%mod;
        }
        printf("%lld\n",sum);
    }

}
 

HDOJ 6069 素数筛法(数学)的更多相关文章

  1. hdu6069(简单数学+区间素数筛法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6069 题意: 给出 l, r, k.求:(lambda d(i^k))mod998244353,其中 ...

  2. 数学#素数筛法 HDU 4548&POJ 2689

    找素数本来是很简单的问题,但当数据变大时,用朴素思想来找素数想必是会超时的,所以用素数筛法. 素数筛法 打表伪代码(用prime数组保存区间内的所有素数): void isPrime() vis[]数 ...

  3. HDU 6069 Counting Divisors(区间素数筛法)

    题意:...就题面一句话 思路:比赛一看公式,就想到要用到约数个数定理 约数个数定理就是: 对于一个大于1正整数n可以分解质因数: 则n的正约数的个数就是 对于n^k其实就是每个因子的个数乘了一个K ...

  4. NowCoder猜想(素数筛法+位压缩)

    在期末被各科的大作业碾压快要窒息之际,百忙之中抽空上牛客网逛了逛,无意中发现一道好题,NowCoder猜想,题意很明显,就是个简单的素数筛法,但竟然超内存了,我晕(+﹏+)~  明明有 3 万多 k ...

  5. [原]素数筛法【Sieve Of Eratosthenes + Sieve Of Euler】

    拖了有段时间,今天来总结下两个常用的素数筛法: 1.sieve of Eratosthenes[埃氏筛法] 这是最简单朴素的素数筛法了,根据wikipedia,时间复杂度为 ,空间复杂度为O(n). ...

  6. POJ 3292 Semi-prime H-numbers (素数筛法变形)

    题意:题目比较容易混淆,要搞清楚一点,这里面所有的定义都是在4×k+1(k>=0)这个封闭的集合而言的,不要跟我们常用的自然数集混淆. 题目要求我们计算 H-semi-primes, H-sem ...

  7. 素数筛法—时间复杂度O(n)

    请你想出一个算法求出n以内(含n)的所有素数,要求算法的时间复杂度越小越好. 这里介绍一种算法——快速线性素数筛法(欧拉筛法),时间复杂度O(n). 诀窍在于:筛除合数时,保证每个合数只会被它的最小质 ...

  8. hdu-2136 Largest prime factor---巧用素数筛法

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2136 题目大意: 每个素数在素数表中都有一个序号,设1的序号为0,则2的序号为1,3的序号为2,5的 ...

  9. UVa 294 - Divisors 解题报告 c语言实现 素数筛法

    1.题目大意: 输入两个整数L.H其中($1≤L≤H≤10^9,H−L≤10000$),统计[L,H]区间上正约数最多的那个数P(如有多个,取最小值)以及P的正约数的个数D. 2.原理: 对于任意的一 ...

随机推荐

  1. java中得到图片的宽度 高度:

    java中得到图片的宽度 高度:BufferedImage srcImage = null;srcImage = ImageIO.read(new File(srcImagePath));int sr ...

  2. thinkphp中try catch的运用

    public function doedit_set(){ $info=$this->_post("info"); $id=$this->_post("id& ...

  3. [LeetCode] Friend Circles 朋友圈

    There are N students in a class. Some of them are friends, while some are not. Their friendship is t ...

  4. PHP xmapp 下面安装 Composer-Setup.exe

    1.打开PHP配置文件E:\xampp\php\php.ini确认以下模块已开启(移除前面的分号). extension=php_openssl.dll, (php.ini文档里面开启一次就OK了) ...

  5. tcp/ip通信传输流

    利用TCP/IP协议族进行网络通信时,会通过分层顺序与对方进行通信,发送端从应用层往下走,接收端则往应用层方向走. 我们用HTTP进行举例 客户端在应用层发出想要看到某个web页面的http请求.HT ...

  6. vijos1080题解

    题目: 对于一个递归函数w(a,b,c) 如果a<=0 or b<=0 or c<=0就返回值1. 如果a>20 or b>20 or c>20就返回w(20,20 ...

  7. JAVA金额按比例分摊,零头处理

    金额精确计算,必须使用BigDecimal; 平均分摊,分摊的零头,一般都是由数据"精度"和分摊系数决定的: 主要是如何对零头进行处理,保证尽可能的平均分配. 1.按户均摊 /** ...

  8. js 数组里求最大值和最小值

    // 数组里相邻两个数做比较 取满足条件的那个(以此类推) var arr = [1,3,4,5,6,7]; function Max(array){ var max = array[0]; for( ...

  9. Nginx 502 Bad Gateway

    今天安装完php环境以后,出现这个问题,排查步骤如下: ps -ef|grep php-fpm 发现没有安装php-fpm 然后直接安装php-fpm yum -y install php-fpm 安 ...

  10. POJ 3279 枚举(思维)

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10931   Accepted: 4029 Descrip ...