这个题就是把三个数论基础合在了一起,算是一道比较全面的题.

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

  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)的最 ...

随机推荐

  1. CAD绘制一个单行文字(com接口VB语言)

    主要用到函数说明: _DMxDrawX::DrawText 绘制一个单行文字.详细说明如下: 参数 说明 DOUBLE dPosX >文字的位置的X坐标 DOUBLE dPosY 文字的位置的Y ...

  2. 打造个人的vimIDE

    环境说明 系统版本:centos7.Ubuntu16 vim版本:7.4 安装git工具 整体说明:本文的vim配置是针对Linux的单个系统用户,python的自动补全使用的是 jedi-vim 插 ...

  3. python环境配置以及基本知识

    python---一种解释型语言(脚本语言),具有代码简洁.入门简单.开发效率高的优点.当然不可避免的有着暴露源码.执行效率低的缺点,但毕竟瑕不掩瑜,在数据是无比宝贵的财富的当下,无疑是一门优秀的编成 ...

  4. react----父子组件之间的参数传递

    1.父组件向子组件传递参数 //父组件 import React from 'react'; import './header.css' import ComponentChild from './h ...

  5. 洛谷 1182 数列分段Section II

    [题解] 最大值最小化,那么一般要联想到二分.二分一个最大值,然后check一下能否分成小于等于m段即可. #include<cstdio> #include<algorithm&g ...

  6. Spring 事务XML配置

    <!-- 配置 Spring 的声明式事务 --> <!-- 1. 配置事务管理器 --> <bean id="transactionManager" ...

  7. RequestMapping注解_修饰类

    [使用RequestMapping映射请求] 1.Spring MVC使用 @RequestMapping 注解为控制器指定可以处理哪些URL请求. 2.在控制器的类定义及方法定义处都可以标注. @R ...

  8. ZOJ 3349 Special Subsequence

    Special Subsequence Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Ori ...

  9. TensorFlow Ops

    TensorFlow Ops 1. Fun with TensorBoard In TensorFlow, you collectively call constants, variables, op ...

  10. c++ 上机实验题

    c++语言俺是不会啦,但是朋友考试需要,那只能勉为其难的入门下做做考试题了. 以下就是具体的题目和答案: ----------------------------------------------- ...