https://scut.online/p/11

T了好多次,还想用mutimap暴力分解每个数的质因数。后来记录每个数的最小质因子过了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; int n, m, MOD; const int SIZE = 1e6, SIZEP = 8e4; int p2[SIZE + 5], p5[SIZE + 5];
int p[SIZEP + 5], ptop;
int minp[SIZE + 5]; void init() {
for(int i = 2; i <= SIZE; i *= 2) {
for(int j = i; j <= SIZE; j += i)
p2[j] ++;
}
for(int i = 5; i <= SIZE; i *= 5) {
for(int j = i; j <= SIZE; j += i)
p5[j] ++;
}
minp[1] = 1;
for(int i = 2; i <= SIZE; i++) {
if(!minp[i]) {
p[++ptop] = i;
minp[i] = ptop;
}
for(int j = 1, t; j <= ptop && (t = i * p[j]) <= SIZE; j++) {
minp[t] = j;
if(i % p[j] == 0)
break;
}
}
//cout<<ptop<<endl;
} int cntp[SIZEP + 5]; void cnt(int n, int d) {
while(n != 1) {
cntp[minp[n]] += d;
n /= p[minp[n]];
}
} int qpow(ll x, int n) {
ll res = 1;
while(n) {
if(n & 1)
res = res * x % MOD;
x = x * x % MOD;
n >>= 1;
}
return res;
} int calc() {
memset(cntp, 0, sizeof(cntp));
m = min(n - m, m);
for(int i = 1; i <= m; ++i) {
cnt(n - i + 1, 1);
cnt(i, -1);
}
int min10 = min(cntp[1], cntp[3]);
cntp[1] -= min10, cntp[3] -= min10;
printf("%d ", min10);
ll ans = 1;
for(int i = 1; i <= ptop; ++i)
ans = ans * (qpow(p[i], cntp[i])) % MOD;
return ans % MOD;
} int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
init();
while(~scanf("%d%d%d", &n, &m, &MOD))
printf("%d\n", calc());
}

事实上,这个是个阶乘(DQ的方法),那么阶乘以内的各个质因子的贡献是可以算出来的,具体而言,2会贡献n/2个2,4再贡献n/4个2,8再贡献n/8个2。

用上面的方法,每个质因数会贡献一个log,一共是80000logn,而我的方法则是1000000logn,而且我对p2和p5的预处理并不是线性的(后面发现这两个白处理)。

二分memset,丧心病狂。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; int n, m, MOD; const int SIZE = 1e6, SIZEP = 8e4;
int p[SIZEP + 5], ptop;
int minp[SIZE + 5]; void init() {
minp[1] = 1;
for(int i = 2; i <= SIZE; i++) {
if(!minp[i]) {
p[++ptop] = i;
minp[i] = ptop;
}
for(int j = 1, t; j <= ptop && (t = i * p[j]) <= SIZE; j++) {
minp[t] = j;
if(i % p[j] == 0)
break;
}
}
//cout<<ptop<<endl;
} int maxptop;
int cntp[SIZEP + 5]; void cnt(int n, int d) {
/*while(n != 1) {
cntp[minp[n]] += d;
n /= p[minp[n]];
}*/
for(int i=1;i<=maxptop;++i){
int tmp=n;
while(tmp/=p[i]){
cntp[i]+=tmp*d;
}
}
} int qpow(ll x, int n) {
ll res = 1;
while(n) {
if(n & 1)
res = res * x % MOD;
x = x * x % MOD;
n >>= 1;
}
return res;
} int calc() {
maxptop=min(int(lower_bound(p+1,p+1+ptop,n)-p),ptop);
memset(cntp, 0, sizeof(cntp[0])*(maxptop+1));
/*m = min(n - m, m);
for(int i = 1; i <= m; ++i) {
cnt(n - i + 1, 1);
cnt(i, -1);
}*/
cnt(n,1);
cnt(m,-1);
cnt(n-m,-1);
int min10 = min(cntp[1], cntp[3]);
cntp[1] -= min10, cntp[3] -= min10;
printf("%d ", min10);
ll ans = 1;
for(int i = 1; i <= maxptop; ++i){
if(cntp[i])
ans = ans * (qpow(p[i], cntp[i])) % MOD;
}
return ans % MOD;
} int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
init();
while(~scanf("%d%d%d", &n, &m, &MOD))
printf("%d\n", calc());
}

SCUT - 11 - 被钦定的选手 - 质因数分解的更多相关文章

  1. 谷歌钦定的编程语言Kotlin大揭秘

    第一时间关注程序猿(媛)身边的故事 谷歌钦定的编程语言Kotlin大揭秘 语法+高级特性+实现原理:移动开发者升职加薪宝典! 谷歌作为世界级的科技公司巨头,强悍的技术研发与创新能力使其一直是业界的楷模 ...

  2. 质因数分解的rho以及miller-rabin

    一.前言 质因数分解,是一个在算法竞赛里老生常谈的经典问题.我们在解决许多问题的时候需要用到质因数分解来辅助运算,而且质因数分解牵扯到许许多多经典高效的算法,例如miller-rabin判断素数算法, ...

  3. 简单数论之整除&质因数分解&唯一分解定理

    [整除] 若a被b整除,即a是b的倍数,那么记作b|a("|"是整除符号),读作"b整除a"或"a能被b整除".b叫做a的约数(或因数),a ...

  4. 【期望dp 质因数分解】cf1139D. Steps to One

    有一种组合方向的考虑有没有dalao肯高抬啊? 题目大意 有一个初始为空的数组$a$,按照以下的流程进行操作: 在$1\cdots m$中等概率选出一个数$x$并添加到$a$的末尾 如果$a$中所有元 ...

  5. POJ1365:质因数分解

    Prime Land Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3590   Accepted: 1623 Descri ...

  6. codevs 3164 质因数分解

    3164 质因数分解  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description (多数据)给出t个数,求出它的质因子个 ...

  7. Pairs Forming LCM (LightOJ - 1236)【简单数论】【质因数分解】【算术基本定理】(未完成)

    Pairs Forming LCM (LightOJ - 1236)[简单数论][质因数分解][算术基本定理](未完成) 标签: 入门讲座题解 数论 题目描述 Find the result of t ...

  8. PAT 甲级 1059 Prime Factors (25 分) ((新学)快速质因数分解,注意1=1)

    1059 Prime Factors (25 分)   Given any positive integer N, you are supposed to find all of its prime ...

  9. 快速质因数分解及素性测试&ABC142D

    首先,这个整数的标准分解非常的显然易见对吧: 一般我们要把一个数分解成这个样子我们可以这样写: #include<cstdio> ],w[],k; void factorize(int n ...

随机推荐

  1. DUBBO原理、应用与面经总结

    研读dubbo源码已经有一段时间了,dubbo中有非常多优秀的设计模式和示例代码值得学习,但是dubbo的调用层级和方法链都较为繁杂,如果不对源码思路进行梳理则很容易忘却,因此总结一篇研读心得,从阅读 ...

  2. C# 实现二维数组的排序算法(代码)

    class Order { /// <summary> /// 对二维数组排序 /// </summary> /// <param name="values&q ...

  3. JavaScript算数运算符和一元运算符

    算数运算符 加法运算符(Addition): x + y 减法运算符(Subtraction): x - y 乘法运算符(Multiplication): x * y 除法运算符(Division): ...

  4. POJ 3691 DNA repair ( Trie图 && DP )

    题意 : 给出 n 个病毒串,最后再给出一个主串,问你最少改变主串中的多少个单词才能使得主串中不包含任何一个病毒串 分析 : 做多了AC自动机的题,就会发现这些题有些都是很套路的题目.在构建 Trie ...

  5. 【bzoj2064】【分裂】状态压缩表示合并子集

    (上不了p站我要死了,画师当然是wlop大大啦) 感觉这个做法还是挺难想的. 但还是总结一下思路吧.. "只可意会不可言传的状压dp"(乱说) Description 背景: 和久 ...

  6. Ubuntu 16.04下使用docker部署MySQL主从复制

    (以下docker相关的命令,需要在root用户环境下或通过sudo提升权限来进行操作.) 首先更新 软件源 https://mirrors.tuna.tsinghua.edu.cn/help/ubu ...

  7. spring boot 集成 mybatis 单元测试Dao层 控制台报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):

    最近帮同学做毕业程序,采用后端spring boot + mybatis + H2,将框架搭好进行各层的单元测试时,在dao层就出现了错,如图 于是在网上找各种资料,有的说是xml文件和接口没有一一对 ...

  8. C#中查找或结束程序域中的主、子进程

    有时候,我们的程序需要启动一些子进程,如嵌入的图形程序. 当启动一个进程后,获得这个进程信息Process,然后其内部在某个时刻启动了一个子进程,这个时候就涉及程序域和进程树的概念.当我们通过非正常操 ...

  9. (转)IDataGridViewEditingControl 接口 作用

    本文转载自:http://blog.csdn.net/zx13525079024/article/details/4814575 IDataGridViewEditingControl 接口 定义承载 ...

  10. python -加密(MD5)

    import hashlib def md5_passwd(str,salt ='aaaaa') str = str + salt m = hashlib.md5()#构造一个MD5对象 m.upda ...