BZOJ2242 [SDOI2011]计算器
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。
本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!
Description
Input
输入包含多组数据。
Output
Sample Input
3 1
2 1 3
2 2 3
2 3 3
【样例输入2】
3 2
2 1 3
2 2 3
2 3 3
【数据规模和约定】
对于100%的数据,1<=y,z,p<=10^9,为质数,1<=T<=10。
Sample Output
2
1
2
【样例输出2】
2
1
0
//It is made by ljh2000
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
const int MOD = ;
const int MAXM = ;
int k,p,first[MOD+],ecnt,to[MAXM],w[MAXM],next[MAXM],m,ans;
inline int gcd(int x,int y){ if(y==) return x; return gcd(y,x%y); }
inline int fast_pow(LL x,int y){ LL r=; while(y>) { if(y&) r*=x,r%=p; x*=x; x%=p; y>>=; } return (int)r; }
inline void wujie(){ printf("Orz, I cannot find x!"); }
inline int getint(){
int w=,q=; char c=getchar(); while((c<''||c>'') && c!='-') c=getchar();
if(c=='-') q=,c=getchar(); while (c>=''&&c<='') w=w*+c-'',c=getchar(); return q?-w:w;
} inline void exgcd(LL x,LL y,LL &d,LL &a,LL &b){
if(y==) { d=x; a=; b=; return ; }
exgcd(y,x%y,d,b,a);
b-=x/y*a;
} inline void solve(int a,int Z){
int GCD=gcd(a,p); if(Z%GCD!=) { wujie(); return ; }
LL x,y,GG; exgcd((LL)a,(LL)p,GG,x,y);
Z/=GCD; p/=GCD;
ans=Z*x%p; ans+=p; ans%=p;
printf("%d",ans);
} inline void insert(int x,int j){
int cc=x; x%=MOD; for(int i=first[x];i;i=next[i]) if(to[i]==cc) { w[i]=j; return ;}
next[++ecnt]=first[x]; first[x]=ecnt; to[ecnt]=cc; w[ecnt]=j;
} inline int query(int x){
int cc=x; x%=MOD; for(int i=first[x];i;i=next[i]) if(to[i]==cc) return w[i];
return -;
} inline void BSGS(int a,int b){
if(a%p==) { wujie(); return; }
//if(b==1) { printf("0"); return ; }
ecnt=; memset(first,,sizeof(first));
m=sqrt(p); if(m*m<p) m++; LL cc=b; insert(b,);
for(int i=;i<=m;i++) cc*=a,cc%=p,insert((int)cc,i);
cc=; LL cun=fast_pow(a,m);
for(int i=;i<=m;i++) {
cc*=cun; cc%=p; ans=query(cc);
if(ans==-) continue;
printf("%d",i*m-ans);
return ;
}
wujie();
} inline void work(){
int T=getint(); k=getint(); int x,y;
while(T--) {
x=getint(); y=getint(); p=getint();
if(k==) printf("%d",fast_pow(x,y));
else if(k==) solve(x,y);
else BSGS(x,y);
printf("\n");
}
} int main()
{
work();
return ;
}
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]计算器——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 算法
BSGS算法 给定y.z.p,计算满足yx mod p=z的最小非负整数x.p为质数(没法写数学公式,以下内容用心去感受吧) 设 x = i*m + j. 则 y^(j)≡z∗y^(-i*m)) (m ...
- 2018.12.18 bzoj2242: [SDOI2011]计算器(数论)
传送门 数论基础题. 对于第一种情况用快速幂,第二种用exgcdexgcdexgcd,第三种用bsgsbsgsbsgs 于是自己瞎yyyyyy了一个bsgsbsgsbsgs的板子(不知道是不是数据水了 ...
- bzoj千题计划246:bzoj2242: [SDOI2011]计算器
http://www.lydsy.com/JudgeOnline/problem.php?id=2242 #include<map> #include<cmath> #incl ...
随机推荐
- GConf error:Failed to contact configuration server
Linux系统运行一直正常,但是图形界面使用root账号登录时遇到下面错误,第一次遇到这么怪异的状况 具体错误信息如下所示: GConf error:Failed to contact configu ...
- JVM之运行时常量池(Runtime Constant Pool)
基本特性: 方法区的一部分,在方法去中分配,加载泪或者接口后就创建运行时常量区. class文件每一个类或接口的常量池表(constant_pool table)的运行时表现形式, 包括编译期的数值字 ...
- Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理7
做完角色之后接下来做先做页面按钮的增加.删除.修改.这里用到的功能和角色那边是一样的.就不多说了.直接上代码. 后台控制器代码 using AuthorDesign.Web.App_Start.Com ...
- js中的prototype和__proto__
var Person = function(name){ this.name = name; this.say = function(){ return "I am " + thi ...
- 实战:rsync+inotify实现数据实时同步
Linux 内核从 2.6.13 版本开始提供了 inotify 通知接口,用来监控文件系统的各种变化情况,如文件存取.删除.移动等.利用这一机制,可以非常方便地实现文件异动告警.增量备份,并针对目录 ...
- RedHat6.2搭建FTP服务器
我的环境: A:Red Hat Enterprise 6.2 IP:192.168.16.12 此机作测试端 B:Red Hat Enterprise 6.2 IP:192.168.16.13 此机做 ...
- [转]Python 字符串操作实现代码(截取/替换/查找/分割)
原文地址:http://www.jb51.net/article/38102.htm ps:好久没更新python代码了,这次用到了字符串,转来看看 Python 截取字符串使用 变量[头下标:尾下标 ...
- [转]推荐highcharts学习网址
highcharts学习网址1:http://www.hcharts.cn/docs/index.php?doc=basic(百度highcharts中文教程即可) highcharts学习网址2:h ...
- Python版本2.7切3.5和3.5切2.7
在Ubuntu上是自带Python2.7和3.5的 当你在终端输入Python的时候是显示Python2.7的 叫大家你怎么切换到Python3.5版本 1,查看是否存在python3.5终端输入 c ...
- NOIP2013pj车站分级[拓扑排序]
题目描述 一条单向的铁路线上,依次有编号为 1, 2, …, n 的 n 个火车站.每个火车站都有一个级 别,最低为 1 级.现有若干趟车次在这条线路上行驶,每一趟都满足如下要求:如果这趟车 次停靠了 ...