B2242 [SDOI2011]计算器
这个题就是把三个数论基础合在了一起,算是一道比较全面的题.
1的时候就是快速幂
2的时候是exgcd求逆元,特殊的,只有两数互质才有逆元.
3就是bsgs啦,还是不太熟
题干:
Description
你被要求设计一个计算器完成以下三项任务:
、给定y,z,p,计算Y^Z Mod P 的值;
、给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数;
、给定y,z,p,计算满足Y^x ≡ Z ( mod P)的最小非负整数。
Input 输入包含多组数据。
第一行包含两个正整数T,K分别表示数据组数和询问类型(对于一个测试点内的所有数据,询问类型相同)。
以下行每行包含三个正整数y,z,p,描述一个询问。
Output
对于每个询问,输出一行答案。对于询问类型2和3,如果不存在满足条件的,则输出“Orz, I cannot find x!”,注意逗号与“I”之间有一个空格。
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<map>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(register int i = a;i <= n;i++)
#define lv(i,a,n) for(register int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
#define int long long
const int INF = << ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
int t,k;
map <int,int> mp;
void work1(int y,int z,int p)
{
int tot = ;
while(z)
{
if(z & )
{
tot *= y;
tot %= p;
}
y *= y;
y %= p;
z >>= ;
}
printf("%lld\n",tot);
}
int exgcd(int a,int b,int &x,int &y)
{
if(b == )
{
x = ;
y = ;
return a;
}
int t = exgcd(b,a % b,y,x);
y -= (a / b * x);
return t;
}
void work2(int y,int z,int p)
{
int x,f;
// z %= p;
int d = exgcd(y,p,x,f);
if(d != )
{
printf("Orz, I cannot find x!\n");
return;
}
x = (x % p + p) % p;
x = (x * z) % p;
/*if(now == p)
printf("Orz, I cannot find x!");
else*/
printf("%lld\n",x);
}
int qpow(int a,int b,int p)
{
int tot = ;
while(b)
{
if(b & )
{
tot *= a;
tot %= p;
}
a *= a;
a %= p;
b >>= ;
}
return tot;
}
void work3(int a,int b,int p)
{
if(a % p == )
{
printf("Orz, I cannot find x!\n");
return;
}
mp.clear();
int m = ceil(sqrt(p));
int now = b % p,ans;
mp[now] = ;
duke(i,,m)
{
now = (now * a) % p;
mp[now] = i;
}
int t = qpow(a,m,p);
now = ;
int ok = ;
duke(i,,m)
{
now = (now * t) % p;
if(mp[now])
{
ans = i * m - mp[now];
printf("%lld\n",(ans % p + p) % p);
ok = ;
break;
}
}
if(ok == )
printf("Orz, I cannot find x!\n");
}
main()
{
read(t);read(k);
//cout<<t<<endl;
duke(i,,t)
{
int y,z,p;
read(y);read(z);read(p);
if(k == )
{
work1(y,z,p);
}
else if(k == )
{
work2(y,z,p);
}
else if(k == )
{
work3(y,z,p);
}
//cout<<i<<" "<<t<<endl;
}
return ;
}
B2242 [SDOI2011]计算器的更多相关文章
- 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 ...
- BZOJ2242 [SDOI2011]计算器 【BSGS】
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 4741 Solved: 1796 [Submit][Sta ...
- 【BZOJ2242】[SDOI2011]计算器 BSGS
[BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...
- 【bzoj2242】: [SDOI2011]计算器 数论-快速幂-扩展欧几里得-BSGS
[bzoj2242]: [SDOI2011]计算器 1.快速幂 2.扩展欧几里得(费马小定理) 3.BSGS /* http://www.cnblogs.com/karl07/ */ #include ...
- 洛谷 P2485 [SDOI2011]计算器 解题报告
P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...
随机推荐
- 完善本地搭建的jekyll环境(Windows)
序:上篇文章虽然在本地搭建好了jekyll环境,但是却存在一些问题,如通过jekyll new创建的站点无法正常跑起来.中文编码有问题.这说明之前搭建的环境有不周之处. PS:因之前自己搭建环境时并未 ...
- C++ 输入外挂
inline int read() { int x=0;char ch=getchar(); while(ch<'0'||ch>'9')ch=getchar(); while(ch> ...
- The Falling Leaves(建树方法)
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of ...
- 升级 HTTPS,价值何在?
HTTPS 实质上是一种面向安全信息通信的协议.从最终的数据解析的角度上看,HTTPS 与 HTTP 没有本质上的区别.对于接收端而言,SSL/TSL 将接收的数据包解密,将数据传给 HTTP 协议层 ...
- oracle中的类似BIN$MrkCYT9eTTK+0sStMwn7+Q==$0的表的作用
https://www.2cto.com/database/201211/166482.html https://docs.oracle.com/cd/E11882_01/server.112/e40 ...
- D - Guess UVALive - 4255 拓扑排序
Given a sequence of integers, a1, a2, . . . , an, we define its sign matrix S such that, for 1 ≤ i ≤ ...
- TortoiseGit推送失败的问题
网络的SSH修改为使用git默认的ssh客户端,而不是tortosieGit提供的客户端 修改成这样 下面的本机凭证修改为当前用户 然后直接使用右键->git同步 在推送url上填写远程的url ...
- BZOJ(6) 1084: [SCOI2005]最大子矩阵
1084: [SCOI2005]最大子矩阵 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3566 Solved: 1785[Submit][Sta ...
- Ubuntu 16.04安装Gufw防火墙(转)
继上一篇文章http://www.cnblogs.com/EasonJim/p/6851241.html讲解的UFW防火墙是没有界面的,下面将介绍在Gufw的GUI配置功能. Ubuntu简化了ipt ...
- 将完整的Maven远程存储库下载到本地存储库(别试了,不太可取)
别试了,这种方式不太可取. 要解决可以有如下思路: 1.做成镜像站点,有如下命令: wget -m http://site.to.mirror.com #-m代表“镜子”. rsync repo1.m ...