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. 完善Hikari连接池扩展项目HikariApi(ORM)

    以前介绍类自定义的Hikari项目,定位于数据库连接池:后扩展了,根据文件名称,以数据库配置文件为基础,支持按照名称多数据操作. 在使用中,发现扩展了SQL语句参数化操作,在管理类中,以扩展方法存在. ...

  2. mpvue中的平台状态判断(H5网页 or 小程序)

    在开发微信小程序或者微信网页H5的时候,有时我们利用外部组件可能不兼容这两者,需要区分开来,可以在对应的mainjs中配置如下 let platform: try{ if(wx){ platform= ...

  3. 关于在IE下JavaScript的 Stack overflow at line 错误可能的原因

    该错误只在IE中出现,出现该提示的原因主要有两种: 1. 重定义了系统的触发事件名称作为自定义函数名如:  onclick / onsubmit …  都是系统保留的事件名称,不允许作为重定义函数名称 ...

  4. Java疯狂讲义笔记——内部类

    [定义]内部类:定义在其它类内部的类.外部类:包含内部类的类,也称 宿主类.局部内部类:定义在方法里的内部类. [接口内部类]接口中也可以定义内部类,必须为public static修饰(自动添加), ...

  5. sed使用---转义字符

    https://blog.csdn.net/wangcg123/article/details/50667883 单引号里面,s表示替换,三根斜线中间是替换的样式,特殊字符需要使用反斜线”\”进行转义 ...

  6. excel条件格式 满足包含xx的整行高亮

    条件格式-->新建规则-->使用公式确定要设置格式的单元格   =COUNTIF($D4,"*_S_*")   =COUNTIF($D4,"*_M_*&quo ...

  7. center os7 安装mysql

    安装mariadb MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可.开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险, ...

  8. Es学习第七课, term、terms、match等基本查询语法

    term.terms查询 term query会去倒排索引中寻找确切的term,它并不知道分词器的存在,这种查询适合keyword.numeric.date等明确值的 term:查询某个字段里含有某个 ...

  9. CentOS7.5 开启Samba服务

    安装 yum install samba 其依赖关系包samba-client samba-common会自动安装上去 查看状态 service smb status 重启服务systemctl re ...

  10. css----overflow(布局)

    CSS overflow 属性用于控制内容溢出元素框时显示的方式. CSS Overflow CSS overflow 属性可以控制内容溢出元素框时在对应的元素区间内添加滚动条. overflow属性 ...