POJ3243 Clever Y "Higher secondary congruence equation"

Source: Internet
Author: User

Topic Links:

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


Main topic:

known formula A^x mod C = B , and the value of a, C, B to solve the value of x.


Ideas:

The typical solution equation a^x = B (mod C), the direct template solves.


AC Code:

#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <    Cmath> #define LL __int64using namespace Std;const int maxn = 65535;struct hash{int A;    int b; int next;}    Hash[maxn*2];int flag[maxn+66];int top,idx;void ins (int a,int b) {int k = b & maxn;        if (flag[k]! = IDX) {Flag[k] = idx;        Hash[k].next =-1;        HASH[K].A = A;        hash[k].b = b;    Return        } while (Hash[k].next! =-1) {if (hash[k].b = = b) return;    K = Hash[k].next;    } hash[k].next = ++top;    Hash[top].next =-1;    HASH[TOP].A = A; hash[top].b = b;}    int Find (int b) {int k = b & maxn;    if (flag[k]! = idx) return-1;        while (k! =-1) {if (hash[k].b = = b) return hash[k].a;    K = Hash[k].next; } return-1;}    int GCD (int a,int b) {if (b = = 0) return A; Return GCD (b,a%b);}    int exgcd (int a,int b,int &x,int &y) {int temp,ret; if (!b) {x = 1;        y = 0;    return A;    } ret = EXGCD (b,a%b,x,y);    temp = x;    x = y;    y = temp-a/b*y; return ret;}    int inval (int a,int b,int n) {int x,y,e;    EXGCD (A,n,x,y);    E = (LL) x*b%n; Return e < 0? e + n:e;}    int Powmod (ll a,int b,int c) {ll ret = 1%c;    a%= C;        while (b) {if (b&1) ret = ret*a%c;        A = a*a%c;    b >>= 1; } return ret;}    int babystep (int a,int b,int C) {top = MAXN;    ++idx;    LL buf = 1%c,d = Buf,k;    int d = 0,temp,i;    for (i = 0; I <=; buf = buf*a%c,++i) {if (buf = = B) return i;        } while ((temp = GCD (a,c)) = 1) {if (B temp) return-1;        ++d;        C/= temp;        B/= temp;    D = d*a/temp%c;    } int M = (int) ceil (sqrt ((double) C));    for (buf = 1%c,i = 0; I <= M; buf = buf*a%c,++i) ins (i,buf); for (i = 0,k = Powmod (LL) a,m,c); I <= M; D = d*k%c,++i) {temp = inval ((int) d, b,c);        int W;    if (temp >= 0 && (w = Find (temp))! =-1) return I * M + W + D; } return-1;}    int main () {int a,b,c; while (~SCANF ("%d%d%d", &a,&c,&b) && (a| | b| |        c)) {B%= C;        int temp = Babystep (a,b,c);        if (Temp < 0) printf ("No solution\n");    else printf ("%d\n", temp); } return 0;}


POJ3243 Clever Y "Higher secondary congruence equation"

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.