P2485 [SDOI2011]计算器
P2485 [SDOI2011]计算器
题目描述
你被要求设计一个计算器完成以下三项任务:
1、给定y、z、p,计算y^z mod p 的值;
2、给定y、z、p,计算满足xy ≡z(mod p)的最小非负整数x;
3、给定y、z、p,计算满足y^x ≡z(mod p)的最小非负整数x。
为了拿到奖品,全力以赴吧!
输入输出格式
输入格式:
输入文件calc.in 包含多组数据。
第一行包含两个正整数T、L,分别表示数据组数和询问类型(对于一个测试点内的所有数
据,询问类型相同)。
以下T 行每行包含三个正整数y、z、p,描述一个询问。
输出格式:
输出文件calc.out 包括T 行.
对于每个询问,输出一行答案。
对于询问类型2 和3,如果不存在满足条件的,则输出“Orz, I cannot find x!”。
输入输出样例
3 1
2 1 3
2 2 3
2 3 3
2
1
2
3 2
2 1 3
2 2 3
2 3 3
2
1
0
4 3
2 1 3
2 2 3
2 3 3
2 4 3
0
1
Orz, I cannot find x!
0
说明

分析
三个模板,
- 快速幂,
- 求yx=z(mod p),转化成,yx-kp = z;可以用扩展欧几里得求解,并且要求z%gcd(y,p)!=0,(扩展欧几里得求ax+by=c:http://www.cnblogs.com/MashiroSky/p/5912977.html), 补充:因为p是质数,所以可以用费马小定理, p是质数,y与p互质,所以y有逆元,x=y^(-1)*z,y^(-1)=y^(p-2),因为0没有逆元,所以只有y=0时无解
- bsgs
注意,int与longlong类型,结尾的换行符
code
#include<iostream>
#include<cstdio>
#include<map>
#include<cmath> using namespace std;
typedef long long LL;
map<int,int>mp; int ksm(int a,int p,int mod)
{
int now = ;
while (p)
{
if (p&)
now = 1ll*now*a%mod;
a = 1ll*a*a%mod;
p = p>>;
}
return now;
}
int exgcd(int a,int b,int &x,int &y)
{
if (b==)
{
x = , y = ;
return a;
}
int r = exgcd(b,a%b,x,y);
int t = x;
x = y;
y = t-(a/b)*y;
return r;
}
void bsgs(int a,int b,int p)
{
int m,t,ans,now;
if (a%p==&&b==)
{
printf("1\n");return ;
}
if (a%p==)
{
printf("Orz, I cannot find x!\n");return ;
}
mp.clear();
m = ceil(sqrt(p));
now = b%p;
mp[now] = ;
for (int i=; i<=m; ++i)
{
now = (1ll*now*a)%p;
mp[now] = i;
}
t = ksm(a,m,p);
now = ;
for (int i=; i<=m; ++i)
{
now = (1ll*now*t)%p;
if (mp[now])
{
ans = i*m-mp[now];
printf("%d\n",(ans%p+p)%p);
return ;
}
}
printf("Orz, I cannot find x!\n");
}
int main()
{
int t,k,a,b,c;
scanf("%d%d",&t,&k);
if (k==)
{
for (int i=; i<=t; ++i)
scanf("%d%d%d",&a,&b,&c),printf("%d\n",ksm(a,b,c));
}
else if (k==)
{
int x,y;
for (int i=; i<=t; ++i)
{
scanf("%d%d%d",&a,&b,&c);
int d = exgcd(a,c,x,y);
if (b%d!=) printf("Orz, I cannot find x!\n"); //换行符
else
{
x = 1ll*x*(b/d)%c;
x = (x%(c/d)+c/d)%(c/d);
printf("%d\n",x);
}
}
}
else
{
for (int i=; i<=t; ++i)
scanf("%d%d%d",&a,&b,&c),bsgs(a,b,c);
}
return ;
}
P2485 [SDOI2011]计算器的更多相关文章
- 洛谷 P2485 [SDOI2011]计算器 解题报告
P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...
- luogu P2485 [SDOI2011]计算器
题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最小非负整数x: 3.给定y.z.p,计算 ...
- 洛谷P2485 [SDOI2011]计算器(exgcd+BSGS)
传送门 一题更比三题强 1操作直接裸的快速幂 2操作用exgcd求出最小正整数解 3操作用BSGS硬上 然后没有然后了 //minamoto #include<cstdio> #inclu ...
- BZOJ 2242 / Luogu P2485 [SDOI2011]计算器 (BSGS)
type 1type\ 1type 1 就直接快速幂 type 2type\ 2type 2 特判+求逆元就行了. type 3type\ 3type 3 BSGS板 CODE #include< ...
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
- BZOJ 2242: [SDOI2011]计算器( 快速幂 + 扩展欧几里德 + BSGS )
没什么好说的... --------------------------------------------------------------------- #include<cstdio&g ...
- BZOJ 2242: [SDOI2011]计算器 [快速幂 BSGS]
2242: [SDOI2011]计算器 题意:求\(a^b \mod p,\ ax \equiv b \mod p,\ a^x \equiv b \mod p\),p是质数 这种裸题我竟然WA了好多次 ...
- BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS
BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS 题意: 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p, ...
- 【bzoj2242】[SDOI2011]计算器
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 3207 Solved: 1258[Submit][Statu ...
随机推荐
- python_1基础学习
2017年12月02日 20:14:48 独行侠的守望 阅读数:221 标签: python 更多个人分类: Python编辑版权声明:本文为博主原创文章,转载请注明文章链接. https://blo ...
- TED:如何掌控你的自由时间以及让自己变得更好,这样就能看到爱情应有的样子
TED:如何掌控你的自由时间以及让自己变得更好,这样就能看到爱情应有的样子 一.<如何掌控你的自由时间> (1)时间管理的传统思维:守时和节省零散的时间.演讲者认为这个观点已经彻底落后. ...
- oracle中查找与已知表的数据库对象
在此次情况中,业务顾问就给我提供了一张客户公司客户化的Form,然后让找出界面上的数据是怎样生成的. 首先我们从EBS form 界面上找到了界面的数据来源于一张表ks_so_line_margin_ ...
- 构建第一个spring boot2.0应用之项目启动运行的几种方式(二)
方法一. 配置Run/Debug Configuration 选择Main Class为项目 Application启动类(入口main方法) (2).进行项目目录,即包含pom.xml的目录下,启 ...
- Mac 颜色取值
command+shift+4 截图,我靠,我以前不知道 系统自带数码测色计, 选择显示十六位制 command+L 锁定 command+shift+c 复制 简直太方便
- Mantis-1.3.3 (Ubuntu 16.04)
平台: Ubuntu 类型: 虚拟机镜像 软件包: mantis-1.3.3 bug tracking commercial devops mantis open-source project man ...
- 利用expect实现自动化操作
管理机上需要安装expect包 yum -y install expect 1.定义主机ip [root@localhost ~]# cat ip.txt 192.168.1.12 192.168.1 ...
- cms-帖子静态化
实现帖子静态化和实现友情链接静态化一致, 1.首先建立帖子类别的实体类: package com.open1111.entity; /** * 帖子类别实体 * @author user * */pu ...
- STL容器 成员函数 时间复杂度表
Sequence containers Associative containers Headers <vector> <deque> <list> <s ...
- iOS开发资料
https://github.com/XCGit/awesome-objc-frameworks https://github.com/KevinHM/ios-good-practices-the-l ...