【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的方法了. 为什么?因为这时候不存在逆元了 ...
随机推荐
- -_-#【HTML】同一个标签页打开
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Screen-Space Bent Cones (SSBC) in Unity5
噪音少.高度保留了纹理细节 博主近期渲染:最近用unity5弄的一些渲染 ---- by wolf96 http://blog.csdn.net/wolf96
- 【转】unity3d 各种优化综合
检测方式: 一,unity3d 渲染统计窗口 Game视窗的Stats去查看渲染统计的信息: 1.FPS fps其实就是 frames per second,也就是每一秒游戏执行的帧数,这个数值越 ...
- [转载]函数getopt(),及其参数optind
最近用到了getopt()这个函数,对它进行了一些了解.这篇博文还是写的非常清楚的.值得学习.最近在改进一个开源项目,希望自己能静下心好好分析代码. ------------------------- ...
- vijosP1285 佳佳的魔法药水
vijosP1285 佳佳的魔法药水 链接:https://vijos.org/p/1285 [思路] 图论思想. 很巧妙. 如A+B=C,将AB之间连边,边权为C,用以找相连物品与合成物. 用Dij ...
- Spring注入-Map
在spring框架中为Map注入属性 1map映射的对象创建 package com; /** * Map集合在spring中的使用测试 */ public class User { private ...
- python 写的http后台弱口令爆破工具
今天来弄一个后台破解的Python小程序,哈哈,直接上代码吧,都有注释~~ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...
- UVa1607 poj1435 UVaLive1686 Gates
填坑系列(p.246) 由函数连续性得满足二分性 #include<cstdio> #include<cstring> #include<cstdlib> #inc ...
- C#中5中timer的比较
C#中有5个timer,它们的主要区别如下: System.Threading.Timer 在线程池启动一个后台任务.我前段时间写过一个关于timer的垃圾回收的需要注意一下,参见谁动了我的time ...
- 使用strace追踪多个进程
http://www.ttlsa.com/tools/use-strace-to-track-multiple-processes/ strace是Linux环境下的一款程序调试工具,用来监察一个应 ...