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
 
Source
Recommend
lcy   |   We have carefully selected several similar problems for you:  2814 2809 3037 2447 2810
【分析】
普通的BSGS是只能够解决A^x = B(mod C),其中C为素数的形式,而通过网上的消元的方法能够解决这种问题。
同样BSGS直接解决C为素数的形式也是可以的.
 /*
宋代晏几道
《生查子·狂花顷刻香》
狂花顷刻香,晚蝶缠绵意。天与短因缘,聚散常容易。
传唱入离声,恼乱双蛾翠。游子不堪闻,正是衷肠事。
*/
#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的更多相关文章

  1. Mod Tree(hdu2815)

    Mod Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. [拓展Bsgs] Clever - Y

    题目链接 Clever - Y 题意 有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\). 解法 如题,是拓展 \(Bsgs\) 板 ...

  3. 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

    什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...

  4. 【SPOJ】Power Modulo Inverted(拓展BSGS)

    [SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...

  5. 【POJ 3243】Clever Y 拓展BSGS

    调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那 ...

  6. 数学:拓展BSGS

    当C不是素数的时候,之前介绍的BSGS就行不通了,需要用到拓展BSGS算法 方法转自https://blog.csdn.net/zzkksunboy/article/details/73162229 ...

  7. 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 ...

  8. hdu 2815 : Mod Tree 【扩展BSGS】

    题目链接 直接用模板好了.实在不行,反正有队友啊~~~~ #include<bits/stdc++.h> using namespace std; typedef long long LL ...

  9. 【POJ3243】拓展BSGS(附hash版)

    上一篇博文中说道了baby step giant step的方法(简称BSGS),不过对于XY mod Z = K ,若x和z并不互质,则不能直接套用BSGS的方法了. 为什么?因为这时候不存在逆元了 ...

随机推荐

  1. 移植lrzsz串口文件传输工具到mini2440

    1.下载源码2.解压源码 tar -xzf lrzsz-0.12.20.tar.gz 3.检查配置 ./configure 4.修改Makefile 有三个Makefile需要修改,分别是lrzsz- ...

  2. JQuery中如何click中传递参数

    代码如下: click(data,fn)中的data其实是json对象,取的时候,只能通过当前的事件源来取,data是默认放在event中的,所以这里的data是eventdata,引用的时候也使用e ...

  3. Delphi Web Service和ISAPI的区别与联系 转

    Web Service和ISAPI的区别与联系   1.Web Service 是一种新的web应用程序分支,他们是自包含.自描述.模块化的应用,可以发布.定位.通过web调用.Web Service ...

  4. cmd命令行设置环境变量

    http://blog.sciencenet.cn/blog-51026-566742.html 1.查看当前所有可用的环境变量:输入 set 即可查看. 2.查看某个环境变量:输入 “set 变量名 ...

  5. NDK的安装和下载

    从官网下载NDK 下载页面:https://developer.android.com/ndk/downloads/index.html 从镜像站点下载NDK "大师兄"是一个由腾 ...

  6. lightoj 1021 - Painful Bases 状态压缩

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1021 #include<cstring> #include<cstd ...

  7. Childlife旗下三驾马车

    Childlife旗下,尤其以 “提高免疫力”为口号的“三驾马车”:第一防御液.VC.紫雏菊,是相当热门的海淘产品.据说这是一系列“成分天然.有效治愈感冒提升免疫力.由美国著名儿科医生研发”的药物.

  8. Spark RDD概念学习系列之Spark Hash Shuffle内幕彻底解密(二十)

    本博文的主要内容: 1.Hash Shuffle彻底解密 2.Shuffle Pluggable解密 3.Sorted Shuffle解密 4.Shuffle性能优化 一:到底什么是Shuffle? ...

  9. Grandpa's Estate - POJ 1228(稳定凸包)

    刚开始看这个题目不知道是什么东东,后面看了大神的题解才知道是稳定凸包问题,什么是稳定凸包呢?所谓稳定就是判断能不能在原有凸包上加点,得到一个更大的凸包,并且这个凸包包含原有凸包上的所有点.知道了这个东 ...

  10. android生成二维码

    新建项目 布局截图如下(一个输入框,一个按钮,一个imageview),输入想要的东西(文字,数字,网站链接等)然后点击按钮生成二维码,然后可以扫描识别. 首先需要一个谷歌的一个jar包 activi ...