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. Google Map API 学习2-界面展示

  2. 最详细在Windows安装Xamarin.iOS教程

    最详细在Windows安装Xamarin.iOS教程 来源:http://www.cnblogs.com/llyfe2006/articles/3098280.html 本文展示了如何设立Xamari ...

  3. 有关DOM的小总结

    一直以为DOM(文档对象模型)是JS中最简单的一部分.不可否认,它确实很简单,因为DOM的思维模式有点固定,只需要简单地记住一些固定的方法,所以DOM可以说是所有js(这里指的是客户端的js)入门的起 ...

  4. HDOJ 2018 母牛的故事

    Problem Description 有一头母牛,它每年年初生一头小母牛.每头小母牛从第四个年头开始,每年年初也生一头小母牛.请编程实现在第n年的时候,共有多少头母牛? Input 输入数据由多个测 ...

  5. [转]让程序在崩溃时体面的退出之CallStack

    原文地址:http://blog.csdn.net/starlee/article/details/6618849 在我的那篇<让程序在崩溃时体面的退出之Unhandled Exception& ...

  6. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  7. XSS 简单理解之:AntiSamy

    AntiSamy介绍 OWASP是一个开源的.非盈利的全球性安全组织,致力于应用软件的安全研究.我们的使命是使应用软件更加安全,使企业和组织能够对应用安全风险作出更清晰的决策.目前OWASP全球拥有1 ...

  8. Hibernate自定义数据库查询(排序、输出条数)

    Hibernate数据库操作类(eg:TexDAO.java) /* * queryString HQL语句,first开始条数, max输出条数 ,norder排序 * 例: List lis = ...

  9. 用python 装饰器打log

    # coding=utf-8    from time import time def logged(when):     def log(f,*args,**kargs):         prin ...

  10. Away3D ATFTexture

    之前在项目中贴图大量使用了 PNG 和 jpg 遇到了个问题.在使用BitmapTexture的时候发现 是必须MIP 不管你 是否开启或者关闭 MIP 他都会去创建.而每次MIP都会根据贴图大小去生 ...