BZOJ2242:[SDOI2011]计算器——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2242
https://www.luogu.org/problemnew/show/P2485
你被要求设计一个计算器完成以下三项任务:1、给定y,z,p,计算Y^Z Mod P 的值;2、给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数;3、给定y,z,p,计算满足Y^x ≡ Z ( mod P)的最小非负整数。
大杂烩题:
1操作快速幂。
2操作把y除过去乘法逆元求一下就能出x了,注意判断是否存在乘法逆元。
3操作就是POJ3243:Clever Y了。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<cmath>
#include<stack>
using namespace std;
typedef long long ll;
const int N=;
const int M=;
struct HASH{
int w,to,nxt;
}h[M];
int cnt,head[N];
inline void add(int x,int y){
int t=x%N;
for(int i=head[t];i;i=h[i].nxt){
int v=h[i].to;
if(v==x){
h[i].w=y;//记大的
return;
}
}
h[++cnt].to=x;h[cnt].w=y;h[cnt].nxt=head[t];head[t]=cnt;
}
inline int query(int x){
int t=x%N;
for(int i=head[t];i;i=h[i].nxt){
int v=h[i].to;
if(v==x)return h[i].w;
}
return -;
}
int gcd(int a,int b){
return (!b)?a:gcd(b,a%b);
}
int exgcd(int a,int b,int &x,int &y){
if(!b){
x=,y=;
return a;
}
int ans=exgcd(b,a%b,y,x);
y-=(ll)(a/b)*x;
return ans;
}
int inv(int a,int c){
int x,y;
exgcd(a,c,x,y);
return (x%c+c)%c;
}
//a^x=b(mod c);
int BSGS(int a,int b,int c){
if(!a){
if(!b)return ;
return -;
}
int tot=,g,d=;
while((g=gcd(a,c))!=){
if(b%g)return -;
++tot;b/=g,c/=g;
d=(ll)d*(a/g)%c;
}
b=(ll)b*inv(d,c)%c;
cnt=;memset(head,,sizeof(head));
int s=sqrt(c),p=;
for(int i=;i<s;i++){
if(p==b)return i+tot;
add((ll)p*b%c,i);
p=(ll)p*a%c;
}
int q=p;
for(int i=s;i-s+<c;i+=s){
int t=query(q);
if(t!=-)return i-t+tot;
q=(ll)q*p%c;
}
return -;
}
int qpow(int k,int n,int p){
int res=;
while(n){
if(n&)res=(ll)res*k%p;
k=(ll)k*k%p;
n>>=;
}
return res;
}
int main(){
int T,k;
scanf("%d%d",&T,&k);
while(T--){
int y,z,p;
scanf("%d%d%d",&y,&z,&p);
if(k==)printf("%d\n",qpow(y,z,p));
if(k==){
if(y%p==)puts("Orz, I cannot find x!");
else printf("%lld\n",(ll)z*qpow(y,p-,p)%p);
}
if(k==){
y%=p,z%=p;
int ans=BSGS(y,z,p);
if(ans==-)puts("Orz, I cannot find x!");
else printf("%d\n",ans);
}
}
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/ +
+++++++++++++++++++++++++++++++++++++++++++
BZOJ2242:[SDOI2011]计算器——题解的更多相关文章
- [bzoj2242][Sdoi2011]计算器_exgcd_BSGS
计算器 bzoj-2242 Sdoi-2011 题目大意:裸题,支持快速幂.扩展gcd.拔山盖世 注释:所有数据保证int,10组数据. 想法:裸题,就是注意一下exgcd别敲错... ... 最后, ...
- BZOJ2242 [SDOI2011]计算器 【BSGS】
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 4741 Solved: 1796 [Submit][Sta ...
- BZOJ2242 [SDOI2011]计算器
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- BZOJ2242[SDOI2011]计算器——exgcd+BSGS
题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给定y,z,p, ...
- bzoj2242: [SDOI2011]计算器 BSGS+exgcd
你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值:(快速幂) 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数:(exgcd) 3.给 ...
- 【数学 BSGS】bzoj2242: [SDOI2011]计算器
数论的板子集合…… Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最 ...
- [bzoj2242][SDOI2011][计算器] (Baby-Step-Giant-Step+快速幂+exgcd)
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- bzoj2242 [SDOI2011]计算器——BSGS
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一次写BSGS,参考了好多好多博客: 然而看到的讲解和模板是一种写法,这道题的网上题 ...
- bzoj2242: [SDOI2011]计算器 && BSGS 算法
BSGS算法 给定y.z.p,计算满足yx mod p=z的最小非负整数x.p为质数(没法写数学公式,以下内容用心去感受吧) 设 x = i*m + j. 则 y^(j)≡z∗y^(-i*m)) (m ...
随机推荐
- uvaoj 489 - Hangman Judge(逻辑+写代码能力)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 小程序解析html和富文本编辑内容【亲测有效】
首先去 https://github.com/icindy/wxParse 下载wxParse,只拷贝wxParse文件夹即可. 1.引入wxss @import "../../util/w ...
- 【转载】appium 操作汇总
'''.appium api第二弹 锋利的python,这是初稿,2015/1/5 如有错误的地方,请同学们进行留言,我会及时予以修改,尽量整合一份ok的api 作者:Mads Spiral QQ:7 ...
- Educational Codeforces Round 32 Problem 888C - K-Dominant Character
1) Link to the problem: http://codeforces.com/contest/888/problem/C 2) Description: You are given a ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- 使用树莓派实现(山寨)高清视频叠加(HDMI OSD)
项目需要在HDMI上叠加一些字符包括汉字和数值,要求不能使用台式机,本身也没有HDMI采集卡驱动开发能力,所以通过海思的HDMI编码器将HDMI编码为h.264网络视频流,然后通过树莓派解码显示,做字 ...
- ThinkPHP - 5 - 学习笔记(2015.4.15)
ThinkPHP __construct()和__initialize() 1.__initialize()不是php类中的函数,php类的构造函数只有__construct().2.类的初始化:子类 ...
- Python中的Comprehensions和Generations
Python中的Comprehensions和Generations语法都是用来迭代的.Comprehensions语法可用于list,set,dictionary上,而Generations语法分为 ...
- C语言实验——时间间隔
Description 从键盘输入两个时间点(24小时制),输出两个时间点之间的时间间隔,时间间隔用“小时:分钟:秒”表示. 如:3点5分25秒应表示为--03:05:25.假设两个时间在同一天内,时 ...
- Android UI 设计之 TextView EditText 组件属性方法最详细解析
. 作者 :万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/18964835 . TextView 相关类的继承结构 ...