"POJ" 3243 Clever Y

Source: Internet
Author: User

http://poj.org/problem?id=3243

Test instructions: $a^y \equiv b \pmod{p}$ the smallest $y$. (0<=x, y, p<=10^9)

#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include < Iostream>typedef Long Long ll;using namespace Std;int gcd (int a, int b) {return b?gcd (b, a%b): A;} void Exgcd (ll A, ll B, LL &d, LL &x, ll &y) {if (!b) {d=a; x=1; y=0; return;} exgcd (b, a%b, D, y, x); y-=a/b *x; }int ni (int a, int b) {static ll x, Y, D;EXGCD (A, B, D, X, y); return (x+b)%b;} int Ipow (int a, int b, int c) {int x=1; for (; b; b>>=1, a= (LL) a*a%c) if (b&1) x= (ll) x*a%c; return x;} struct H {static const int md=3999997;bool VIS[MD]; int DT[MD], FOO[MD], S[MD], top;void CLR () {while (top) {int x=s[top- -]; Vis[x]=0, Dt[x]=-1; }}void Add (int a, int b) {int x=a%md;while (1) {if (!vis[x] | | dt[x]==a) {if (!vis[x]) s[++top]=x; vis[x]=1; dt[x]=a; foo[ X]=b; Break } ++x; if (X==MD) x=0; }}int find (int a) {int x=a%md;while (1) {if (!vis[x]) return-1, if (dt[x]==a) return foo[x]; ++x; if (X==MD) x=0;}} H;int A, B, Mo;bool SPJ () {if (b==1) puts ("0"); else if (!a &&!b) puts ("1"), else if (!b) puts ("No solution"); else return 0;return 1;} void work () {b%=mo;if (SPJ ()) return;for (int i=0, t=1; i<30; ++i, t= (LL) t*a%mo) if (t==b) {printf ("%d\n", I); return;} int a1=0, d=1, G;while ((G=GCD (A, MO))!=1) {if (b%g) {puts ("No solution"); return; ++a1, Mo/=g, B/=g, d= (LL) A/g*d%mo;} D=ni (d, MO); int m=sqrt (0.5+MO); h.clr (); for (int i=0, t= (LL) B*d%mo; i<m; ++i, t= (LL) t*a%mo) H.add (t, I); for (int i=0, wn= Ipow (A, M, MO), t=1; i<=m; ++i, t= (LL) t*wn%mo) {int x=h.find (t), if (x!=-1) {printf ("%d\n", a1+i*m-x); return;}} Puts ("No solution");} int main () {while (scanf ("%d%d%d", &a, &mo, &b) && (A|B|MO)) work (); return 0;}

  

The big step of expansion = =

Kneel on your knees: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4

Since $c$ are not necessarily prime numbers, they cannot be used with the original BSGS algorithm

And that's a solution, but it may not be the only thing.

But it is still easy to think of the enumeration $i$ solve this equation $a^{im} x \equiv b \pmod{p}$ then find $x$ is $a^t$, then the answer is $im+t$.

However, it is found that the size of the solution set is $ (A^{im}, p) $, it may be large = cannot withstand ....

Let's simplify the problem by turning the equation into an equivalent form:

We divide the equation by $d= (A, p) $ and get: $\frac{a}{d} a^m \equiv \frac{b}{d} \pmod{\frac{p}{d}}$. (Of course, if $b$ can't divide $d$, then the equation has no solution spicy = = will $b = xxx$ to be able to know spicy =)

has been $t$ times until $ (a, p_{t}) = 1$, so $d = A^t \frac{1}{\prod_{i=1}^{t}} d_{t}$, then obviously $ (D, p_{t}) = 1$ is not spicy = =

Then get the equivalent form of the original equation $a^h \equiv b_{t} d^{-1} \pmod{p_t}$, solve $h$ then the original question answer is $h+t$ spicy = =

So naked $bsgs$ spicy

But here to pay attention to Oh, there may be $y<t$ situation yo, because $t$ pine upper bound for $log_2 p$, we first enumerate the judgment on the line spicy ....

"POJ" 3243 Clever Y

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.