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. 【hackerrank】Weather Observation Station 18

    题目如下: Consider  and  to be two points on a 2D plane. happens to equal the minimum value in Northern ...

  2. Java面试之基础篇(1)

    1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致. 2.Java有 ...

  3. Bugku 杂项 宽带信息泄露

    宽带信息泄露 flag是宽带用户名 下载文件后用RouterPassView打开,搜索username即可

  4. BZOJ 3043: IncDec Sequence 差分 + 思维

    Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...

  5. nginx 静态资源WEB服务

    1.静态资源类型 非服务器动态运行生成的文件 类型种类 浏览器端渲染     HTML.CSS.JS 图片            JPEG.GIF.PNG 视频            FLV.MPEG ...

  6. Miniprofiler在目中使用报 mini-profiler-resources/includes.js 404错误

    原因,没有配置webconfig <system.webServer> <modules> <remove name="FormsAuthentication& ...

  7. va_list原理及用法

    最后更新:2017-02-22 这是一篇很早很早的博客文章,虽然很基础,但是毕竟曾经历程,因此也保存下来 1. 概念 va_list 是在C语言中定义的宏,指在解决 变参问题是指参数的个数不定,可以是 ...

  8. percona-toolkit 工具介绍

    percona-toolkit 工具介绍 percona-toolkit 是一组高级命令行工具的集合,用来执行各种通过手工执行非常复杂和麻烦的mysql和系统任务.这些任务包括: 检查master和s ...

  9. .bash_profile vs .bashrc

    w http://www.joshstaiger.org/archives/2005/07/bash_profile_vs.html

  10. DRF 组件

    DRF组件中的认证  授权  频率限制   分页  注册器  url控件