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

Sample Output

【样例输出1】
2
1
2
【样例输出2】
2
1
0

Hint

【数据规模和约定】
对于100%的数据,1<=y,z,p<=10^9,为质数,1<=T<=10。

题解

数论三合一。

第一问,快速幂。

第二问,扩欧求特解。

第三问,BSGS。

 //It is made by Awson on 2018.1.16
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
using namespace std;
const LL MOD = ;
void read(LL &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
}
void write(LL x) {
if (x > ) write(x/);
putchar(x%+);
} LL t, l, a, b, c; map<LL, int>mp;
LL exgcd(LL a, LL b, LL &x, LL &y) {
if (b == ) {x = , y = ; return a; }
LL d = exgcd(b, a%b, x, y), t = x;
x = y, y = t-a/b*y;
return d;
}
LL quick_pow(LL a, LL b, LL c) {
LL ans = ;
while (b) {
if (b&) ans = ans*a%c;
a = a*a%c, b >>= ;
}
return ans;
} LL BSGS(LL a, LL b, LL c) {
if (b == ) return ;
if (a == && b != ) return -;
mp.clear();
LL tim = ceil(sqrt(c)), tmp = b%c;
for (int i = ; i <= tim; i++) {
mp[tmp] = i, tmp = tmp*a%c;
}
LL t = tmp = quick_pow(a, tim, c);
for (int i = ; i <= tim; i++) {
if (mp.count(tmp)) return tim*i-mp[tmp];
tmp = tmp*t%c;
}
return -;
}
void work() {
read(t), read(l);
while (t--) {
read(a), read(b), read(c);
if (l == ) write(quick_pow(a, b, c)), putchar('\n');
else if (l == ) {
LL x, y; LL d = exgcd(a, c, x, y);
if (b%d) printf("Orz, I cannot find x!\n");
else {x = x*b/d, d = c/d; write((x%d+d)%d), putchar('\n'); }
}else {
LL ans = BSGS(a%c, b%c, c);
if (ans == -) printf("Orz, I cannot find x!\n");
else write(ans), putchar('\n');
}
}
}
int main() {
work();
return ;
}

最新文章

  1. [学习笔记]tarjan求割点
  2. 解决Hibernate向MySQL数据库插入中文乱码问题
  3. 将Asp.Net页面输出到EXCEL里去
  4. long和Long的区别
  5. python seq
  6. 依赖注入及AOP简述(七)——FQCN请求模式
  7. C++中的namespace
  8. Robot Framework: 自定义自己的python库
  9. UVA 10618 Tango Tango Insurrection
  10. OO的奇妙冒险1
  11. xilink 烧写flash
  12. 用友软件系统管理员账号admin密码忘记了,如何解决?
  13. 启动 angular-phonecat 项目时出现这玩意 。(&#39;The header content contains invalid characters&#39;);
  14. ITIL之“变更管理”
  15. Sencha Touch 实战开发培训 电子书 基础篇
  16. ubuntu14.04 使用传统的netcat
  17. Lua 字符串库函数总结
  18. Linux命令-实时监测命令:watch
  19. 入参是小数的String,返回小数乘以100的String
  20. java 静态代理 JDK动态代理 Cglib动态代理

热门文章

  1. ord在python是什么意思?
  2. Struts2之配置文件中Action的详细配置
  3. Python之旅.第三章.函数3.29
  4. 格式化输出io:format的奇技淫巧
  5. PHP模式设计之单例模式、工厂模式、注册树模式、适配器模式、观察者模式
  6. Spring知识点回顾(01)Java Config
  7. .NET CORE 框架ABP的代码生成器(ABP Code Power Tools )使用说明文档
  8. Python学习之dict和set
  9. JS中全等和相等操作符的区别和比较规则
  10. leetcode算法: Average of Levels in Binary Tree