Discrete logging hunnu10590 pku2417 fzu 1352 hit 1928 zoj 1898

Source: Internet
Author: User

From: http://hi.baidu.com/aekdycoin/blog/item/b317ca18bb24334942a9ad55.html

[Normal baby step giant step]

[Problem model]
Solution
0 <= x <C in a ^ x = B (mod C), where C is a prime number.

[Idea]
We can make an equivalent
X = I * m + J (0 <= I <m, 0 <= j <m) M = Ceil (SQRT (c ))
The purpose of such decomposition is nothing more than to convert:
(A ^ I) ^ M * A ^ J = B (mod C)

Then you can solve the problem by doing a little violent work:
(1) for I = 0-> M, insert Hash (I, a ^ I mod C)
(2) Enumeration I. For each enumerated I, make AA = (a ^ m) ^ I mod C
We have
AA * a ^ J = B (mod C)
Obviously, AA, B, and C are all known. Since C is a prime number, (AA, c) is unconditionally 1.
Therefore, the number of solutions to this model equation is unique (it can be solved by extending Euclidean or Euler's theorem)
For the obtained unique solution x, search in the hash table. If it is found, the return value isI * m + J
Note:Due to the small to large enumeration of I, the J in the hash table must be the smallest element x in a residual system (that is, the indicator)
Therefore, we can obtain the explain solution at this time.

If you want to obtain a solution of x> 0, you only need to judge in the preceding step that the return value is when I * m + j> 0.

So far, the above algorithms are not controversial, and the Code implemented by everyone is not much different. It can be seen that when C is a prime number, the problem of such discrete logarithm can become very easy to implement.

#include<stdio.h>#include<math.h>#include<stdlib.h>#define nmax 46341#define LL long longtypedef struct Num {int nnum;int ii;} Num;Num num[nmax];int x, y;int cmp(const void *a, const void *b) {Num *n = (Num *) a;Num *m = (Num *) b;if (n->nnum > m->nnum) {return 1;}return -1;}int extend_gcd(int a, int b) {if (b == 0) {x = 1, y = 0;return a;}int d = extend_gcd(b, a % b);int tx = x;x = y;y = tx - a / b * y;return d;}int find_num(int x, int n) {int mid, left, right;left = 0, right = n + 1;while (left <= right) {mid = (left + right) >> 1;if (num[mid].nnum == x) {return num[mid].ii;} else if (num[mid].nnum > x) {right = mid - 1;} else {left = mid + 1;}}return -1;}int main() {#ifndef ONLINE_JUDGEfreopen("t.txt", "r", stdin);freopen("out.txt", "w", stdout);#endifLL ptemp, te;int i, j, pte, p, b, n, bb, temp;while (scanf("%d %d %d", &p, &b, &n) != EOF) {pte = (int) ((sqrt(p * 1.0) + 0.5));for (i = 0, ptemp = 1; i <= pte; i++) {num[i].nnum = (int) (ptemp);num[i].ii = i;ptemp = ptemp * b % p;}bb = num[pte].nnum;qsort(num, pte + 1, sizeof(num[0]), cmp);for (i = 0, ptemp = 1; i <= pte; i++) {temp = (int) (ptemp);extend_gcd(temp, p);te = (int) (x);te = te * n;te = te % p + p;x = (int) (te % p);j = find_num(x, pte);if (j != -1) {printf("%d\n", pte * i + j);break;}ptemp = ptemp * bb % p;}if (i > pte) {printf("ERROR\n");}}return 0;}
Hunnu 10590 fzu 1352 PKU 2417 hit 1928 zoj 1898
 
/*
a^x=b (mod c) c is prime
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define LL long long
#define nmax 46345
typedef struct num {
int ii, value;
} num;
num Num[nmax];
int x, y;
int cmp(const void *a, const void *b) {
num n = *(num *) a;
num m = *(num *) b;
return n.value - m.value;
}
void extend_gcd(int a, int b) {
int xx;
if (b == 0) {
x = 1, y = 0;
return;
}
extend_gcd(b, a % b);
xx = x;
x = y, y = xx - a / b * y;
}
int bfindNum(int key, int n) {
int left, right, mid;
left = 0, right = n;
while (left <= right) {
mid = (left + right) >> 1;
if (Num[mid].value == key) {
return Num[mid].ii;
} else if (Num[mid].value > key) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return -1;
}
void solve(int c, int a, int b) {
int i, j, te, aa;
LL temp, xx;
te = (int) (sqrt(c * 1.0) + 0.5);
for (i = 0, temp = 1 % c; i <= te; i++) {
Num[i].ii = i;
Num[i].value = (int) (temp);
temp = temp * a % c;
}
aa = Num[te].value;
qsort(Num, te + 1, sizeof(Num[0]), cmp);
for (i = 0, temp = 1; i <= te; i++) {
extend_gcd((int) (temp), c);
xx = (LL) x;
xx = xx * b;
xx = xx % c + c;
x = (int) (xx % c);
j = bfindNum(x, te + 1);
if (j != -1) {
printf("%d\n", i * te + j);
return;
}
temp = temp * aa % c;
}
puts("no solution");
}
int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int p, b, n;
while (~scanf("%d %d %d", &p, &b, &n)) {
solve(p, b, n);
}
return 0;
}

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.