Mod Tree
Problem Description
The picture indicates a tree, every node has 2 children.
The depth of the nodes whose color is blue is 3; the depth of the node whose color is pink is 0.
Now out problem is so easy, give you a tree that every nodes have K children, you are expected to calculate the minimize depth D so that the number of nodes whose depth is D equals to N after mod P.
Input
The input consists of several test cases.
Every cases have only three integers indicating K, P, N. (1<=K, P, N<=10^9)
Output
The minimize D.
If you can’t find such D, just output “Orz,I can’t find D!”
Sample Input
3 78992 453
4 1314520 65536
5 1234 67
Sample Output
Orz,I can’t find D!
8
20
Author
AekdyCoin
Source
HDU 1st “Old-Vegetable-Birds Cup” Programming Open Contest
题目类型:形如 A ^ X = B (mod C)的高次同余方程
算法分析:直接按照A ^ X = B (mod C)的格式求解即可,注意如果B >= C,则直接输出Orz,I can’t find D!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 110000 + 66; struct HashNode { long long data, id, next; }; HashNode Hash[maxn<<1]; bool flag[maxn<<1]; long long top; void Insert (long long a, long long b) { long long k = b & maxn; if (flag[k] == false) { flag[k] = true; Hash[k].next = -1; Hash[k].id = a; Hash[k].data = b; return ; } while (Hash[k].next != -1) { if (Hash[k].data == b) return ; k = Hash[k].next; } if (Hash[k].data == b) return ; Hash[k].next = ++top; Hash[top].next = -1; Hash[top].id = a; Hash[top].data = b; } long long Find (long long b) { long long k = b & maxn; if (flag[k] == false) return -1; while (k != -1) { if (Hash[k].data == b) return Hash[k].id; k = Hash[k].next; } return -1; } long long gcd (long long a, long long b) { return b ? gcd (b, a % b) : a; } long long gcd_ex (long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = gcd_ex (b, a % b, y, x); y = y - a / b * x; return d; } long long q_mul_mod (long long a, long long b, long long p) { long long ans = 1; while (b) { if (b & 1) { ans = ((ans % p) * (a % p)) % p; b--; } b = b >> 1; a = ((a % p) * (a % p)) % p; } return ans; } long long BabyStep_GiantStep (long long A, long long B, long long C) { top = maxn; B %= C; long long tmp = 1, i; for (i = 0; i <= 100; tmp = tmp * A % C, i++) if (tmp == B % C) return i; long long D = 1, cnt = 0; while ((tmp = gcd(A, C)) != 1) { if(B % tmp) return -1; C /= tmp; B /= tmp; D = D * A / tmp % C; cnt++; } long long M = (long long) ceil (sqrt (C + 0.0)); for (tmp = 1, i = 0; i <= M; tmp = tmp * A % C, i++) Insert (i, tmp); long long x, y, K = q_mul_mod (A, M, C); for (i = 0; i <= M; i++) { gcd_ex (D, C, x, y); // D * X = 1 ( mod C ) tmp = ((B * x) % C + C) % C; if((y = Find(tmp)) != -1) return i * M + y + cnt; D = D * K % C; } return -1; } int main() { // ifstream cin ("aaa.txt"); long long A, B, C; while(cin >> A >> C >> B) { if (B >= C) { cout << "Orz,I can’t find D!" << endl; continue; } memset (flag, 0, sizeof(flag)); long long ans = BabyStep_GiantStep (A, B, C); if (ans == -1) cout << "Orz,I can’t find D!" << endl; else cout << ans << endl; } return 0; } |
- « 上一篇:hdu2686
- hdu2888:下一篇 »