本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

本文作者:ljh2000

作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

Description

你被要求设计一个计算器完成以下三项任务:
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)的最小非负整数。

Input

输入包含多组数据。

第一行包含两个正整数T,K分别表示数据组数和询问类型(对于一个测试点内的所有数据,询问类型相同)。
以下行每行包含三个正整数y,z,p,描述一个询问。

Output

对于每个询问,输出一行答案。对于询问类型2和3,如果不存在满足条件的,则输出“Orz, I cannot find x!”,注意逗号与“I”之间有一个空格。

Sample Input

【样例输入1】
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

【样例输出1】
2
1
2
【样例输出2】
2
1
0
 
 
正解:快速幂+exgcd+BSGS
解题报告:
  快速幂+exgcd+BSGS。
  有一些细节。exgcd都快忘了......
  学习BSGS戳这里:http://www.cnblogs.com/ljh2000-jump/p/6230999.html
  
  

 //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]计算器的更多相关文章

  1. [bzoj2242][Sdoi2011]计算器_exgcd_BSGS

    计算器 bzoj-2242 Sdoi-2011 题目大意:裸题,支持快速幂.扩展gcd.拔山盖世 注释:所有数据保证int,10组数据. 想法:裸题,就是注意一下exgcd别敲错... ... 最后, ...

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

    2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 4741  Solved: 1796 [Submit][Sta ...

  3. BZOJ2242[SDOI2011]计算器——exgcd+BSGS

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

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

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

  5. 【数学 BSGS】bzoj2242: [SDOI2011]计算器

    数论的板子集合…… Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最 ...

  6. [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.给 ...

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

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

  8. 2018.12.18 bzoj2242: [SDOI2011]计算器(数论)

    传送门 数论基础题. 对于第一种情况用快速幂,第二种用exgcdexgcdexgcd,第三种用bsgsbsgsbsgs 于是自己瞎yyyyyy了一个bsgsbsgsbsgs的板子(不知道是不是数据水了 ...

  9. bzoj千题计划246:bzoj2242: [SDOI2011]计算器

    http://www.lydsy.com/JudgeOnline/problem.php?id=2242 #include<map> #include<cmath> #incl ...

随机推荐

  1. Windows 10 IoT Serials 1 - 针对Minnow Board MAX的Windows 10 IoT开发环境搭建

    目前,微软针对Windows IoT计划支持的硬件包括树莓派2,Minnow Board MAX 和Galileo (Gen 1和Gen 2).其中,Galileo (Gen 1和Gen 2)运行的是 ...

  2. RedHat 4下无resize2fs命令

    在Red Hat Enterprise Linux AS release 4上进行LVM扩展分区时,发现RedHat 4下没有resize2fs,不过可以用ext2online替换resize2fs. ...

  3. CentOS系统 yum 安装 iftop实时流量监控工具

    一. 不多说其他的无用话题.直接看步骤: 1.要安装某个工具,首先得知道这个工具需要依赖包. eg:iftop 就依赖 以下这几个依赖包(不清楚工具依赖什么包“Google.百度”). flex   ...

  4. python数据库操作对主机批量管理

    import paramiko import MySQLdb conn = MySQLdb.connect(host=',db='host') cur = conn.cursor(cursorclas ...

  5. js中数组遍历for与for in区别(强烈建议不要使用for in遍历数组)

    js中遍历数组的有两种方式 var array=['a'] //标准的for循环 for(var i=1;i<array.length;i++){ alert(array[i]) } //for ...

  6. OpenStack 企业私有云的若干需求(10):OpenStack 的前景和钱景

    本系列会介绍OpenStack 企业私有云的几个需求: 自动扩展(Auto-scaling)支持 多租户和租户隔离 (multi-tenancy and tenancy isolation) 混合云( ...

  7. Hibernate延迟加载、三种状态、脏检查 缓存

    一.持久化对象的唯一标识 java中按内存地址不同区分同一个类的不同对象,关系数据库用主键区分同一条记录,Hibernate使用OID来建立内存中的对象和数据库中记录的对应关系 什么是OID? 解析: ...

  8. 如何进行安全测试-XSS篇

    XSS分为三类:Stored XSS.Reflected XSS.Dom-Base XSS (1)Stored XSS,即存储式跨站攻击,存储式跨站攻击简单来说就是攻击者提交给网站的数据会提交并永久保 ...

  9. ubuntu14.04为安装fcitx卸载ibus后出现system setting (系统设置)中图标消失的问题

    最近在系统为ubuntu14.04原版中,安装fictx.按照以往的经验应先把ibus卸载干净,否则可能会有冲突.因此惯性思维驱使,先卸载ibus,然后安装fcitx,但是问题出现了,system s ...

  10. sublime text nodejs set

    把新建的system清空,输入{ "cmd": ["node", "$file"], "selector": " ...