group 求阶(BSGS 欧拉定理)
来源:互联网 发布:淘宝直通车有用么 编辑:程序博客网 时间:2024/06/07 01:31
group
10.20
30%
直接暴力算数列,直到遇到一个出现过的数,这样后出现的数前面一定都出现过了,所以直接看当前出现的数有多少个不同即可。
复杂度: O(T mod)
100%
因为gcd(a, mod) = 1,所以本质上那个数列是一个”环”,即是某一段数一直重复。
因为a^(phi(n)) = 1(mod n),所以该最小循环节一定是phi(n)的约数(我们证明过的)。
所以我们可以直接枚举所有phi(n)的约数,然后暴力check是否有a^d=1(mod)。
复杂度:O(T sqrt(mod) log(m))
本人的BSGS解法,ans = BSGS(a, inv[a], mod) + 1。(inv是逆元)。
#include <cstdio>#include <cmath>#include <cstring>#include <iostream>#define LL long longusing namespace std;const LL S = 100007;struct Hash_{ LL head[S], dest[S][2], last[S], etot; void init(){ memset(head, 0, sizeof(head)); etot = 0; } void add(LL a, LL b){ LL key = a % S; for(LL t=head[key]; t; t=last[t]) if(dest[t][0] == a) return; etot++; last[etot] = head[key]; dest[etot][0] = a; dest[etot][1] = b; head[key] = etot; } LL find(LL a){ LL key = a % S; for(LL t=head[key]; t; t=last[t]) if(dest[t][0] == a) return dest[t][1]; return -1; }}hs;void exgcd(LL a, LL b, LL &d, LL &x, LL &y) { if(b == 0){ d = a; x = 1; y = 0; } else{ exgcd(b, a%b, d, y, x); y -= a / b * x; }}LL inverse(LL a, LL m){ LL x, y, d; exgcd(a, m, d, x, y); return (x % m + m) % m;}LL BSGS(LL a, LL b, LL m){//a^x = b(mod m) hs.init(); LL sz = (LL)ceil( sqrt(b) + 1 ); LL cur = 1; for(register LL i=0; i<sz; i++,cur=(1LL*cur*a)%m){ if(cur == b) return i; hs.add(cur, i); } LL base = inverse(cur, m); cur = 1LL * base * b % m; for(register LL i=sz; i<=m-1; i+=sz,cur=(1LL*cur*base)%m) { LL j = hs.find(cur); if(j != -1 && (i+j)) return i + j; } return -1;}int main(){ freopen("group.in", "r", stdin); freopen("group.out", "w", stdout); LL T, a, p; scanf("%I64d", &T); while( T-- ){ scanf("%I64d%I64d", &a, &p); LL ans = BSGS(a, inverse(a, p), p); if(p == 1) ans = 0;//容易忽略的特判 printf("%I64d\n", ans+1); } return 0;}
阅读全文
0 0
- group 求阶(BSGS 欧拉定理)
- fzoj1759(欧拉定理)
- 费马小定理&&欧拉定理
- 威尔逊定理,费马小定理,欧拉定理
- 基础数论算法(7) 欧拉函数与BSGS算法
- poj3358数论(欧拉定理)
- POJ 3358 (欧拉定理)
- Mathematically Hard (欧拉定理)
- LA 3263 (欧拉定理)
- 欧拉定理
- 欧拉定理证明
- 欧拉定理
- 【数论】欧拉定理
- 欧拉定理
- 欧拉定理
- 欧拉定理
- 欧拉定理
- 欧拉定理
- HDU-1999
- Spring3 MVC Tutorial helloworld
- 1、找到数组里两个数字之和为某个值
- Effective C++ tips day1
- Hadoop笔记
- group 求阶(BSGS 欧拉定理)
- 虹膜识别 Iris_Osiris_v4.1(支持OpenCV3.3/2.4.13;win/linux)
- 路由和侧边栏结合使用
- 使用 sudo nautilus 进入ubuntu 文件管理器。可以随意复制,删除,粘贴,无权限限制
- 远程连接ubuntu mysql出现2003错误 cant connect to mysql
- 隐马尔可夫模型hidden Markov model
- 单例模式(Singleton)的创建
- Codeforces 279B Books(dp大法好)
- 2017不一样的大数据