HDU 2815 Mod Tree (扩展 Baby Step Giant Step )
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 |
Sample Output
Orz,I can’t find D! |
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 )的更多相关文章
- HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法
联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- hdu 2815 : Mod Tree 【扩展BSGS】
题目链接 直接用模板好了.实在不行,反正有队友啊~~~~ #include<bits/stdc++.h> using namespace std; typedef long long LL ...
- hdu 2815 Mod Tree (exBSGS)
http://acm.hdu.edu.cn/showproblem.php?pid=2815 //解 K^D ≡ N mod P #include<map> #include<cma ...
- hdu 2815 Mod Tree 高次方程,n不为素数
Accepted 406MS 8576K 2379 B C++/** 这里加了一点限制,,大体还是一样的,, **/ #include <iostream> #include <cs ...
- HDU 2815 Mod Tree
不会,先搁着…… http://blog.csdn.net/acm_cxlove/article/details/7832197
- HDU 2815 扩展baby step giant step 算法
题目大意就是求 a^x = b(mod c) 中的x 用一般的baby step giant step 算法会超时 这里参考的是http://hi.baidu.com/aekdycoin/item/2 ...
- 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法
先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝 扩展Baby Step Gian ...
- 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 ...
- [置顶] hdu2815 扩展Baby step,Giant step入门
题意:求满足a^x=b(mod n)的最小的整数x. 分析:很多地方写到n是素数的时候可以用Baby step,Giant step, 其实研究过Baby step,Giant step算法以后,你会 ...
随机推荐
- 对VS2019进行32位汇编环境配置
1.库文件(很重要) 用我这一份就行:https://www.lanzous.com/i6364hg 2.VS依赖库 打开VS2019,选择桌面向导 配置项目时,选择新项目. 选择生成依赖项 选中ma ...
- Windows程序设计--(一)起步
1.3 你的第一个Windows程序 1.3.2 Windows 对应程序 #include <windows.h> int WINAPI WinMain(HINSTANCE hInsta ...
- tornado ioloop current和instance的一些区别
import tornado.ioloop # 此时_current没有instance print dir(tornado.ioloop.IOLoop._current) # 通过instance ...
- unity DOTween Pro的使用--简化流程--自动播放
当gameobject setActive(true)的时候自动播放动画 1) 添加DoTween Animation. 设置动画效果, 略 选中 AutoPlay, 取消 AutoKill 2) 在 ...
- 【记录】微信emoji表情存入数据库
最近遇到一个问题,在微信授权获取用户信息的时候,由于微信昵称(nickName)是表情,无法存入数据库. 查其原因,原来是因为数据库的编码格式不正确,我的是utf-8 改成utf8mb4就可以了. 其 ...
- jenkins部署的零碎知识
环境要求 1)版本控制子系统(SVN):SVN服务器.项目对应版本库.版本库中钩子程序(提交代码后,触发Jenkins自动打包并部署到应用服务器)(2)持续集成子系统(存在Jenkins的服务器):J ...
- Java的GC机制及算法
GC的阶段 对每个对象而言,垃圾回收分为两个阶段:finalization和reclamation. finalization: 指运行这个对象的finalize的方法. reclamation: ...
- canvas 图片反色
代码实例: <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <tit ...
- 【串线篇】Mybatis拓展之MBG
MBG-逆向工程 一.介绍 MBG:MyBatis Generator:代码生成器: MyBatis官方提供的代码生成器:帮我们逆向生成: 正向: table----javaBean---BookDa ...
- Hadoop编程调用HDFS(PYTHON)
1.运行环境 开发工具:PyCharm Python 版本:3.5 Hadoop环境: Cloudera QuickStart 2.GITHUB地址 https://github.com/nbfujx ...