【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的方法了. 为什么?因为这时候不存在逆元了 ...
随机推荐
- This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery 解决方法
This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'的意思是,这版本的 MySQL 不支持使 ...
- VS2010编译libjpeg
环境:win7旗舰版 x64 VS2010 下载源代码下载地址:http://www.ijg.org/,选择windows format file 解压源代码,修改源代码中jconfig.vc为jco ...
- 【PHP】将EXCEL表中的数据轻松导入Mysql数据表
在网络上有不较多的方法,在此介绍我已经验证的方法. 方法一.利用EXCEL表本身的功能生成SQL代码 ①.先在“phpmyadmin”中建立数据库与表(数据库:excel,数据表:excel01,字段 ...
- poj 1321 棋盘问题【dfs】
棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28308 Accepted: 13996 Descriptio ...
- ELK监控系统nginx / mysql慢日志
ELK监控系统nginx / mysql慢日志 elasticsearch logstash kibana ELK监控系统nginx日志 1.环境准备 centos6.8_64 mini IP:192 ...
- Google搜索解析
Google搜索解析 是一款相似于Google趋势的SEO 在 线keyword工具,其官方提出的口号是“看看全世界的人们都在搜索些什么”.利用Google搜索解析,能够比較特定区域.类别.时间范围以 ...
- linux_2.6内核内存缓冲与I/O调度机制:
http://blog.csdn.net/kaiwii/article/details/7030178 到底是BIO还是BH?答案是BIO与BH
- iOS NavigaitonController详解(code版)
参考文章:http://blog.csdn.net/totogo2010/article/details/7681879,参考了这篇文章,写的超级好,自己他的基础上加上了自己的理解. 下面的图显示了导 ...
- linux device driver —— ioctl
实现了应用程序和设备驱动通过ioctl通信.还是对设备驱动没什么感觉,贴一下代码吧. 在Ubuntu 16.04 64bit中测试通过 ioctldemo.c #include <linux/m ...
- viewpager+fragment学习笔记
有暇,总结一下viewpager+fragment的使用. 先来看看效果图: 有三个标题,三个fragment,滑动时标题的颜色会随着变化. MainActivity.java public clas ...