HDU 5393 【数论】

来源:互联网 发布:北京军工软件开发 编辑:程序博客网 时间:2024/06/07 09:23

最近总被虐…被虐…虐…

%%%fsf大爷,太强辣
欢迎神犇打脸(求轻喷

题目大意:给出 x0,每次操作将 x 变成 (kx + b) mod p,求最少多少次操作能回到 x0

…首先 k = 0,1 要特判 …

n 次操作后的值就是 n1i=0kib+knx
所以就能得到

i=0n1kib+knxx(modp)kn1k1(b+(k1)x)=0(modp)

两边可以约去 gcd((k1)x+b,p),现在令 p /= gcd
现在就是求
kn1 (modp(k1))

这里 gcd(k,p)==1,不然无解
但是右边不是质数,所以要分解质因数然后 CRT 合并(其实这步是多余的。。。我又智障了嘛。。。TAT
对于
an1 (mod pk)

设最小的 n 为 x,那么有 x|φ(pk) 且对于所有 n 满足 x|n 都满足上述表达式
所以对于 n ,若满足上述表达式且 i|nni 也能满足上述表达式,那么 n 就可以除掉 i
然而 φ 的因数都小于等于 p,就可以暴力分解了(雾

为什么感觉不太科学…

#include<iostream>#include<algorithm>#include<cstdlib>#include<cstdio>#include<string>#include<cstring>#include<ctime>#include<cmath>#define INF 2000000020#define LL long longusing namespace std;LL T,k,b,x,p,tot,ans;struct node{int p,k;}A[100];LL gcd(LL a,LL b){    return b ? gcd(b,a % b) : a;}LL lcm(LL a,LL b){    return a / gcd(a,b) * b;}LL ksm(LL a,LL b,LL p){    LL ret = 1;    for (;b;b >>= 1,a = a * a % p)        if (b & 1) ret = ret * a % p;    return ret;}void divid(int x){    for (int i = 2;i * i <= x;i ++)        if (x % i == 0)        {            A[++ tot] = (node){i,1},x /= i;            for (;x % i == 0;x /= i,A[tot].k ++);        }    if (x ^ 1) A[++ tot] = (node){x,1};}bool cmp(const node &a,const node &b){    return a.p < b.p;}LL cal(int tt){    LL P = 1,phi,p = A[tt].p;    for (int i = 1;i <= A[tt].k;i ++) P *= p;    phi = P / p * (p - 1);    while (phi % p == 0 && ksm(k,phi / p,P) == 1) phi /= p;    p --;    for (int i = 2;i * i <= p;i ++)    {        for (;phi % i == 0 && ksm(k,phi / i,P) == 1;phi /= i);        for (;phi % (p/i) == 0 && ksm(k,phi / (p/i),P) == 1;phi /= (p/i));    }    if (phi % p == 0 && ksm(k,phi / p,P) == 1) phi /= p;    return phi;}int main(){    cin >> T;    while (T --)    {        cin >> k >> b >> x >> p;        if (!k) {printf("%d\n",b == x ? 1 : -1);continue;}        if (k == 1) {printf("%d\n",b ? p / gcd(p,b): 1);continue;}        p /= gcd((k - 1) * x + b,p);        if (p == 1) {puts("1");continue;}        if (gcd(p,k) ^ 1) {puts("-1");continue;}        tot = 0,divid(p),divid(k - 1);        sort(A + 1,A + tot + 1,cmp);        for (int i = 2;i <= tot;i ++)            if (A[i].p == A[i - 1].p) A[i].k += A[i - 1].k,A[i - 1].p = INF;         sort(A + 1,A + tot + 1,cmp);        for (;A[tot].p == INF;tot --);        ans = 1;        for (int i = 1;i <= tot;i ++)            ans = lcm(ans,cal(i));        cout << ans << endl;    }    return 0;}
0 0
原创粉丝点击