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.
Every cases have only three integers indicating K, P, N. (1<=K, P, N<=10^9)
If you can’t find such D, just output “Orz,I can’t find D!”
3 78992 453 4 1314520 65536 5 1234 67
Orz,I can’t find D! 8 20
求离散对数的模板题
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
int K,P,N;
LL mod_pow(LL x,LL p,LL mod)
{
LL ans = 1;
while(p > 0)
{
if(p & 1)
ans = (x * ans)%mod;
x = (x*x) % mod;
p >>= 1;
}
return (ans + mod )% mod;
}
LL exgcd(LL a, LL b, LL& x, LL& y)
{
if(!b){ x = 1; y = 0; return a; }
LL ret = exgcd(b, a % b, x, y);
LL tmp = x; x = y;
y = tmp - a/b*y;
return ret;
}
LL mod_inverse(LL a,LL m)
{
LL x,y;
exgcd(a,m,x,y);
return (m+x%m)%m;
}
LL BSGS(LL a,LL b,LL MOD){
LL m = sqrt(MOD + 0.5);
//if(m * m < MOD) m++;
LL am = mod_pow(a,m,MOD);
LL inv_am = mod_inverse(am,MOD);
map<LL,LL> mp;
//baby step
LL aj = 1;
mp[1] = m; // ÓÃm±íʾ0
for(int j = 1;j < m;j++){
aj = aj * a % MOD;
if(!mp[aj]) mp[aj] = j;
}
//giant step
LL inv_am_i = b % MOD;
if(mp[inv_am_i]) return mp[inv_am_i] % m;
for(int i = 1;i < m;i++){
inv_am_i = inv_am_i * inv_am % MOD;
if(mp[inv_am_i]) return i * m + (mp[inv_am_i] % m);
}
return -1;
}
LL ex_BSGS(LL a,LL b,LL MOD){
LL g,c = 0,v = 1;
while((g = __gcd(a,MOD)) != 1){
if(b % g) return -1;
MOD /= g;
b /= g;
v = v * a / g % MOD;
c++;
if(b == v) return c;
}
b = b * mod_inverse(v,MOD) % MOD;
LL ret = BSGS(a,b,MOD);
return ~ret?ret + c:ret;
}
int main(){
while(cin>>K>>P>>N){
LL ans = ex_BSGS(K,N,P);
if(ans == -1 || N >= P){
printf("Orz,I can’t find D!\n");
}
else{
printf("%lld\n",ans);
}
}
return 0;
}