SPOJ:Divisors of factorial (hard) (唯一分解&分块优化)
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) (唯一分解&分块优化)的更多相关文章
- 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, ...
- HDU5780 gcd (BestCoder Round #85 E) 欧拉函数预处理——分块优化
分析(官方题解): 一点感想: 首先上面那个等式成立,然后就是求枚举gcd算贡献就好了,枚举gcd当时赛场上写了一发O(nlogn)的反演,写完过了样例,想交发现结束了 吐槽自己手速慢,但是发了题解后 ...
- BZOJ 2301 Problem b(莫比乌斯反演+分块优化)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37166 题意:对于给出的n个询问,每次求有多少个数对(x,y),满 ...
- codevs 2606 约数和(分块优化数学公式 )
题目背景 Smart最近沉迷于对约数的研究中. 题目描述 对于一个数X,函数f(X)表示X所有约数的和.例如:f(6)=1+2+3+6=12.对于一个X,Smart可以很快的算出f(X).现在的问题是 ...
- BZOJ 2301 Problem b(莫比乌斯反演+分块优化)
Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input 第一行一个整数 ...
- [bzoj2301]Problem b莫比乌斯反演+分块优化
题意: $\sum\limits_{\begin{array}{*{20}{c}}{a < = x < = b}\\{c < = y < = d}\end{array}} {\ ...
- Codeforces 1129D - Isolation(分块优化 dp)
Codeforces 题目传送门 & 洛谷题目传送门 又独立切了道 *2900( 首先考虑 \(dp\),\(dp_i\) 表示以 \(i\) 为结尾的划分的方式,那么显然有转移 \(dp_i ...
- BZOJ 1257 余数之和sum(分块优化)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=46954 题意:f(n, k)=k mod 1 + k mod 2 ...
- Spoj 7001 Visible Lattice Points 莫比乌斯,分块
题目:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37193 Visible Lattice Points Time L ...
随机推荐
- Day 13 Python 一之helloworld
直接肝程序吧! """ # 作业六:用户登录测试(三次机会) count = 1 while count <= 3: user = input('请输入用户名: ' ...
- android控件-images
1.imageButton 图片按钮 <ImageButton android:id="@+id/imageButton" android:layout_width=&quo ...
- RabbitMQ核心组件及应用场景
一.适用场景 1.解耦 2.最终一致性 3.广播 4.错峰与流控(秒杀业务用于流量削峰场景) 秒杀场景 二.核心组件,关键点(交换器.队列.绑定) AMPQ消息路由必要三部分:交换器.队列.绑定. J ...
- Windows下maven安装配置(包括本地化仓库配置)
一.下载maven maven官网:http://maven.apache.org/ 下载下来也就是一个压缩文件,解压.我下载的是3.5.2版本,解压之后如下: 路径为 :D:\Program Fil ...
- Java中Arrays类与Math类
Arrays(数组工具类) Java中已经封装好的类,提供大量静态方法供用户对数组的使用. 导包:import java.util.Arrays 1.Arrays.toString(数组) //返回值 ...
- php 笔记 汇总 学习
php命令行:通过命令行进入到当前要被执行的php文件路径,然后输入php环境可执行路径(后面包含php.exe),然后输入要被执行的php文件,比如runData.php即可. php框架:yaf. ...
- 移动端日历选择控件(支持Zepto和JQuery)
移动端日历选择控件(支持Zepto和JQuery) <!DOCTYPE html> <html> <head> <meta charset="utf ...
- 制作ubuntu U盘安装盘
sudo dd if=ubuntu.iso of=/dev/sdb2 sudo syslinux /dev/sdb1
- 利用Loader来动态载入不同的QML文件来改变UI
在这篇文章中.我们将介绍怎样使用Loader来载入不同的QML文件来实现动态的UI.在之前的文章"怎样使用Loader来动态载入一个基于item的Component"中,我们已经介 ...
- 【转载】NULL,"",String.Empty三者在C#中的区别
(1)NULLnull 关键字是表示不引用任何对象的空引用的文字值.null 是引用类型变量的默认值.那么也只有引用型的变量可以为NULL,如果int i=null,的话,是不可以的,因为Int是值类 ...