HDU 2815 Mod Tree 解高次同余方程

Mod Tree

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3908    Accepted Submission(s): 1018


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


转换一下就变成A^x=B(mod C)的问题,需要注意的是B>=0&&B<=C-1,若B不在此范围,则无解。
另外注意,如果你粘贴Orz,I can’t find D!就会WA到死,就是这么坑。
//1544K	125MS
#include<stdio.h>
#include<cmath>
#define Maxn 65535
struct hash
{
    int a,b,next;
}Hash[Maxn*2];
int flg[Maxn+66];
int top,idx;
void ins(int a,int b)
{
    int k=b&Maxn;
    if(flg[k]!=idx)
    {
        flg[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(flg[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)
{
    return b==0?a:gcd(b,a%b);
}
int ex_gcd(int a,int b,int& x,int& y)
{
    int t,ret;
    if(!b){x=1,y=0;return a;}
    ret=ex_gcd(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;
    ex_gcd(a,n,x,y);
    e=(long long)x*b%n;
    return e<0?e+n:e;
}
int pow_mod(long long a,int b,int c)
{
    long long 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;
    long long 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(C*1.0));
    for(buf=1%C,i=0;i<=M;buf=buf*A%C,++i)ins(i,buf);
    for(i=0,K=pow_mod((long long)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()
{
    int a,b,c;
    while(scanf("%d%d%d",&a,&c,&b)!=EOF)
    {
        if(b>c){printf("Orz,I can’t find D!\n");continue;}
        b%=c;
        int ans=BabyStep(a,b,c);
        if(ans==-1)printf("Orz,I can’t find D!\n");
        else printf("%d\n",ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值