Mod Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7143 Accepted Submission(s): 1750
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
题意: 一个树上,每个节点有k个孩子,给你一个p和N
,且不保证p为素数
分析: 这是一个板子题,如果p为素数的时候,是BSGS,这里要用到扩展BSGS,原理看下这里吧
就当做个板子吧
参考代码
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
#include <cmath>
#include <unordered_map>
using namespace std;
typedef long long ll;
unordered_map<ll,ll> ha;
ll MOD;
ll gcd(ll a, ll b) {
return !b ? a : gcd(b, a % b);
}
#define mod(x) ((x) % MOD)
ll qpow(ll a,ll b) {
ll res = 1;
while (b) {
if (b & 1) {
res = mod(res * a);
}
a = mod(a * a);
b >>= 1;
}
return res;
}
ll ebsgs(ll a, ll b, ll p) {
if (b == 1) return 0;
ll t,d = 1,k = 0;
while ((t = gcd(a, p)) != 1) {
if (b % t) return -1;
k++,b /= t,p /= t, d = (d*(a / t)) % p;
if (b == d) return k;
}
ha.clear();
ll m = ceil(sqrt(p+0.5));
ll a_m = qpow(a,m);
ll mul = b;
for (ll j = 1; j <= m; j++) {
mul = (mul * a) % p;
ha[mul] = j;
}
for (ll i = 1; i <= m; i++) {
d = (d * a_m) % p;
if (ha[d]) return i * m - ha[d] + k;
}
return -1;
}
int main() {
ll a,b;
while (~scanf("%lld%lld%lld", &a, &MOD, &b)) {
if (b >= MOD) {
puts("Orz,I can’t find D!");continue;
}
if (ebsgs(mod(a), mod(b),MOD) == -1) puts("Orz,I can’t find D!");
else printf("%lld\n", ebsgs(mod(a), mod(b),MOD));
}
return 0;
}