题目:https://www.acwing.com/problem/content/226/

题意:有一个计算器能完成三种功能

1、给定Y,Z,P,计算YZModPYZModP 的值;

2、给定Y,Z,P,计算满足xY≡Z(modP)xY≡Z(modP)的最小非负整数;

3、给定Y,Z,P,计算满足Yx≡Z(modP)Yx≡Z(modP)的最小非负整数。

思路:第一种很明显就是个快速幂取模,第二种,因为p是个质数,所以我们可以用快速幂取逆元来计算,第三种就是BSGS的板子

#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
#define ll long long
//快速幂求a^b
LL power(LL a,LL b,LL n)
{
LL s=;
while(b)
{
if(b&)
s=(s*a)%n;
a=(a*a)%n;
b=b>>;
}
return s;
}
ll work2(ll y,ll z,ll p)//xy=z mod p
{
if(y%p==&&z!=) return -;
return z*power(y,p-,p)%p;
}
ll bsgs(ll a,ll b,ll p){
map<ll,ll> hash;
hash.clear();
b%=p;
ll t=(ll)sqrt(p)+;
for(int j=;j<t;j++){
ll val = (ll)b*power(a,j,p)%p;
hash[val]=j;
}
a=power(a,t,p);
if(a==) return b==?:-;
for(int i=;i<=t;i++){
ll val = power(a,i,p);
ll j=hash.find(val)==hash.end()?-:hash[val];
if(j>=&&i*t-j>=) return i*t-j;
}
return -;
}
int main()
{
LL a,b,t,n;
ll op;
scanf("%lld%lld",&t,&op);
for(int i=;i<t;i++){
scanf("%lld%lld%lld",&a,&b,&n);
if(op==){
printf("%lld\n",power(a,b,n));
}
else if(op==){
ll w=work2(a,b,n);
if(w==-) printf("Orz, I cannot find x!\n");
else printf("%lld\n",w);
}
else if(op==){
ll w=bsgs(a,b,n);
if(w==-) printf("Orz, I cannot find x!\n");
else printf("%lld\n",w);
}
}
return ;
}

AcWing 224. 计算器 (BSGS算法)打卡的更多相关文章

  1. bzoj2242: [SDOI2011]计算器 && BSGS 算法

    BSGS算法 给定y.z.p,计算满足yx mod p=z的最小非负整数x.p为质数(没法写数学公式,以下内容用心去感受吧) 设 x = i*m + j. 则 y^(j)≡z∗y^(-i*m)) (m ...

  2. bzoj 2242: [SDOI2011]计算器 & BSGS算法笔记

    这题的主要难点在于第三问该如何解决 于是就要知道BSGS是怎样的一种方法了 首先BSGS是meet in the middle的一种(戳下面看) http://m.blog.csdn.net/blog ...

  3. luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法

    BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...

  4. 【codevs 1565】【SDOI 2011】计算器 快速幂+拓展欧几里得+BSGS算法

    BSGS算法是meet in the middle思想的一种应用,参考Yveh的博客我学会了BSGS的模版和hash表模板,,, 现在才会hash是不是太弱了,,, #include<cmath ...

  5. BSGS算法

    BSGS算法 我是看着\(ppl\)的博客学的,您可以先访问\(ppl\)的博客 Part1 BSGS算法 求解关于\(x\)的方程 \[y^x=z(mod\ p)\] 其中\((y,p)=1\) 做 ...

  6. BSGS算法及扩展

    BSGS算法 \(Baby Step Giant Step\)算法,即大步小步算法,缩写为\(BSGS\) 拔山盖世算法 它是用来解决这样一类问题 \(y^x = z (mod\ p)\),给定\(y ...

  7. BSGS算法及其扩展

    bsgs算法: 我们在逆元里曾经讲到过如何用殴几里得求一个同余方程的整数解.而\(bsgs\)就是用来求一个指数同余方程的最小整数解的:也就是对于\(a^x\equiv b \mod p\) 我们可以 ...

  8. BSGS算法总结

    BSGS算法总结 \(BSGS\)算法(Baby Step Giant Step),即大步小步算法,用于解决这样一个问题: 求\(y^x\equiv z\ (mod\ p)\)的最小正整数解. 前提条 ...

  9. 【BZOJ2242】[SDOI2011]计算器 BSGS

    [BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...

随机推荐

  1. 2018-2019-2 20175307实验三《敏捷开发与XP实践》实验报告

    实验三 敏捷开发与XP实践-1 1.仔细学习了http://www.cnblogs.com/rocedu/p/4795776.html,发布了一篇关于Google的Java编码的博客,具体内容就不在这 ...

  2. Java JsonPath grab InvalidPathException in code, you must be catching Java 7's java.nio.file.InvalidPathException instead of JsonPath's com.jayway.jsonpath.InvalidPathExceptio

    I am using JsonPath and am able to parse my data and get the values when the path provided is correc ...

  3. Apache: No space left on device: Couldn’t create rewrite_map(XXXX)

    启动apache的时候 有时候会遇到这样的错误:No space left on device: Couldn’t create rewrite_map(XXXX) 第一眼看以为是磁盘没有空间了,其实 ...

  4. zabbix真的很简单 (安装篇)

    系统环境: Centos 6.4 一直觉得 zabbix 很简单,但是还是有好多人看了好多文档都搞不明白怎么用,我从2013年使用到现在也小有心得,如果时间允许,很高兴与大家一起分享我在使用过程中的一 ...

  5. vector的自定义实现

    #pragma warning(disable:4996) #include<iostream> #include<string> #include<vector> ...

  6. Day 45 Mysql 数据库练习题二

    1.表关系   注意:创建表时,根据合理性设置字段的长度和类型. 2.下面:开始你的表演 1.查询所有人员信息 select * from ren 2.只查询人员的姓名和年龄 select name, ...

  7. SpringMVC表单验证器

    本章讲解SpringMVC中怎么通过注解对表单参数进行验证. SpringBoot配置 使用springboot,spring-boot-starter-web会自动引入hiberante-valid ...

  8. Oracle学习笔记<5>

    组函数(多值函数) 数据库中函数的分类:1)单值函数 Single Rows Functions 特点:n条数据参与函数处理,最终得到n条结果.2)多值函数(组函数) Multiple Rows Fu ...

  9. A - Max Sum Plus Plus (好题&&dp)

    A - Max Sum Plus Plus  I think you have got an AC in Ignatius.L's "Max Sum" problem. To be ...

  10. mySQL部分疑问和小结(orale)

    2015/10/15 1.mysql语句: ALTER table scfz_xewp add BGR varchar(255) after KYR 2.创建触发器时:  --/   CREATE D ...