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 ...
随机推荐
- python之ATM
每次做一点就发出来,大神不要嫌重复 2016/11/4 今天来搞ATM,反正逃不了的,说来惭愧,这个作业是我10/4号20天前拿到的,当时是万脸蒙比的,今天又做了一点,现在算是百脸蒙比吧. 一.需求: ...
- Spring实现AOP的4种方式
了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“ ...
- js判断鼠标是否停止移动
本程序实现当鼠标在一个特定的div内悬停n秒时,判断出已经停止移动. 思路: 1.定义全局变量鼠标移动状态imouse,定时器timer.当鼠标在div内移动时,imouse值为1,相反静止时值为0: ...
- 多行图片hover加边框兼容IE7+
问题: 遇到多行多列排列的图片时,hover上去加边框会把下面的图片挤到别处 ============================================================ ...
- Java源码分析之LinkedList
LinkedList与ArrayList正好相对,同样是List的实现类,都有增删改查等方法,但是实现方法跟后者有很大的区别. 先归纳一下LinkedList包含的API 1.构造函数: ①Linke ...
- Reporting Service 服务启动时报错The service did not respond to the start or control request in a timely fashion
案例环境: 启动一台数据库服务器(Windows Server 2003)的Reporting Service(SQL Server 2008 R2)服务时,出现如下错误信息: System.Inva ...
- Python中的绝对路劲和相对路径
1.绝对路径 os.path.abspath("文件名"): 显示的是一个文件的绝对路劲 eg: >>> import os >>> os.ch ...
- SQL范式
第一范式:确保每列的原子性(字段不可分). 如果每列(或者每个属性)都是不可再分的最小数据单元(也称为最小的原子单元),则满足第一范式. 释义: 1.每一列属性都是不可再分的属性值,确保每一列的原子性 ...
- java 单例(懒汉式)
该示例考虑了懒汉式单例的线程安全问题
- mvc页面跳转
用例列举: RedirectToAction: return RedirectToAction("PublicPreview", controllerName, new { id ...