一道裸的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. (1.2)DML增强功能-4大排名函数与top ties/tablesample

    关键字:sql server窗口函数.分析函数.四大窗口函数 1.row_number()  over( partition by column order by column) (1)测试数据 (2 ...

  2. python 面向对象 类 __doc__

    用来打印类的描述信息 class A1(object): '''类的描述信息''' print(A1.__doc__) # 类的描述信息

  3. Executor框架与Thread

    Executor将线程的创建和线程的执行解耦,比较下面两个例子: 1:TaskExecutionWebServer.java package chapter06; import java.io.IOE ...

  4. RDD的基础知识

    以下的这些分析都是基于spark2.1进行的 (一)什么是RDD A Resilient Distributed Dataset (RDD), the basic abstraction in Spa ...

  5. vim多行注释和取消注释 Ubuntu

    多行注释: 1. 进入命令行模式,按ctrl + v进入 visual block模式,然后按d 是选择到最后一行,也可以直接光标上下左右,把需要注释的行标记起来 2. 按大写字母I,再插入注释符,例 ...

  6. 2018 Multi-University Training Contest 2 Solution

    A - Absolute 留坑. B - Counting Permutations 留坑. C - Cover 留坑. D - Game puts("Yes") #include ...

  7. docker 容器目录挂载 | 进出容器

    docker run --name wnginx -d -p 9001:80 -v /home/www:/usr/share/nginx/html nginx --name   别名 -d      ...

  8. laravel 中间件排除

    public function __construct(){ //除了主页之外 $this->middleware('auth', ['except' => ['index','show' ...

  9. IDEA,与gradle引入jar包报错

    Warning:<i><b>root project 'netty_lecture': Unable to resolve additional project configu ...

  10. linux下mysql数据库导入导出命令

    首先linux 下查看mysql相关目录root@ubuntu14:~# whereis mysqlmysql: /usr/bin/mysql----   mysql的运行路径 /etc/mysql ...