SCUT - 485 - 质因数计数 - 原根
给定a和n,求有多少个质数p,满足n是使得a^n=1 mod p成立的最小正整数。
翻译:求有多少个质数p,使得a模p的阶delta_m(a)是n
先验证 a^n=1 mod p 成立
那么假如还有更小的m使得 a^m=1 mod p 成立,则这个p不合要求
由阶的性质有delta_m(a)|n,故只需要检查n的所有因子就可以了。
但其实不需要检查所有因子,只需要检查n的所有质因子。(从板子上面可以看出来,但是为什么)
即 a^(p_i) = 1 mod p 是否成立,假如恒不成立,则n是a模p的阶,其中p_i是n的每种质因子。
证明如下:很显然的,原本我们要检查n的所有因子才能确定阶,但是有一个更好的办法。
假如还有更小的m使得 a^m=1 mod p 成立,那么m的倍数km也一定满足 a^(km)=1 mod p 成立,那么从n中只去除一个质因子p_i,假如这个t=(n/p_i)有 a^t=1 mod p ,则可能存在更小的m使得 a^m=1 mod p 成立。否则假如 a^t != 1 mod p ,则t的所有因子也都不需要检查了。
这样就只需要检查log次。
1e9都直接暴力,快速幂还有个logn,真的服了没话讲。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int q, n;
const int MAXN = 1e7 + 5;
int p[MAXN], ptop;
int minp[MAXN];
bool np[MAXN];
void sieve(int n) {
np[1] = 1;
for(int i = 2; i <= n; i++) {
if(!np[i]) {
p[++ptop] = i;
minp[i] = i;
}
for(int j = 1, t; j <= ptop && (t = i * p[j]) <= n; j++) {
minp[t] = p[j];
np[t] = 1;
if(i % p[j] == 0)
break;
}
}
}
int qpow(ll x, int n, int p) {
ll res = 1;
while(n) {
if(n & 1)
res = res * x % p;
x = x * x % p;
n >>= 1;
}
return res;
}
int ans[MAXN], atop;
int fac[60], ftop;
void GetFactor(int n) {
ftop = 0;
while(n != 1) {
int cp = minp[n];
fac[++ftop] = cp;
while(n % cp == 0)
n /= cp;
}
}
int check(int n, int P) {
if(qpow(q, n, P) != 1)
return 0;
for(int i = 1; i <= ftop; ++i) {
int t = n / fac[i];
if(qpow(q, t, P) == 1)
return 0;
}
return 1;
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
sieve(MAXN);
while(~scanf("%d%d", &q, &n)) {
GetFactor(n);
atop = 0;
for(int i = 1; i <= ptop; ++i) {
if(check(n, p[i]))
ans[++atop] = p[i];
}
printf("%d\n", atop);
for(int i = 1; i <= atop; ++i)
printf("%d%c", ans[i], " \n"[i == atop]);
}
return 0;
}
直接上BSGS的话,假如这样的p有10000个就比较麻烦。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int q, n;
const int MAXN = 1e7 + 5;
int p[MAXN], ptop;
bool np[MAXN];
void sieve(int n) {
np[1] = 1;
for(int i = 2; i <= n; i++) {
if(!np[i])
p[++ptop] = i;
for(int j = 1, t; j <= ptop && (t = i * p[j]) <= n; j++) {
np[t] = 1;
if(i % p[j] == 0)
break;
}
}
}
int qpow(ll x, int n, int p) {
ll res = 1;
while(n) {
if(n & 1)
res = res * x % p;
x = x * x % p;
n >>= 1;
}
return res;
}
unordered_map<int,int> M;
int bsgs(int a,int b,int n,int k=1,int t=0){
/*if(b==1)
return 0;*/
M.clear();
int m=ceil(sqrt(n));
ll s=b;
for(int i=0;i<m;++i,s=s*a%n){
M[s]=i;
}
s=k;
k=qpow(a,m,n);
for(ll i=1;i<=m;++i){
s=s*k%n;
if(M.count(s))
return i*m-M[s]+t;
}
return -1;
}
int ans[MAXN], atop;
int check(int n, int p) {
if(qpow(q, n, p) != 1)
return 0;
int res=bsgs(q,1,p);
if(res==-1||res==n)
return 1;
return 0;
}
int main() {
#ifdef Yinku
freopen("Yinku.in", "r", stdin);
#endif // Yinku
sieve(MAXN);
while(~scanf("%d%d", &q, &n)) {
atop = 0;
for(int i = 1; i <= ptop; ++i) {
if(check(n, p[i]))
ans[++atop] = p[i];
}
printf("%d\n", atop);
for(int i = 1; i <= atop; ++i)
printf("%d%c", ans[i], " \n"[i == atop]);
}
return 0;
}
SCUT - 485 - 质因数计数 - 原根的更多相关文章
- SCUT - 11 - 被钦定的选手 - 质因数分解
https://scut.online/p/11 T了好多次,还想用mutimap暴力分解每个数的质因数.后来记录每个数的最小质因子过了. #include <bits/stdc++.h> ...
- SCUT - 243 - 宝华复习 - 二分 - 桶计数
https://scut.online/p/243 这道题唯一难点在于如何快速确定m合法.可以统计滑动窗口中已有元素的数量. #include<bits/stdc++.h> using n ...
- 数论入门2——gcd,lcm,exGCD,欧拉定理,乘法逆元,(ex)CRT,(ex)BSGS,(ex)Lucas,原根,Miller-Rabin,Pollard-Rho
数论入门2 另一种类型的数论... GCD,LCM 定义\(gcd(a,b)\)为a和b的最大公约数,\(lcm(a,b)\)为a和b的最小公倍数,则有: 将a和b分解质因数为\(a=p1^{a1}p ...
- 【BZOJ】【1211】【HNOI2004】树的计数
Prufer序列+组合数学 嗯哼~给定每个点的度数!求树的种数!那么很自然的就想到是用prufer序列啦~(不知道prufer序列的……自己再找找资料吧,这里就不放了,可以去做一下BZOJ1005明明 ...
- 【poj1284-Primitive Roots】欧拉函数-奇素数的原根个数
http://poj.org/problem?id=1284 题意:给定一个奇素数p,求p的原根个数. 原根: { (xi mod p) | 1 <= i <= p-1 } is equa ...
- 原根求解算法 && NTT算法
原根求解算法: 获取一个数\(N\)的原根\(root\)的算法 #include<bits/stdc++.h> #define ll long long #define IL inlin ...
- 简单数论之整除&质因数分解&唯一分解定理
[整除] 若a被b整除,即a是b的倍数,那么记作b|a("|"是整除符号),读作"b整除a"或"a能被b整除".b叫做a的约数(或因数),a ...
- [总结]数论和组合计数类数学相关(定理&证明&板子)
0 写在前面 0.0 前言 由于我太菜了,导致一些东西一学就忘,特开此文来记录下最让我头痛的数学相关问题. 一些引用的文字都注释了原文链接,若侵犯了您的权益,敬请告知:若文章中出现错误,也烦请告知. ...
- BZOJ2219数论之神——BSGS+中国剩余定理+原根与指标+欧拉定理+exgcd
题目描述 在ACM_DIY群中,有一位叫做“傻崽”的同学由于在数论方面造诣很高,被称为数轮之神!对于任何数论问题,他都能瞬间秒杀!一天他在群里面问了一个神题: 对于给定的3个非负整数 A,B,K 求出 ...
随机推荐
- 手动安装jar包到maven仓库
1.手动安装jar包到maven仓库 本地没有下载安装maven,但是eclipse已经集成的maven. 选中任何一个maven项目,右键/Run as/maven build... 在Goals输 ...
- Android图片缩放 指定尺寸
//使用Bitmap加Matrix来缩放 public static Drawable resizeImage(Bitmap bitmap, int w, int h) { ...
- Android中StatFs获取系统/sdcard存储(剩余空间)大小
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- ES6注
1.Promise构造函数 //resolve(成功),reject(失败)两个参数 function runAsync(){ var p = new Promise(function(resolve ...
- Jupiter Code Review Reference -- Jupiter代码审查工具使用参考
Jupiter Code Review Reference -- Jupiter代码审查工具使用参考 (修改版) 原创 2010年07月06日 10:43:00 标签: 审查 / reference ...
- leetcode-mid-math-172. Factorial Trailing Zeroes-NO-????
mycode 问题:为甚在小于200的时候,答案ok,大于等于200的时候,就少一个1??? class Solution(object): def trailingZeroes(self, n): ...
- Docker安装CentOS7
1. 拉取镜像 docker pull centos:centos7 2. 启动镜像创建容器 docker run -d -p 36622:22 -p 36680:80 --name centos7- ...
- lua源码学习篇一:环境部署
研究生即将毕业,答辩完成后,这几天有些时间.开始写一些自己的东西,记录自己的学习历程. --前言 本着学习和交流的原则,本文的内容仅供参考,而不是权威版本,如有任何问题,欢迎指出. --声明 跨专业考 ...
- Octavia 创建 loadbalancer 的实现与分析
目录 文章目录 目录 从 Octavia API 看起 Octavia Controller Worker database_tasks.MapLoadbalancerToAmphora comput ...
- 看看 Delphi XE2 为 VCL 提供的 14 种样式
看看 Delphi XE2 为 VCL 提供的 14 种样式 其实只提供了 13 个 vsf 样式文件, 还有默认的 Windows 样式, 共 14 种. 在空白窗体上添加 ListBox1 等控件 ...