Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=2815
Main topic:
There is a tree, each node has a k son, then the question is: whether to calculate the minimum depth of the tree D, so that the depth
The number of nodes to p modulo the result is n?
Ideas:
Changing the meaning of the title becomes the Understanding k^i = N (mod P), the typical a^i = B (mod C) problem, the range of this question B
Obviously between [0,c-1], if not in this interval, the equation obviously has no solution.
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)) {if (B > C) {printf ("Orz,i can ' t find d!\n"); Continue } B%= C; int temp = Babystep (a,b,c); if (Temp < 0) printf ("Orz,i can ' t find d!\n"); else printf ("%d\n", temp); } return 0;}
HDU2815 Mod Tree "high-secondary congruence equation"