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 ...
随机推荐
- ci框架——辅助函数
辅助函数:application/helper下面.命名要求为***_helper.php;这样在调用的时候直接$this->load->helper('***');若想给自定义的辅助函数 ...
- SGU 分类
http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...
- npm 安装vue-cli
TIP:win10下安装,使用管理员身份进行,否则会有权限限制. 1,安装完成node,node有自带的npm,可以直接在cmd中,找到nodeJs安装的路径下,进行命令行全局安装vue-cli.(n ...
- Idea其他设置
一.生成javadoc Tools->Gerenate JavaDoc 1. 选择是整个项目还是模块还是单个文件 2. 文档输出路径 3. Locale 选择地区,这个决定了文档的语言,中文就是 ...
- sqlplus登陆scott用户,以及退出连接
进入sqlplus界面 即登陆成功,PLsql也一样 退出连接:
- android 图片浏览器滑动切换图片
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...
- 数据库的DDL、DML和DCL的区别与理解
DML(data manipulation language): 它们是SELECT.UPDATE.INSERT.DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言 DDL ...
- jinjia2模板学习
http://docs.jinkan.org/docs/jinja2/templates.html#
- 【转】 C++ 简单的 Tcp 实现[socket] 客户端与客户端通信
// 服务器端代码 // Server.cpp : Defines the entry point for the console application.// #include "std ...
- 进程间通信之-信号signal--linux内核剖析(九)
信号及信号来源 什么是信号 信号是UNIX和Linux系统响应某些条件而产生的一个事件.接收到该信号的进程会对应地採取一些行动.通常信号是由一个错误产生的. 但它们还能够作为进程间通信或修改行为的一种 ...