就是求Ax三B(mod C)当C为素数时

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long LL;
const int MAXINT = (( << ) - ) * + ; int A, B, C;
struct Hashmap //哈希表代替map
{
static const int Ha = , maxe = ;
int E, lnk[Ha], son[maxe + ], nxt[maxe + ], w[maxe + ];
int top, stk[maxe + ];
void clear() { E = ; while (top) lnk[stk[top--]] = ; }
void Add(int x, int y) { son[++E] = y; nxt[E] = lnk[x]; w[E] = MAXINT; lnk[x] = E; }
bool count(int y)
{
int x = y%Ha;
for (int j = lnk[x]; j; j = nxt[j])
if (y == son[j]) return true;
return false;
}
int& operator [] (int y)
{
int x = y%Ha;
for (int j = lnk[x]; j; j = nxt[j])
if (y == son[j]) return w[j];
Add(x, y); stk[++top] = x; return w[E];
}
};
Hashmap f; int exgcd(int a, int b, int &x, int &y)
{
if (!b) { x = ; y = ; return a; }
int r = exgcd(b, a%b, x, y), t = x; x = y; y = t - a / b*y;
return r;
}
int BSGS(int A, int B, int C)
{
if (C == ) if (!B) return A != ; else return -;
if (B == ) if (A) return ; else return -;
if (A%C == ) if (!B) return ; else return -; //几种特判
int m = ceil(sqrt(C)), D = , Base = ; f.clear();
for (int i = ; i <= m - ; i++) //先把A^j存进哈希表
{
f[Base] = min(f[Base], i);
Base = ((LL)Base*A) % C;
}
for (int i = ; i <= m - ; i++)
{
int x, y, r = exgcd(D, C, x, y);
x = ((LL)x*B%C + C) % C; //扩欧求A^j
if (f.count(x)) return i*m + f[x]; //找到了
D = ((LL)D*Base) % C;
}
return -;
}
int main()
{
while (~scanf("%d%d%d", &C, &A, &B))
{
int ans = BSGS(A, B, C);
if (ans == -) printf("no solution\n"); else
printf("%d\n", ans);
}
return ;
}

Discrete Logging ZOJ - 1898 (模板题大小步算法)的更多相关文章

  1. POJ2417 Discrete Logging | A,C互质的bsgs算法

    题目: 给出A,B,C 求最小的x使得Ax=B  (mod C) 题解: bsgs算法的模板题 bsgs 全称:Baby-step giant-step 把这种问题的规模降低到了sqrt(n)级别 首 ...

  2. hdu 2586 How far away?(LCA模板题+离线tarjan算法)

    How far away ? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  3. hdu-3549 Flow Problem---最大流模板题(dinic算法模板)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3549 题目大意: 给有向图,求1-n的最大流 解题思路: 直接套模板,注意有重边 传送门:网络流入门 ...

  4. P1184 高手之在一起(字典树模板题,hash算法, map)

    哎,唯一值得说明的是,这道题的输入有bug 先把字典树的算法模板放一下 #include<iostream> #include<cstring> using namespace ...

  5. HDU2255 奔小康赚大钱 (最大权完美匹配) 模板题【KM算法】

    <题目链接> 奔小康赚大钱 Problem Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊 ...

  6. HDU 2389 Rain on your Parade 最大匹配(模板题)【HK算法】

    <题目链接> 题目大意:有m个宾客,n把雨伞,预计时间t后将会下大雨,告诉你每个宾客的位置和速度,每把雨伞的位置,问你最多几个宾客能够拿到伞. 解题分析: 本题就是要我们求人与伞之间的最大 ...

  7. hdu 4009 最小树形图模板题朱刘算法

    #include<stdio.h> /*思路:显然对于每个地方, 只有一种供水方式就足够了,这样也能保证花费最小, 而每个地方都可以自己挖井,所以是不可能出现无解的情况的, 为了方便思考, ...

  8. BSGS算法(大小步算法)

    $BSGS$ 算法 $Baby\ Steps\ Giant\ Steps$. 致力于解决给定两个互质的数 $a,\ p$ 求一个最小的非负整数 $x$ 使得 $a^x\equiv b(mod\ p)$ ...

  9. [poj2449]Remmarguts' Date(K短路模板题,A*算法)

    解题关键:k短路模板题,A*算法解决. #include<cstdio> #include<cstring> #include<algorithm> #includ ...

随机推荐

  1. Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';

    springboot jdbc查询使用LocalDate报:Failed to convert value of type 'java.lang.String' to required type 'j ...

  2. swiper下滑分页,减少swiper-slide项的时候会出现空白

    修改子项后,先重置当前的页,调用 swiper.slideTo(0); 滚动到初始位置 再调用 swiper.update(); 更新一系列设置就可以了.

  3. Windows安装diango框架<一>

    下一篇:使用Django创建网站项目<二> python工具安装 python下载:https://www.python.org/downloads/windows/(我的版本3.7.0) ...

  4. C#设计模式——简单工厂模式、工厂模式和抽象工厂模式

    一:简单工厂模式 1:描述:简单工厂模式是由一个工厂对象根据接收到的消息决定要创建哪一个类的对象事例. 2:优点:工厂类中有相关逻辑判断,可以根据需要动态创建相关的对象事例,而客户端只需要告诉工厂类创 ...

  5. CSS之fontAwesome代替网页icon小图标

    引言 奥森图标(Font Awesome)提供丰富的矢量字体图标—通过CSS可以任意控制所有图标的大小 ,颜色,阴影. 网页小图标到处可见,如果一个网页都是干巴巴的文字和图片,而没有小图标,会显得非常 ...

  6. 用 JS 写 (轮播图 / 选项卡 / 滑动门)

    页面中经常会用到各式各样的轮播图,今天贺贺为大家介绍一种常用的方法,对于JS我们需要举一反三,一种方法可以对多个轮播样式进行渲染. <head> <meta charset=&quo ...

  7. 读懂SAP Leonardo物联网平台

    读懂SAP Leonardo物联网平台 https://blog.csdn.net/weixin_42137700/article/details/81903290 本文比较系统.全面地介绍了SAP ...

  8. 为什么 C# 比 C++ 编译快那么多

    Go 我不懂,下面以 C++ 和 C# 对比来说明为什么 C++ 编译慢和 C# 编译快. C 和 C++ 文件的编译经过几个主要步骤: 处理续行符处理(“\”)之类的杂事 词法分析,解析出 toke ...

  9. Android常用数据类型转换

    String转int.float.double.byte[].bitmap Int i = Integer.parseInt(str); Float f = Float.parseFloat(str) ...

  10. Apktool(1)——Apktool的安装

    Apktool是google提供的apk的编译工具,有了它就可以做很多事情.比如获取apk的源码,apk汉化,对手机rom包做一些美化. 首先来看看apktool的安装(配置): 以下内容主要翻译字A ...