【洛谷 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 ...
随机推荐
- iOS-Debug Symbol(调试符号)
Debug Symbol(调试符号) 编译警告 从svn下载下来的文件,到处都是编译警告,看着不爽,找下原因,没想到还是一条大鱼 warning: (i386) /UsersLibrary/Devel ...
- Linux环境搭建系列之sorl服务器的安装部署
http://blog.csdn.net/upxiaofeng/article/details/51425732
- K8s集群内热改代码
1.登录到k8s master服务器 $ ssh developer@XXX.XXX.X.XXX(IP地址) 2.查看服务容器所在的节点(以xx-server为例) $ kubectl get pod ...
- Page Object 设计模式介绍
Page Object 是 Selenium 自动化测试项目开发实践的最佳设计模式之一,Page Object 的主要体现于对界面交互细节的封装,这样可以使测试案例更关注与业务而非界面细节,提高测试案 ...
- 不得不服!Python速度虽然慢,但是它工作效率很高!
写在前面 让我们来讨论一个我最近一直在思考的问题:Python 的性能.顺便说一下,我是 Python 的忠实拥趸,我在各种情况下都会积极尝试使用 Python 来解决问题.大家对 Python 最大 ...
- 七天入门C++
- CF 55D
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer numb ...
- POJ 2229 递推
Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...
- NO7——二分
int binsearch(int *t,int k,int n) {//t为数组,k是要查找的数,n为长度,此为升序 ,high = n,mid; while(low<=high) { mid ...
- PAT 1086 就不告诉你
https://pintia.cn/problem-sets/994805260223102976/problems/1038429065476579328 做作业的时候,邻座的小盆友问你:“五乘以七 ...