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

1.快速幂

2.拓展欧几里德解线性方程

3.BSGS

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long lol;
int MOD=;
lol hash[],id[];
void insert(lol x,lol d)
{
lol pos=x%MOD;
while ()
{
if (hash[pos]==-||hash[pos]==x)
{
hash[pos]=x;
id[pos]=d;
return;
}
pos++;
if (pos>=MOD) pos-=MOD;
}
}
bool count(lol x)
{
lol pos=x%MOD;
while ()
{
if (hash[pos]==-) return ;
if (hash[pos]==x) return ;
pos++;
if (pos>=MOD) pos-=MOD;
}
}
lol query(lol x)
{
lol pos=x%MOD;
while ()
{
if (hash[pos]==x) return id[pos];
pos++;
if (pos>=MOD) pos-=MOD;
}
}
lol qpow(lol x,lol y,lol Mod)
{
lol res=;
while (y)
{
if (y&) res=res*x%Mod;
x=x*x%Mod;
y>>=;
}
return res;
}
lol exgcd(lol a,lol b,lol &x,lol &y)
{
if (!b)
{
x=;y=;
return a;
}
lol d=exgcd(b,a%b,x,y);
lol t=x;x=y;y=t-a/b*y;
return d;
}
lol BSGS(lol a,lol b,lol Mod)
{lol i;
if (b==) return ;
if (a==&&b!=) return -;
memset(hash,-,sizeof(hash));
memset(id,,sizeof(id));
lol tim=sqrt((double)Mod);
lol tmp=b%Mod;
for (i=;i<=tim;i++)
{
insert(tmp,i);
tmp=tmp*a%Mod;
}
lol t=tmp=qpow(a,tim,Mod);
for (i=;i<=tim;i++)
{
if (count(tmp))
return i*tim-query(tmp);
tmp=tmp*t%Mod;
}
return -;
}
int main()
{int T,k,i;
lol x,y,p,ans;
while (cin>>T>>k)
{
for (i=;i<=T;i++)
{
scanf("%lld%lld%lld",&x,&y,&p);
if (k==)
{
printf("%lld\n",qpow(x,y,p));
}
else if (k==)
{
lol a,b;
lol d=exgcd(x,p,a,b);
if (y%d) printf("Orz, I cannot find x!\n");
else
{
lol t=y/d;
a=a*t;
d=p/d;
printf("%lld\n",(a%d+d)%d);
}
}
else if (k==)
{
ans=BSGS(x%p,y%p,p);
if (ans==-) printf("Orz, I cannot find x!\n");
else printf("%lld\n",ans);
}
}
}
}

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

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

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

  2. BZOJ 2242: [SDOI2011]计算器( 快速幂 + 扩展欧几里德 + BSGS )

    没什么好说的... --------------------------------------------------------------------- #include<cstdio&g ...

  3. BZOJ 2242: [SDOI2011]计算器 [快速幂 BSGS]

    2242: [SDOI2011]计算器 题意:求\(a^b \mod p,\ ax \equiv b \mod p,\ a^x \equiv b \mod p\),p是质数 这种裸题我竟然WA了好多次 ...

  4. BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS

    BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS 题意: 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p, ...

  5. 【bzoj2242】[SDOI2011]计算器

    2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 3207  Solved: 1258[Submit][Statu ...

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

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

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

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

  8. 【bzoj2242】: [SDOI2011]计算器 数论-快速幂-扩展欧几里得-BSGS

    [bzoj2242]: [SDOI2011]计算器 1.快速幂 2.扩展欧几里得(费马小定理) 3.BSGS /* http://www.cnblogs.com/karl07/ */ #include ...

  9. 洛谷 P2485 [SDOI2011]计算器 解题报告

    P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...

  10. P2485 [SDOI2011]计算器

    P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...

随机推荐

  1. C语言的第 次作业总结

    PTA实验作业 第一题: 使用函数输出水仙花数 1.设计思路: 2.碰到的问题及解决方法: 实验中碰到的主要问题是:虽然知道如何求每一位的数但不知道如何输出m到n之间的水仙花数,我上面截图中的和瓮恺视 ...

  2. Git使用方法2.0

    ## Git来源: 最早开始是由Ruby程序员们发起的.Ruby是日本的家伙搞出来的,日本有个代码托管网站叫heroku,当时用这个的人比较多,现在这个网站还能打开,网址是www.heroku.com ...

  3. Windows下编译SDL

    Windows下编译SDL的理由我就不多说了,无论用VS来编译或调试SDL库都是很方便的.而且SDL源代码中也包含了VC工程,你所要做的只是解压VC工程,进行适当的配置,然后编译.调试. 编译SDL大 ...

  4. vue.js下载及安装配置

    环境 Deepin15.4 下载及配置 node下载地址:http://nodejs.cn/download/ 解压到文件夹 /home/maskerk/vue/ 下 设置软连接: $ ln -s / ...

  5. nyoj 正数性质

    整数性质 时间限制:500 ms  |  内存限制:65535 KB 难度:1   描述 我们知道,在数学中,对于任意两个正整数a和b,必定存在一对整数s.t使得sa+tb=gcd(a,b).   输 ...

  6. JAVA_SE基础——57.有了包之后类与类之间的访问使用import语句

    代码1访问代码2 代码1: class Demo3 { public static void main(String[] args) { Demo4 a = new Demo4(); a.print( ...

  7. linux——网络基础

    装完linux系统要对网络(ip地址,子网掩码,网关,DNS)进行配置,才能连接网络 一,开启网卡eth0 CentOS显示没有网卡(eth0) 2.设置静态IP vim /etc/sysconfig ...

  8. 新概念英语(1-31)Where's Sally?

    新概念英语(1-31)Where's Sally? Is the cat climbing the tree ? A:Where is Sally, Jack ? B:She is in the ga ...

  9. python网络爬虫与信息提取 学习笔记day1

    Day1: 安装python之后,为其配置requests第三方库,并爬取百度主页内容. 语句解释: r.status_code检测请求的状态码,如果状态码为200,则说明访问成功,否则,则说明访问失 ...

  10. ccf认证 201709-4 通信网络 java实现

    试题编号:                                                               201709-4 试题名称: 通信网络 时间限制: 1.0s 内 ...