BZOJ 2242 / Luogu P2485 [SDOI2011]计算器 (BSGS)
type 1type\ 1type 1 就直接快速幂
type 2type\ 2type 2 特判+求逆元就行了.
type 3type\ 3type 3 BSGS板
CODE
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
inline LL qpow(LL a, LL b, LL c) {
LL re = 1;
while(b) {
if(b & 1) re = re * a % c;
a = a * a % c; b >>= 1;
}
return re;
}
int gcd(int a, int b) { return b ? gcd(b, a%b) : a; }
map<int, int>myhash;
inline int Baby_Step_Giant_Step(int a, int b, int p) {
if(p == 1) return 0;
a %= p, b %= p;
if(b == 1) return 0;
int cnt = 0; LL tmp = 1;
for(int g = gcd(a, p); g != 1; g = gcd(a, p)) {
if(b % g) return -1;
b /= g, p /= g, tmp = tmp * (a/g) % p;
++cnt;
if(b == tmp) return cnt;
}
myhash.clear();
int m = int(sqrt(p) + 1);
LL base = b;
for(int i = 0; i < m; ++i) {
myhash[base] = i;
base = base * a % p;
}
base = qpow(a, m, p);
for(int i = 1; i <= m+1; ++i) {
tmp = tmp * base % p;
if(myhash.count(tmp))
return i*m - myhash[tmp] + cnt;
}
return -1;
}
inline void solve1(int a, int b, int p) {
printf("%d\n", qpow(a, b, p));
}
inline void solve2(int a, int b, int p) {
a %= p, b %= p;
if(!a) {
if(!b) puts("0");
else puts("Orz, I cannot find x!");
}
else printf("%d\n", int(1ll * b * qpow(a, p-2, p) % p));
}
inline void solve3(int a, int b, int p) {
int ans = Baby_Step_Giant_Step(a, b, p);
if(~ans) printf("%d\n", ans);
else puts("Orz, I cannot find x!");
}
int main() {
int a, b, p, T, type;
scanf("%d%d", &T, &type);
while(T--) {
scanf("%d%d%d", &a, &b, &p);
if(type == 1) solve1(a, b, p);
else if(type == 2) solve2(a, b, p);
else solve3(a, b, p);
}
}
BZOJ 2242 / Luogu P2485 [SDOI2011]计算器 (BSGS)的更多相关文章
- luogu P2485 [SDOI2011]计算器
题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最小非负整数x: 3.给定y.z.p,计算 ...
- 洛谷 P2485 [SDOI2011]计算器 解题报告
P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...
- 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 你被 ...
- 【BZOJ2242】[SDOI2011]计算器 BSGS
[BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...
- bzoj 2242 [SDOI2011]计算器——BSGS模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...
- BZOJ 2242 [SDOI2011]计算器 BSGS+高速幂+EXGCD
题意:id=2242">链接 方法: BSGS+高速幂+EXGCD 解析: BSGS- 题解同上.. 代码: #include <cmath> #include <c ...
- BZOJ 2242 [SDOI2011]计算器 | BSGS
insert的时候忘了取模了-- #include <cstdio> #include <cmath> #include <cstring> #include &l ...
- bzoj 2242: [SDOI2011]计算器 & BSGS算法笔记
这题的主要难点在于第三问该如何解决 于是就要知道BSGS是怎样的一种方法了 首先BSGS是meet in the middle的一种(戳下面看) http://m.blog.csdn.net/blog ...
随机推荐
- Design HashMap
Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...
- Linux系列(11):之bash进阶与数据流导向、管线命令
1.万用字符与特殊字符 在bash的环境下有一个特别有用的功能,那就是万用字符!下面展示常见的万用字符: 1.万用字符 除了万用字符还有一些其他的特殊字符,接下来显示特殊字符. 2.特殊字符 2.数据 ...
- OpenVZ平台 Google BBR加速
前言 一直以来用的都是搬瓦工的VPS,不得不说比国内那些大厂的性价比高得不知道哪里去了. 当做梯子来用的话搬瓦工年付19.9美元的方案就够用了,网上还有一些官方优惠码(折扣6%: BWH1ZBPV ...
- pyton 类(4) 静态方法
class Cat(): tag = ' 猫科动物 ' def __init__(self, name): self.name = name @staticmethod def breah(): pr ...
- linux环境下Python搭建
安装gcc yum install gcc 安装zlib yum -y install zlib* 安装openssl-devel yum install openssl-devel -y ...
- 树莓派驱动开发 helloworld
编写Makefile ifneq ($(KERNELRELEASE),) obj-m := MiniX.o else KDIR := /home/hi/pi/kernel/linux/ all: ma ...
- java:线上问题排查常用手段(转)
出处:java:线上问题排查常用手段 一.jmap找出占用内存较大的实例 先给个示例代码: import java.util.ArrayList; import java.util.List; imp ...
- charles 抓包 (一)
在web.app开发中经常需要通过抓包来定位页面.接口返回数据的问题.在mac系统中,charles是一款功能丰富的抓包软件.可以实现app的数据抓包. 工具:charles 附送charles的破解 ...
- [转载]VS2005的工程用VS2010打开后,用VS2005不能打开的解决方法
首先,在“项目”菜单里,把项目属性“目标平台”改为框架2.0,保存退出. 然后,用记事本或用编辑文本文件的方式打开你的项目文件,后缀为.sln 第一行:把“Microsoft Visual Studi ...
- LeetCode 腾讯精选50题--数组中的第K个最大元素
好吧,不得不承认,书上看到的始终不是自己的,只有亲身时间过才会明白该怎么操作. 找数组中第K个最大元素,简而言之就是先排序,不论使用哪种算法,都需要先排序,确认位置,由于数组可以通过下标直接访问,所以 ...