洛古题面

对于操作一,用快速幂算即可

代码如下

int quickpow(int a,int b,int k)
{
int r=1;
while(b)
{
if(b&1) r=(r*a)%k;
b>>=1;
a=(a*a)%k;
}
return r;
}

对于操作二,用拓展欧几里得算法即可。

已知\(a,b,n\),求\(x\)的最小值,使得\(a*x≡b(mod p)\),可以转化为:\(a*x+p*y=b\),则要求\(gcd(a,n)|b\),否则无解。不定方程的求法可以参照这道题

\(exgcd\)代码如下

int exgcd(int a,int b,int&x,int&y)
{
if(!b)
{
x=1,y=0;
return a;
}
re int gcd=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return gcd;
}

对于操作三,我们需要用到一个新的算法B(拔)S(山)G(盖)S(世),他可以快速的求出求,满足\(a^x ≡ b(mod p)\)的最小的非负整数\(x\)。

求法是将\(x\)拆分成\(i*m-j\)的形式(其中\(m\)为\(sqrt(p)\)向上取整的值,则原式化为\(a^{i*m-j} ≡ b(mod p)\)。

移向后得\(a^{i*m} ≡ b*a^j(mod p)\)

我们从\(0-m\)枚举\(j\),并将\(b*a^j\)的所有值存入哈希表中

接着在从\(1-m\)枚举\(i\),算出所有的\(a^{i*m}\)

如果一个i对应的\(a^{i*m}\)的值已经在哈希表中,则表明i*m-j为一个解,输出此时的解即可

因为j<=m,所以求出的解随i的增大而减小,所以最先求出的i所对的解,即为所求。

	re int y=read(),z=read(),p=read();
re int m=ceil(sqrt(p));
if(y%p==0&&z)
{
puts("Orz, I cannot find x!");
continue;
}
//这里要特判,因为如果y%p==0了,那么不管x取何值,(y^x)%p一定为0。
a.clear();
re int now=z%p,f=quickpow(y,m,p);
a[now]=0;
for(re int i=1;i<=m;++i)
{
now=(now*y)%p;
a[now]=i;
}
now=1;
re int flag=1;
for(re int i=1;i<=m;++i)
{
now=(now*f)%p;
if(a[now])
{
re int ans=(i*m-a[now])%p;
printf("%lld\n",(ans+p)%p);
flag=0;
break;
}
}
if(flag) puts("Orz, I cannot find x!");

所有代码如下:

#include<bits/stdc++.h>
using namespace std;
#define il inline
#define re register
#define debug printf("Now is Line : %d\n",__LINE__)
#define file(a) freopen(#a".in","r",stdin);freopen(#a".out","w",stdout)
#define int long long
map<int,int>a;
il int read()
{
re int x=0,f=1;re char c=getchar();
while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') x=x*10+c-48,c=getchar();
return x*f;
}
il int quickpow(int a,int b,int k)
{
re int r=1;
while(b)
{
if(b&1) r=(r*a)%k;
b>>=1;
a=(a*a)%k;
}
return r;
}
il int exgcd(int a,int b,int&x,int&y)
{
if(!b)
{
x=1,y=0;
return a;
}
re int gcd=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return gcd;
}
signed main()
{
re int T=read(),k=read();
if(k==1)
{
while(T--)
{
re int y=read(),z=read(),p=read();
printf("%lld\n",quickpow(y,z,p));
}
}
else if(k==2)
{
while(T--)
{
re int a=read(),b=read(),p=read(),x,y;
re int gcd=exgcd(a,p,x,y);
if(b%gcd) puts("Orz, I cannot find x!");
else
{
re int temp=p/gcd;
while(x<0) x+=temp;
printf("%lld\n",((x*b/gcd)%temp+temp)%temp);
}
}
}
else
{
while(T--)
{
re int y=read(),z=read(),p=read();
re int m=ceil(sqrt(p));
if(y%p==0&&z)
{
puts("Orz, I cannot find x!");
continue;
}
a.clear();
re int now=z%p,f=quickpow(y,m,p);
a[now]=0;
for(re int i=1;i<=m;++i)
{
now=(now*y)%p;
a[now]=i;
}
now=1;
re int flag=1;
for(re int i=1;i<=m;++i)
{
now=(now*f)%p;
if(a[now])
{
re int ans=(i*m-a[now])%p;
printf("%lld\n",(ans+p)%p);
flag=0;
break;
}
}
if(flag) puts("Orz, I cannot find x!");
}
}
return 0;
}

[SDOI2011]计算器(BSGS)的更多相关文章

  1. bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德

    2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...

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

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

  3. bzoj2242: [SDOI2011]计算器 BSGS+exgcd

    你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值:(快速幂) 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数:(exgcd) 3.给 ...

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

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

  5. BZOJ 2242 [SDOI2011]计算器 | BSGS

    insert的时候忘了取模了-- #include <cstdio> #include <cmath> #include <cstring> #include &l ...

  6. bzoj 2242 [SDOI2011]计算器——BSGS模板

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...

  7. bzoj2242 [SDOI2011]计算器——BSGS

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一次写BSGS,参考了好多好多博客: 然而看到的讲解和模板是一种写法,这道题的网上题 ...

  8. BZOJ 2242 [SDOI2011]计算器 BSGS+高速幂+EXGCD

    题意:id=2242">链接 方法: BSGS+高速幂+EXGCD 解析: BSGS- 题解同上.. 代码: #include <cmath> #include <c ...

  9. BZOJ 2242 / Luogu P2485 [SDOI2011]计算器 (BSGS)

    type 1type\ 1type 1 就直接快速幂 type 2type\ 2type 2 特判+求逆元就行了. type 3type\ 3type 3 BSGS板 CODE #include< ...

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

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

随机推荐

  1. Non-Volatile Register 非易失性寄存器 调用约定对应寄存器使用

    非易失性寄存器(Non-volatile register)是它的内容必须通过子程序调用被保存的一个寄存器.如果一个程序改变了一个非易失性寄存器的值,它必须保存在改变这个寄存器之前堆栈中保存旧的值和在 ...

  2. toTree

    // js实现树级递归, // 通过js生成tree树形菜单(递归算法) const data = [ { id: 1, name: "办公管理", pid: 0 }, { id: ...

  3. JQuery动态修改样式

    JQuery动态修改样式 SetStyle(); function SetStyle() { $(".toolbar").remove(); $(".placeholde ...

  4. Laravel 门面实例教程 —— 创建自定义 Facades 类

    我们首先创建一个需要绑定到服务容器的Test类: <?php namespace App\Facades; class Test { public function doSomething() ...

  5. 4 Past progressive VS simple past

    1 一般过去时用来谈论过去开始和结束的活动.过去进行时用来谈论过去正在进行或者发生的活动. Why were you at office so later yesterday? I was worki ...

  6. [转帖]你云我云•兄弟夜谈会 第三季 企业IT架构

    你云我云•兄弟夜谈会 第三季 企业IT架构 https://www.cnblogs.com/sammyliu/p/10425252.html 你云我云•兄弟夜谈会 第三季 企业IT架构 你云我云•兄弟 ...

  7. 【学亮开讲】Oracle存储过程教学笔记(二)20181116

    --带出参的存储过程的创建和调用 create or replace procedure pro_owners_add1 ( v_name varchar2,--名称 v_addressid numb ...

  8. cookie,localStorage和sessionStorage区别

    三者的异同 特性 Cookie localStorage sessionStorage 数据的生命期 一般由服务器生成,可设置失效时间.如果在浏览器端生成Cookie,默认是关闭浏览器后失效 除非被清 ...

  9. yum仓库搭建

    1. 创建yum仓库目录 mkdir -p /application/yum/centos6.6/x86_64/ cd /application/yum/centos6.6/x86_64/ rz  # ...

  10. Linux在shell中进入python敲方向键出现「^[[C^[[D」的解决办法

    安装yum -y install readline-devel,然后在重新编译python