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 ...
随机推荐
- BandingList 泛型集合数据绑定
public IList<Student> IStudent = new List<Student>(); public BindingList<Student> ...
- PAT A1058 A+B in Hogwarts (20)
AC代码 #include <cstdio> struct Money { long long Galleon, Sicklke, Knut; }A, B, Sum; void init( ...
- PAT B1046.猜拳
课本AC #include <cstdio> int main() { int n, failA = 0, failB = 0; scanf("%d", &n) ...
- # [洛谷1337] 吊打XXX/平衡点 (模拟退火)
[洛谷1337] 吊打XXX/平衡点 (模拟退火) 题意 n个重物(x,y,w),求平衡时x的位置(x,y) 分析 模拟退火基础题,基于随机数的优化算法,时间复杂度玄学,参数玄学,能不能AC看脸,当然 ...
- RESTful、共用接口、前后端分离、接口约定的实践 (转)
出处: 某小公司RESTful.共用接口.前后端分离.接口约定的实践 前言 随着互联网高速发展,公司对项目开发周期不断缩短,我们面对各种需求,使用原有对接方式,各端已经很难快速应对各种需求,更难以提 ...
- Socket-实例
import socket,os,time server = socket.socket() server.bind(("localhost",9999)) server.list ...
- nginx-consul-template
概述Consul-template 是 HashiCorp 基于 Consul 所提供的可扩展的工具,通过监听 Consul中的数据变化,动态地修改一些配置文件中地模板.常用于在 Nginx.HAPr ...
- Tomcat 的部署器
要使用一个Web应用程序,必须要将表示该应用程序的Context实例部署到一个Host实例中,在Tomcat中,Context实例可以用WAR文件的形式来部署,也可以将整个WEB应用程序复制到Tomc ...
- 电脑主板插线方法图解_JFP1主板插线图解
电脑主板插线方法图解_JFP1主板插线图 仔细看主板上有对应的英文标识的,一对一插就行分别是电源,复位,硬盘灯,电源灯的负极,正极
- div布局(持续更新)
1. 效果: 代码: <!DOCTYPE html> <html> <head> <meta name="viewport" conten ...