Factorial numbers are getting big very soon, you'll have to compute the number of divisors of such highly composite numbers.

Input

The first line contains an integer T, the number of test cases.
On the next T lines, you will be given two integers N and M.

Output

Output T lines, one for each test case, with the number of divisors of the factorial of N.
Since the answer can get very big, output it modulo M.

Example

Input:
3
2 1000
3 11
4 5
Output:
2
4
3

Constraints

0 < T < 5432
1 < N < 10^8
1 < M < 10^9

For N, M : uniform random input in the range.
One input file.

题意:  T组数据 给定N,M ; 求N的阶乘的因子个数 , 结果模M .

思路:   对于求某个数的因子个数 , 我们知道是唯一分解为素数 ,表示为素数的幂的乘积形式a1^p1*a2^p2... 然后结果就是所有(p1+1)*(p2+1)...的乘积.

但是这里的N的阶乘过大,  而且多组询问, T也过大 ,而且需要模不同的M (即可能不能离线求).

考虑到对于大于10000的素数  最多除一次 所以单独考虑 即把前面1229个素数和后面第1230个素数到最后一个素数分开考虑  :前面的暴力除,复杂度1229*logN

后面部分分块 把除数相同的合并. 然后快速幂, 复杂度logN*logN

所以最后的复杂度是O(T*1230*logN)

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn=1e8;
const int Mod=1e9+;
int p[],vis[maxn+],cnt;
void prime()
{
for(int i=;i<=maxn;i++){
if(!vis[i]) p[++cnt]=i;
for(int j=;j<=cnt&&(ll)i*p[j]<=maxn;j++){
vis[i*p[j]]=;
if(!(i%p[j])) break;
}
}
}
int qpow(int a,int x,int M){
int res=;
while(x){
if(x&) res=(ll)res*a%M;
a=(ll)a*a%M; x>>=;
} return res;
}
int main()
{
int T,i,j,x,M,pos,ans;
prime(); scanf("%d",&T);
for(i=;i<=T;i++){
ans=; scanf("%d%d",&x,&M);
for(j=;j<&&p[j]<=x;j++){ //暴力部分
int tmp=x,num=;
while(tmp){
num+=tmp/p[j]; tmp/=p[j];
}
ans=(ll)ans*(num+)%M;
}
pos=upper_bound(p+,p+cnt+,x)-p; pos--;
int fcy;
for(j=;j<=pos;j=fcy+){ //合并部分
fcy=upper_bound(p+,p+cnt+,x/(x/p[j]))-p; fcy--;
if(fcy>pos) fcy=pos;
ans=(ll)ans*qpow(x/p[j]+,fcy-j+,M)%M;
}
printf("%d\n",ans);
}
return ;
}

SPOJ:Divisors of factorial (hard) (唯一分解&分块优化)的更多相关文章

  1. bzoj 2301 [HAOI2011]Problem b(莫比乌斯反演+分块优化)

    题意:对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. 1≤n≤50000,1≤a≤b≤50000, ...

  2. HDU5780 gcd (BestCoder Round #85 E) 欧拉函数预处理——分块优化

    分析(官方题解): 一点感想: 首先上面那个等式成立,然后就是求枚举gcd算贡献就好了,枚举gcd当时赛场上写了一发O(nlogn)的反演,写完过了样例,想交发现结束了 吐槽自己手速慢,但是发了题解后 ...

  3. BZOJ 2301 Problem b(莫比乌斯反演+分块优化)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37166 题意:对于给出的n个询问,每次求有多少个数对(x,y),满 ...

  4. codevs 2606 约数和(分块优化数学公式 )

    题目背景 Smart最近沉迷于对约数的研究中. 题目描述 对于一个数X,函数f(X)表示X所有约数的和.例如:f(6)=1+2+3+6=12.对于一个X,Smart可以很快的算出f(X).现在的问题是 ...

  5. BZOJ 2301 Problem b(莫比乌斯反演+分块优化)

    Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...

  6. [bzoj2301]Problem b莫比乌斯反演+分块优化

    题意: $\sum\limits_{\begin{array}{*{20}{c}}{a < = x < = b}\\{c < = y < = d}\end{array}} {\ ...

  7. Codeforces 1129D - Isolation(分块优化 dp)

    Codeforces 题目传送门 & 洛谷题目传送门 又独立切了道 *2900( 首先考虑 \(dp\),\(dp_i\) 表示以 \(i\) 为结尾的划分的方式,那么显然有转移 \(dp_i ...

  8. BZOJ 1257 余数之和sum(分块优化)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46954 题意:f(n, k)=k mod 1 + k mod 2 ...

  9. Spoj 7001 Visible Lattice Points 莫比乌斯,分块

    题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193   Visible Lattice Points Time L ...

随机推荐

  1. CSS3 伪类选择器 nth-child() 的用法

    伪类选择器 nth-child() 在IE6-8和FF3.0-浏览器不支持,CSS3中nth-of-type(n)(比如nth-of-type(1))这个特殊的类选择符可以样式更加个性的标题和段落等, ...

  2. centos6安装概述

    1.1.选择安装类型:[Install or upgrade an existing system]安装或升级现有系统 1.2.介质校验:[Skip]跳过介质校验,校验时间较长 1.3.语言选择:[E ...

  3. Android L中的RecyclerView 、CardView 、Palette的使用

    <Material Design>提到,Android L版本中新增了RecyclerView.CardView .Palette.RecyclerView.CardView为用于显示复杂 ...

  4. Android Problem- android.content.res.Resources$NotFoundException: String resource ID #0xa

    在使用listview时出现错误android.content.res.Resources$NotFoundException: String resource ID #0xa 经查证是在调用Text ...

  5. 时间格式 2016-08-15T16:00:00.000Z

    我修改的时间是2016-08-16(转换成Date后默认为2016-08-16 00:00:00),而我得到的时间却是2016-08-15T16:00:00.000Z 联想到我们当前的时区是+8区   ...

  6. PHPMailer发送邮件乱码

    PHPMailer发送邮件乱码, $mail->CharSet="GB2312";$mail->Encoding = "base64"; 设成这样不 ...

  7. hdu 5358 First One 2015多校联合训练赛#6 枚举

    First One Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Tota ...

  8. 疯狂Java学习笔记(77)-----------凝视注意事项

    代码凝视,能够说是比代码本身更重要.这里有一些方法能够确保你写在代码中的凝视是友好的: 不要反复阅读者已经知道的内容 能明白说明代码是做什么的凝视对我们是没有帮助的. // If the color ...

  9. ViewPagerIndicator

    https://github.com/eltld/ViewPagerIndicator

  10. JavaScript 的 MVP 框架 Riot.js

    Riot.js 详细介绍 Riot.js是一个客户端模型-视图-呈现(MVP)框架并且它非常轻量级甚至小于1kb.尽管他的大小令人难以置信,所有它能构建的有如下:一个模板引擎,路由,甚至是库和一个严格 ...