【洛谷 P2485】 [SDOI2011]计算器 (BSGS)
题目链接
第一问:快速幂
第二问:扩欧解线性同余方程
第三问:\(BSGS\)
三个模板
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
int a, b, p;
ll x, y, z;
ll exgcd(ll a, ll b, ll &x, ll &y){
if(!b){
x = 1; y = 0; return a;
}
ll d = exgcd(b, a % b, x, y);
ll z = x; x = y; y = z - a / b * y;
return d;
}
int fast_pow(int n, int k){ //n^k%p
int ans = 1;
while(k){
if(k & 1) ans = (ll)ans * n % p;
n = (ll)n * n % p;
k >>= 1;
}
return ans;
}
ll BSGS(){ //a^x≡b(mod p)
map <ll, ll> hash; hash.clear();
int t = ceil(sqrt(p)), val = b, j = 1;
for(int i = 0; i < t; ++i){
hash[val] = i;
val = (ll)val * a % p;
}
a = fast_pow(a, t);
if(!a) return !b ? 1 : -1;
for(int i = 0; i <= t; ++i){
int k = hash.find(j) == hash.end() ? -1 : hash[j];
if(k >= 0 && (i * t - k) >= 0) return (ll)i * t - k;
j = (ll)j * a % p;
}
return -1;
}
int T, K;
int main(){
scanf("%d%d", &T, &K);
if(K == 3)
while(T--){
scanf("%d%d%d", &a, &b, &p);
int ans = BSGS();
ans == -1 ? puts("Orz, I cannot find x!") : printf("%d\n", ans);
}
else if(K == 1)
while(T--){
scanf("%d%d%d", &a, &b, &p);
printf("%d\n", fast_pow(a, b));
}
else
while(T--){
scanf("%d%d%d", &a, &b, &p);
if(b % (z = exgcd(a, p, x, y))) puts("Orz, I cannot find x!");
else printf("%d\n", ((ll)x * b / z % p + p) % p);
}
return 0;
}
【洛谷 P2485】 [SDOI2011]计算器 (BSGS)的更多相关文章
- 洛谷 P2485 [SDOI2011]计算器 解题报告
P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...
- 洛谷P2485 [SDOI2011]计算器(exgcd+BSGS)
传送门 一题更比三题强 1操作直接裸的快速幂 2操作用exgcd求出最小正整数解 3操作用BSGS硬上 然后没有然后了 //minamoto #include<cstdio> #inclu ...
- 【洛谷P2485】计算器
BSGS模板题 代码如下 #include <bits/stdc++.h> using namespace std; typedef long long LL; LL fpow(LL a, ...
- BZOJ 2242 / Luogu P2485 [SDOI2011]计算器 (BSGS)
type 1type\ 1type 1 就直接快速幂 type 2type\ 2type 2 特判+求逆元就行了. type 3type\ 3type 3 BSGS板 CODE #include< ...
- P2485 [SDOI2011]计算器
P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
- [洛谷P2491] [SDOI2011]消防
洛谷题目链接:[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超 ...
- 【BZOJ2242】[SDOI2011]计算器 BSGS
[BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...
- BZOJ2243 洛谷2486 [SDOI2011]染色 树链剖分
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2243 题目传送门 - 洛谷2486 题意概括 一棵树,共n个节点. 让你支持以下两种操作,共m次操 ...
- 洛谷 P2495 [SDOI2011]消耗战(虚树,dp)
题面 洛谷 题解 虚树+dp 关于虚树 了解一下 具体实现 inline void insert(int x) { if (top == 1) {s[++top] = x; return ;} int ...
随机推荐
- App间常用的五种通信方式
1.URL Scheme 2.Keychain 3.UIPasteboard剪切板 4.UIDocumentInteractionController 5.local socket 详见: 转自:ht ...
- Windows模拟linux终端工具Cmder+Gow
1. 说明 Cmder:Windows下的终端模拟器. Gow: Windows下模拟Linux命令行工具集合.可以在windows执行linux下的大部分命令,如ls.grep.xargs等. 2. ...
- Python 3 学习笔记之——错误和异常
1. 语法错误 Python 的语法错误被称为解析错,语法分析器会指出出错的代码行,并且在最先找到的错误的位置标记一个小小的箭头. >>> while True File " ...
- Python之tornado框架实现翻页功能
1.结果如图所示,这里将html页面与网站的请求处理放在不同地方了 start.py代码 import tornado.ioloop import tornado.web from controlle ...
- 使用HashOperations操作redis
方法 c参数 s说明 Long delete(H key, Object... hashKeys); H key:集合key Object... hashKeys:key对应hashkey 删除ma ...
- 【转】C++操作符的优先级 及其记忆方法
优先级 操作符 描述 例子 结合性 1 ()[]->.::++-- 调节优先级的括号操作符数组下标访问操作符通过指向对象的指针访问成员的操作符通过对象本身访问成员的操作符作用域操作符后置自增操作 ...
- HDU 1798 Tell me the area
http://acm.hdu.edu.cn/showproblem.php?pid=1798 Problem Description There are two circles in the ...
- 入口文件-npm run dev
如果你是用vue.js官网提供的脚手架工具并沿用默认配置的话,你执行npm run dev的时候会出来页面,是因为你根目录下的package.json文件里script配置了"dev&quo ...
- 数组中键key相等时,后面的值覆盖前面的值
<?php $arr[]='abc'; $arr[]='; $arr[]='; $arr[]='; var_dump($arr); 结果;
- 【题解】POI2014FAR-FarmCraft
这题首先手玩一下一下数据,写出每个节点修建软件所需要的时间和到达它的时间戳(第一次到达它的时间),不难发现实际上就是要最小化这两者之和.然后就想到:一棵子树内,时间戳必然是连续的一段区间,而如果将访问 ...