Mod Tree

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 96 Accepted Submission(s): 38
 
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

证明来自:

http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL ;
const int N = ; struct B
{
LL num , id ;
bool operator < ( const B &a ) const{
if( num != a.num ) return num < a.num;
else return id < a.id ;
}
}baby[N]; LL n , k , p ;
int tot ;
void e_gcd( LL &x , LL &y , LL &d , LL a , LL b ){
if( b == ){ x = , y = ; d = a ; return ; }
e_gcd( y , x , d , b , a%b );
y -= x* (a/b) ;
} int inv( LL a, LL b ,LL n )
{
LL d,e,x,y ;
e_gcd(x,y,d,a,n);
e = ( x * b ) % n ;
return e < ? e + n : e;
} inline LL gcd(LL a, LL b ){ return b == ? a : gcd( b , a % b ) ; } LL quick_mod( LL a , LL b ,LL mod )
{
LL res = ;
while( b )
{
if( b & ) res = res * a % mod ;
a = a * a % mod ;
b >>= ;
}
return res ;
} int find( LL n )
{
int l = , r = tot - ;
while( l <= r ){
int m = (l + r) >> ;
if( baby[m].num == n){
return baby[m].id;
}
else if( baby[m].num < n )
l = m + ;
else
r = m - ;
}
return -;
} void run()
{
if( p <= n ){
puts("Orz,I can’t find D!");
return ;
}
LL temp = % p ;
for( int i = ; i < ; ++i ) {
if( temp == n ){
printf("%d\n",i);
return ;
}
temp = temp * k % p ;
} LL d = , kk = % p ;
while( ( temp = gcd( k , p ) ) != ){
if( n % temp ) {
puts("Orz,I can’t find D!");
return ;
}
d ++ ;
p /= temp;
n /= temp;
kk = k / temp * kk % p ;
}
int m = ( int ) ceil( sqrt( (double)p ) );
baby[].num = , baby[].id = ;
for( int i = ; i <= m ; ++i ){
baby[i].num = baby[i-].num * k % p ;
baby[i].id = i ;
}
sort( baby , baby + m + ) ;
tot = ;
for( int i = ; i <= m ; ++i ){
if(baby[i].num != baby[tot-].num ){
baby[tot++] = baby[i];
}
} LL am = quick_mod( k , m , p ); for( int j = ; j <= m ; ++j ){
temp = inv(kk,n,p);
if( temp < ){
continue ;
}
int pos = find( temp );
if( pos != - ){
printf("%d\n", m * j + d + pos );
return ;
}
kk = kk * am % p ;
}
puts("Orz,I can’t find D!");
}
int main()
{
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
while( scanf("%I64d%I64d%I64d",&k,&p,&n) != EOF ) run();
}

HDU 2815 Mod Tree (扩展 Baby Step Giant Step )的更多相关文章

  1. HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法

    联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...

  2. hdu 2815 : Mod Tree 【扩展BSGS】

    题目链接 直接用模板好了.实在不行,反正有队友啊~~~~ #include<bits/stdc++.h> using namespace std; typedef long long LL ...

  3. hdu 2815 Mod Tree (exBSGS)

    http://acm.hdu.edu.cn/showproblem.php?pid=2815 //解 K^D ≡ N mod P #include<map> #include<cma ...

  4. hdu 2815 Mod Tree 高次方程,n不为素数

    Accepted 406MS 8576K 2379 B C++/** 这里加了一点限制,,大体还是一样的,, **/ #include <iostream> #include <cs ...

  5. HDU 2815 Mod Tree

    不会,先搁着…… http://blog.csdn.net/acm_cxlove/article/details/7832197

  6. HDU 2815 扩展baby step giant step 算法

    题目大意就是求 a^x = b(mod c) 中的x 用一般的baby step giant step 算法会超时 这里参考的是http://hi.baidu.com/aekdycoin/item/2 ...

  7. 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法

    先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝  扩展Baby Step Gian ...

  8. POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)

    不理解Baby Step Giant Step算法,请戳: http://www.cnblogs.com/chenxiwenruo/p/3554885.html #include <iostre ...

  9. [置顶] hdu2815 扩展Baby step,Giant step入门

    题意:求满足a^x=b(mod n)的最小的整数x. 分析:很多地方写到n是素数的时候可以用Baby step,Giant step, 其实研究过Baby step,Giant step算法以后,你会 ...

随机推荐

  1. Android线程间通信的几种实现方式

    1. 通过Handler机制: private void one() { handler=new Handler(){ @Override public void handleMessage(Mess ...

  2. smbsh - 允许用UNIX命令访问NT文件系统

    总览 smbsh 描述 此程序是Samba套件的一部分. smbsh允许你用UNIX命令诸如ls,egrep和rcp等来访问NT文件系统.必须用动态链接的shell以便使smbsh工作正常. 从命令提 ...

  3. CMSIS-DAP

    https://www.jixin.pro/bbs/topic/4187 https://lceda.cn/jixin002/stm32f103c8t6_cmsis-dap http://tieba. ...

  4. ActiveMQ修改端口号

    1.修改tcp端口号 安装目录下的conf/activemq.xml 2.修改管理页面的访问端口号 安装目录下的conf/jetty.xml

  5. java 字符串常量池

  6. 03.父工程pom、整合测试、SpringBootApplication注解

    父工程 idea点击spring-boot-starter-parent找到父工程spring-boot-dependencies模仿配置 父工程 <?xml version="1.0 ...

  7. Shell输入命令时一些有用的快捷键

    Ctrl + u: 从光标所在位置一直删除到开头 Ctrl + k: 从光标所在位置一直删除到尾 Ctrl + b: 光标向后移动一个字符 Ctrl + f: 光标后前移动一个字符 Alt + b: ...

  8. paper 164: Siamese网络--相似度量方法

    简介:    Siamese网络是一种相似性度量方法,当类别数多,但每个类别的样本数量少的情况下可用于类别的识别.分类等.传统的用于区分的分类方法是需要确切的知道每个样本属于哪个类,需要针对每个样本有 ...

  9. Monkey命令及调试

    monkey命令: 执行50w次,随机数200,忽略crash,忽略超时,详细信息级别为1:monkey -p 包名 -s 200 --throttle 100 --ignore-crashes -- ...

  10. Equivalent Prefixes

    题目链接 题意:给你两个数组a,b,大小为n,让你寻找一个数p (1<= p <= n) ,使之在 1~p 任意一个区间中a,b数组的最小值下标相同. 思路:看到用线段树去写的我也是服了. ...