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 long
using 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;
}

文章探讨了如何利用伯努利-斯蒂尔吉斯平方根算法(BSGS)和欧拉定理来解决阶求解问题。通过分析gcd(a, mod) = 1的情况,指出数列呈环状重复,循环节长度为phi(n)的约数。作者提出了枚举phi(n)约数并检查a^d是否等于1(mod m)的方法,以及个人的BSGS解决方案,即ans = BSGS(a, inv[a], mod) + 1,其中inv表示逆元。"
128260347,15339039,Qt实现ToolButton点击弹出新窗口,"['Qt开发', 'GUI编程', 'C++', '用户界面']
&spm=1001.2101.3001.5002&articleId=78298581&d=1&t=3&u=3e3ad2a9a43b4e56a59f4f95af178d67)
2533

被折叠的 条评论
为什么被折叠?



