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. Spring Boot整合tk.mybatis及pageHelper分页插件及mybatis逆向工程

    Spring Boot整合druid数据源 1)引入依赖 <dependency> <groupId>com.alibaba</groupId> <artif ...

  2. 043:Django使用原生SQL语句操作数据库

    Django使用原生SQL语句操作数据库 Django配置连接数据库: 在操作数据库之前,首先先要连接数据库.这里我们以配置 MySQL 为例来讲解. Django 连接数据库,不需要单独的创建一个连 ...

  3. 前端node面试题之---对比JS和NodeJS的区别

    区别: 1.JS运行在浏览器端,用于用户的交互效果,NodeJS运行在服务器端,用于服务器的操作,例如,Web服务器创建,数据库的操作,文件的操作等 2.JS运行在浏览器端,存在多个JS解释器,存在兼 ...

  4. Es6 之 const关键字

    https://blog.csdn.net/jin_doudouer/article/details/80493649 es6中新增了一个const.就是用来定义一个常量的.以前其实一直没有把这个放在 ...

  5. Linux内核设计与实现 总结笔记(第十六章)页高速缓存和页回写

    页高速缓存是Linux内核实现磁盘缓存.磁盘告诉缓存重要源自:第一,访问磁盘的速度要远远低于访问内存. 第二,数据一旦被访问,就很有可能在短期内再次被访问到.这种短时期内集中访问同一片数据的原理称作临 ...

  6. 多重背包的二进制优化——DP

    #include<cstdio> #include<cstring> #include<algorithm> #define LL long long using ...

  7. Kohana Minion cli 学习

    1.E:\html\tproject\framebota\platform\bootstrap.php Kohana::modules(array( 'auth' => MODPATH.'aut ...

  8. Qt中图元对象的多重集成

    在继承自定义QGraphicsItem图元对象时,有时需要用到信号/槽机制,由于QGraphicsItem非QObject的子类 所以需要多重继承QObject,有一点需要特别注意:就是继承的顺序,一 ...

  9. 阿里云服务器+ubantu+nodejs 服务器基本配置流程

    所有步骤在MAC 电脑环境下操作 一.配置环境 1.连接到远程服务器 1.购买阿里云ECS服务器,我选用的 ubantu 14.0.4 (64位),购买的时候输入的密码记录下来,没有设置的话可以随后在 ...

  10. React-Native 之 GD (七)下拉刷新 及 上拉加载更多

    1.下拉刷新  使用第三方插件 下载插件: $ npm install react-native-pull@latest --save 引入: import {PullList} from 'reac ...