同余方程
贴个板子。
#include<cstdio> #include<cstring> #include<cstdlib> #include<map> #include<cmath> #include<algorithm> //#include<iostream> using namespace std; #define LL long long LL gcd(LL a,LL b) {return b?gcd(b,a%b):a;} void ext_gcd(LL a,LL b,LL& d,LL& x,LL& y) { if (!b) {d=a;x=1;y=0;} else {ext_gcd(b,a%b,d,y,x);y-=a/b*x;} } LL mul_mod(LL a,LL b,LL p) {return a*b%p;} LL pow_mod(LL a,LL b,LL p) { LL tmp=a,ans=1; while (b) {if (b&1) ans=ans*tmp%p;tmp=tmp*tmp%p;b>>=1;} return ans; } LL china(int n,int* a,int* m) { LL M=1,d,ans=0,y; for (int i=1;i<=n;i++) M*=m[i]; for (int i=1;i<=n;i++) { LL w=M/m[i],tmp; ext_gcd(m[i],w,d,tmp,y); ans=(ans+y*w*a[i])%M; } return (ans+M)%M; } LL inv(LL a,LL p) { LL d,x,y; ext_gcd(a,p,d,x,y); return d==1?(x+p)%p:-1; } LL log_mod(LL a,LL b,LL p) { LL m,v,e=1; m=(int)sqrt(p+0.5);v=inv(pow_mod(a,m,p),p); map<int,int> x;x[1]=0; for (int i=1;i<m;i++) {e=mul_mod(e,a,p);if (!x.count(e)) x[e]=i;} for (int i=0;i<m;i++) { if (x.count(b)) return i*m+x[b]; b=mul_mod(b,v,p); } return -1; } int main() { return 0; }