BZOJ3239 Discrete Logging
一道裸的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的更多相关文章
- 【BSGS】BZOJ3239 Discrete Logging
3239: Discrete Logging Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 729 Solved: 485[Submit][Statu ...
- 【BZOJ3239】Discrete Logging BSGS
[BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...
- poj 2417 Discrete Logging ---高次同余第一种类型。babystep_gaint_step
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2831 Accepted: 1391 ...
- BSGS算法+逆元 POJ 2417 Discrete Logging
POJ 2417 Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 4860 Accept ...
- POJ 2417 Discrete Logging (Baby-Step Giant-Step)
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 2819 Accepted: 1386 ...
- POJ2417 Discrete Logging【BSGS】
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5577 Accepted: 2494 ...
- [POJ2417]Discrete Logging(指数级同余方程)
Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...
- Discrete Logging
Discrete Logging Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5865 Accepted: 2618 ...
- BSGS 扩展大步小步法解决离散对数问题 (BZOJ 3239: Discrete Logging// 2480: Spoj3105 Mod)
我先转为敬? orz% miskcoo 贴板子 BZOJ 3239: Discrete Logging//2480: Spoj3105 Mod(两道题输入不同,我这里只贴了3239的代码) CODE ...
随机推荐
- TA-Lib中文文档(二):talib安装
安装 使用pip安装 PyPI: $ pip install TA-Lib Or checkout the sources and run setup.py yourself: $ python se ...
- Buy Tickets---poj2828(线段树)
题目链接:http://poj.org/problem?id=2828 题意就是有n个x y每次都是把y放到x位置,如果x位置有数,则把该位置之后的数往后放一位: [题解]: 线段树节点中保存这一段中 ...
- Web开发:URL编码与解码(转)
原文:http://www.cnblogs.com/greatverve/archive/2011/12/12/URL-Encoding-Decoding.html 通常如果一样东西需要编码,说明这样 ...
- 【selenium】selenium ide的安装过程
简介一:SeleniumIDE安装 1.安装seleniumIDE,打开火狐浏览器,地址栏输入地址—>点击Add按钮—>安装结束后重启FireFox—>在菜单栏中可以看到Seleni ...
- sql server 里的文件和文件组使用
转自:https://www.cnblogs.com/woodytu/p/5821827.html 参考:https://www.sqlskills.com/blogs/paul/files-and- ...
- centos samba/squid 配置 samba配置 smbclient mount fstab自动挂载samba curl -xlocalhost:3128 www.qq.com squid配置 3128 DNSPOD 第二十七节课
centos samba/squid 配置 samba配置 smbclient mount fstab自动挂载samba curl -xlocalhost:3128 www.qq.com squ ...
- 当IDENTITY_INSERT设置为OFF时不能向表插入显示值。(源:MSSQLServer,错误码:544)
错误提示"事务和快照同步时提示:当IDENTITY_INSERT设置为OFF时不能向表插入显示值.(源:MSSQLServer,错误码:544)" 原因:在SQL2008同步时到S ...
- The adidas NMD Singapore is one of the brands top selling
Like pointed out, we've two adidas NMD Singapore releases using the first arriving Blue and Black as ...
- php array_map array_filter sort
array_map — Applies the callback to the elements of the given arrays (处理映射) array_filter — Filters e ...
- 【深入理解JVM】:类加载器与双亲委派模型
类加载器 加载类的开放性 类加载器(ClassLoader)是Java语言的一项创新,也是Java流行的一个重要原因.在类加载的第一阶段“加载”过程中,需要通过一个类的全限定名来获取定义此类的二进制字 ...