一道裸的BSGS题目(叫baby step, giant step)

爱酱的blog里学来的,是一个很神的根号算法。

如果我们有hash的工具的话,就是O(sqrt(p))的,这里又用了一个map所以是O(sqrt(p) * log(sqrt(p)))

 /**************************************************************
Problem: 3239
User: rausen
Language: C++
Result: Accepted
Time:280 ms
Memory:2932 kb
****************************************************************/ #include <cstdio>
#include <cmath>
#include <map> using namespace std;
typedef long long ll; ll p, y, z, B;
map <ll, int> mp; ll pow(ll a, ll x) {
a %= p;
ll res = , base = a;
while (x) {
if (x & ) (res *= base) %= p;
(base *= base) %= p;
x >>= ;
}
return res;
} int main() {
ll i, now, base, tmp;
while (scanf("%lld%lld%lld", &p, &y, &z) != EOF) {
mp.clear();
y %= p;
if (!y) {
if (!z) puts("");
else puts("no solution");
goto end_of_work;
}
B = (int) ceil(sqrt(p)), now = ;
mp[] = B + ;
for (i = ; i < B; ++i) {
(now *= y) %= p;
if (!mp[now]) mp[now] = i;
}
now = , base = pow(y, p - B - );
for (i = ; i < B; ++i) {
tmp = mp[z * now % p];
if (tmp) {
if (tmp == B + ) tmp = ;
printf("%lld\n", i * B + tmp);
goto end_of_work;
}
(now *= base) %= p;
}
puts("no solution");
end_of_work:;
}
return ;
}

(p.s. bz上开了O2,所以还不是很慢恩!)

BZOJ3239 Discrete Logging的更多相关文章

  1. 【BSGS】BZOJ3239 Discrete Logging

    3239: Discrete Logging Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 729  Solved: 485[Submit][Statu ...

  2. 【BZOJ3239】Discrete Logging BSGS

    [BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...

  3. poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2831   Accepted: 1391 ...

  4. BSGS算法+逆元 POJ 2417 Discrete Logging

    POJ 2417 Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4860   Accept ...

  5. POJ 2417 Discrete Logging (Baby-Step Giant-Step)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2819   Accepted: 1386 ...

  6. POJ2417 Discrete Logging【BSGS】

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5577   Accepted: 2494 ...

  7. [POJ2417]Discrete Logging(指数级同余方程)

    Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...

  8. Discrete Logging

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5865   Accepted: 2618 ...

  9. BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)

    我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...

随机推荐

  1. 设计模式之——浅谈strategy模式(策略模式)

    strategy模式,即策略模式.个人觉得吧,策略模式更多的是一种思维方式. 首先我们要知道,为什么需要策略模式.举个例子,比如用程序输出今天下午去玩什么. PlayGame 玩游戏 package ...

  2. No module named pip.req

    https://stackoverflow.com/questions/25192794/no-module-named-pip-req#

  3. android进入页面会定位到ListView问题解决方法

    在我们的页面中如果存在有ListView,当我们进入这个activity时,页面会定位到ListView的位置去,而不是activity的头部,这是由于ListView会去默认获取焦点所造成的. 解决 ...

  4. grep命令做永久别名 显示颜色

    grep命令做永久别名  显示颜色 http://jingyan.baidu.com/article/22fe7ced17c1543002617f9c.htmlhttp://blog.csdn.net ...

  5. Openstack(十二)部署neuron(计算节点)

    在计算节点安装 12.1安装neuron(计算节点) # yum install openstack-neutron-linuxbridge ebtables ipset –y 12.2配置neutr ...

  6. Miller-Rabin素数测试算法(POJ1811Prime Test)

    题目链接:http://poj.org/problem?id=1811 题目解析:2<=n<2^54,如果n是素数直接输出,否则求N的最小质因数. 求大整数最小质因数的算法没看懂,不打算看 ...

  7. Linux系统——FTP

    FTP连接及传输模式1. 控制连接:TCP21,用于发送FTP命令信息2. 数据连接:TCP20,用于上传.下载数据3. 数据连接的建立类型:(1)主动模式:服务器制动发起数据连接首先由客户端向服务端 ...

  8. php \r \n 和 <br/> \t

    利用\r \n 和 <br/> \t做了个实验,话不多说,看代码就很清楚的知道

  9. 使用 shell 脚本对 Linux 系统和进程资源进行监控

    Shell 简介 Shell 语言对于接触 LINUX 的人来说都比较熟悉,它是系统的用户界面,提供了用户与内核进行交互操作的一种接口.它接收用户输入的命令并把它送入内核去执行.实际上 Shell 是 ...

  10. sysctl.conf文件详解

    本文转自:http://www.cnblogs.com/Rosanna/p/3446557.html 使文件立刻生效命令:/sbin/sysctl -p /proc/sys目录下存放着大多数内核参数, ...