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. VUE小案例--简易计算器

    这个小案例主要时练习v-model的使用,功能并不完善 <!DOCTYPE html> <html lang="zh-CN"> <head> & ...

  2. Hibernate与 MyBatis的区别

    第一章     Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀 ...

  3. html5 lineTo的使用例子

    demo.js function draw(id) { var CANVAS=document.getElementById(id); var context=CANVAS.getContext('2 ...

  4. SSH配置与修改

    ssh文件路径:/etc/ssh/ ssh的日志文件:/var/log/secure 端口修改:./sshd_config 服务启停: service sshd start/stop/restart ...

  5. 【leetcode】722. Remove Comments

    题目如下: Given a C++ program, remove comments from it. The program source is an array where source[i] i ...

  6. sql语句中取整数和小数部分

    sql 取整数去小数点 ,) ' Sql截取浮点小数位数,不四舍五入 ,) 结果:551.24 ,) 结果:551.23 第一个2表示截取2位 第二个0,1分别表示0是四舍五入,0以外是截取 如何分别 ...

  7. LDD3 第11章 内核的数据类型

    考虑到可移植性的问题,现代版本的Linux内核的可移植性是非常好的. 在把x86上的代码移植到新的体系架构上时,内核开发人员遇到的若干问题都和不正确的数据类型有关.坚持使用严格的数据类型,并且使用-W ...

  8. JS中Object的一些关于原型的方法

    1.Object.getPrototypeOf(obj) 该方法返回 obj 对象的原型对象,等同于 obj.__proto__.获取对象的原型对象推荐使用该方法而不是 obj.__proto__方法 ...

  9. <三剑客> 老三:grep命令用法

    grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正 ...

  10. PHP之namespace小结

    命名空间的使用 在声明命名空间之前唯一合法的代码是用于定义源文件编码方式的 declare 语句.所有非 PHP 代码包括空白符都不能出现在命名空间的声明之前. PHP 命名空间中的类名可以通过三种方 ...