【HDU2815】【拓展BSGS】Mod Tree

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.
Every cases have only three integers indicating K, P, N. (1<=K, P, N<=10^9)
If you can’t find such D, just output “Orz,I can’t find D!”
4 1314520 65536
5 1234 67
8
20
/*
宋代晏几道
《生查子·狂花顷刻香》
狂花顷刻香,晚蝶缠绵意。天与短因缘,聚散常容易。
传唱入离声,恼乱双蛾翠。游子不堪闻,正是衷肠事。
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <vector>
#define LOCAL
const int MAXN = + ;
const int INF = 0x7fffffff;
using namespace std;
typedef long long LL; int gcd(int a, int b ){return b == ? a : gcd(b, a % b);}
int ext_gcd(int a, int b, int &x, int &y){
if (b == ) {x = ; y = ; return a;}
int tmp = ext_gcd(b, a % b, y, x);
y -= x * (a / b);
return tmp;
}
//求解
int Inval(int a, int b, int n){
int x, y, e;
ext_gcd(a, n, x, y);
e = (LL) x * b % n;//小心超出int 的范围,因为a,n是互质的,因此求解出来的结果就是ax + ny = 1,乘以b才正确的答案
return e < ? e + n : e;
}
// k s m
int pow_mod(LL a, int b, int c){
if (b == ) return a % c;
LL tmp = pow_mod(a, b / , c);
if (b % == ) return (tmp * tmp) % c;
else return (((tmp * tmp) % c) * a) % c;
}
int BSGS(int A, int B, int C){
map<int, int> H;//hash
LL buf = % C, D = buf, K;
int d = , tmp;
//小步
for (int i = ; i <= ; buf = buf * A % C, i++)
if (buf == B) return i;
//消因子
while ((tmp = gcd(A, C)) != ){
if (B % gcd(A, C) != ) return -;//为了解不定方程
d++;
C /= tmp;
B /= tmp;
D = D * A / tmp % C;
}
H.clear();
int M = (int)ceil(sqrt(C * 1.0));
buf = % C;
for (int i = ; i <= M; buf = buf * A % C, i++)
if (H.find((int)buf) == H.end()) H[(int)buf] = i;//Hash K = pow_mod ((LL) A, M, C);
for (int i = ; i <= M; D = D * K % C, i++){
tmp = Inval((int) D ,B, C);//D和C是互质的
//一定不要忘了最后的d
if (tmp >= && H.find(tmp) != H.end()) return i * M + H[tmp] + d;
}
return -;//找不到
}
int main(){ //转换为A^x = B(mod C)的形式
int A, B, C;
while (scanf("%d%d%d", &A, &C, &B) != EOF){
if (B >= C) {printf("Orz,I can’t find D!\n"); continue;}//
int tmp = BSGS(A, B, C);
if (tmp < ) printf("Orz,I can’t find D!\n");
else printf("%d\n", tmp);
}
return ;
}
【HDU2815】【拓展BSGS】Mod Tree的更多相关文章
- Mod Tree(hdu2815)
Mod Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- [拓展Bsgs] Clever - Y
题目链接 Clever - Y 题意 有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\). 解法 如题,是拓展 \(Bsgs\) 板 ...
- 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)
什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...
- 【SPOJ】Power Modulo Inverted(拓展BSGS)
[SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...
- 【POJ 3243】Clever Y 拓展BSGS
调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那 ...
- 数学:拓展BSGS
当C不是素数的时候,之前介绍的BSGS就行不通了,需要用到拓展BSGS算法 方法转自https://blog.csdn.net/zzkksunboy/article/details/73162229 ...
- 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 Subm ...
- hdu 2815 : Mod Tree 【扩展BSGS】
题目链接 直接用模板好了.实在不行,反正有队友啊~~~~ #include<bits/stdc++.h> using namespace std; typedef long long LL ...
- 【POJ3243】拓展BSGS(附hash版)
上一篇博文中说道了baby step giant step的方法(简称BSGS),不过对于XY mod Z = K ,若x和z并不互质,则不能直接套用BSGS的方法了. 为什么?因为这时候不存在逆元了 ...
随机推荐
- java对文件拷贝的简单操作
package fileInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNot ...
- Spring IOC配置与应用
1. FAQ:不给提示: a) window – preferences – myeclipse – xml – xml catalog b) User Specified E ...
- kafka leader 服务器均衡。
Whenever a broker stops or crashes leadership for that broker's partitions transfers to other replic ...
- 关于PHP程序使用file_get_content()函数进行抓取PHP程序与smarty结合编译过程中产生的静态文件,抓取不了?连接超时?(地址映射)
问题: 当file_get_content()函数的参数 url中是localhost时不能抓取,是127.0.0.1时可以抓取到静态html代码.实现页面静态化技术提高访问效率. test.php ...
- hibernate批量删除和更新数据
转载自:http://blog.csdn.net/yuhua3272004/article/details/2909538 Hibernate3.0 採用新的基于ANTLR的HQL/SQL查询翻译器, ...
- Qtcreator中经常使用快捷键总结
Qtcreator中经常使用快捷键总结 F1 查看帮助 F2 跳转到函数定义(和Ctrl+鼠标左键一样的效果) Shift+F2 声明和定义之间切换 F4 ...
- [转] 一个资深iOS开发者对于React Native的看法
当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道. 我认为一个js开发者可以使用javascript来构建iPhone应用确实是一件很酷的事情,但是我很 ...
- media query
accepted Another useful media feature is device-aspect-ratio. Note that the iPhone 5 does not have a ...
- 【Android】数据的应用-使用sharedpreferences存储数据
Android应用开发SharedPreferences存储数据的使用方法 SharedPreferences是Android中最容易理解的数据存储技术,实际上SharedPreferences处理的 ...
- 11.1 afternoon
幸运数字(number)Time Limit:1000ms Memory Limit:64MB题目描述LYK 最近运气很差,例如在 NOIP 初赛中仅仅考了 90 分,刚刚卡进复赛,于是它决定使用一些 ...