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();
}

最新文章

  1. poj 3614
  2. ubuntu12.04中shell脚本无法使用source的原因及解决方法
  3. mycat的读写分离设置
  4. 微信小程序-设备
  5. 初学Python之字符串操作
  6. ANSI_NULLS、QUOTED_IDENTIFIER
  7. NSSpeechSynthesizer 文字变语音
  8. Beautiful Numbers
  9. 在.Net中将RocketMQ跑起来_入门篇【2】
  10. C# - 如何让类型可以比较
  11. 《Node.js高级编程》之Node 核心API基础
  12. Mysql加锁过程详解(5)-innodb 多版本并发控制原理详解
  13. 部署描述符 web.xml
  14. git 小乌龟安装教程
  15. 【python014--字符串内置函数】
  16. c# winform 自动升级
  17. solr开发,提交索引数据的几种方式
  18. Improving Performance【转】
  19. HDUOJ-----(1329)Calling Extraterrestrial Intelligence Again
  20. sql日期查询

热门文章

  1. Ajax爬取豆瓣电影目录(Python)
  2. saltstack的高级管理
  3. Asp.Netcore使用Filter来实现接口的全局异常拦截,以及前置拦截和后置拦截
  4. 道路识别demo
  5. springboot2.0+websocket集成【群发消息+单对单】(二)
  6. Sass--调用混合宏
  7. TCP协议之三次握手四次挥手
  8. 为什么要用unittest
  9. html中 的method=post和method=get的区别
  10. cocos2D-X 打包