Mod Tree
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4969 Accepted Submission(s): 1298
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)
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!”
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
注意B<C
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 65535
#define MAXM 100010
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
int Read()
{
char ch;
int a = 0;
while((ch = getchar()) == ' ' | ch == '\n');
a += ch - '0';
while((ch = getchar()) != ' ' && ch != '\n')
{
a *= 10;
a += ch - '0';
}
return a;
}
void Print(int a) //输出外挂
{
if(a>9)
Print(a/10);
putchar(a%10+'0');
}
struct hash
{
int a,b,next;
}Hash[MAXN*2];
int flag[MAXN+66];
int top,index;
void ins(int a,int b)
{
int k=b&MAXN;
if(flag[k]!=index)
{
flag[k]=index;
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]!=index) 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 t,ret;
if(!b)
{
x=1; y=0; return a;
}
ret=Exgcd(b,a%b,x,y);
t=x; x=y; y=t-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 pow_mod(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; ++index;
ll buf=1%C,D=buf,K;
int i,d=0,tmp;
for(i=0;i<=100;buf=buf*A%C,++i)
if(buf==B)
return i;
while((tmp=gcd(A,C))!=1)
{
if(B%tmp) return -1;
++d;
C/=tmp;
B/=tmp;
D=D*A/tmp%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=pow_mod((ll)A,M,C);i<=M;D=D*K%C,++i)
{
tmp=Inval((int)D,B,C);
int w;
if(tmp>=0&&(w=find(tmp))!=-1)
return i*M+w+d;
}
return -1;
}
int main()
{
// fread;
int A,B,C;
while(scanf("%d%d%d",&A,&C,&B)!=EOF)
{
if(B>C)
{
puts("Orz,I can’t find D!");
continue;
}
B%=C;
int res=BabyStep(A,B,C);
if(res<0) puts("Orz,I can’t find D!");
else printf("%d\n",res);
}
return 0;
}