题目链接

第一问:快速幂

第二问:扩欧解线性同余方程

第三问:\(BSGS\)

三个模板

#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
int a, b, p;
ll x, y, z;
ll exgcd(ll a, ll b, ll &x, ll &y){
if(!b){
x = 1; y = 0; return a;
}
ll d = exgcd(b, a % b, x, y);
ll z = x; x = y; y = z - a / b * y;
return d;
}
int fast_pow(int n, int k){ //n^k%p
int ans = 1;
while(k){
if(k & 1) ans = (ll)ans * n % p;
n = (ll)n * n % p;
k >>= 1;
}
return ans;
}
ll BSGS(){ //a^x≡b(mod p)
map <ll, ll> hash; hash.clear();
int t = ceil(sqrt(p)), val = b, j = 1;
for(int i = 0; i < t; ++i){
hash[val] = i;
val = (ll)val * a % p;
}
a = fast_pow(a, t);
if(!a) return !b ? 1 : -1;
for(int i = 0; i <= t; ++i){
int k = hash.find(j) == hash.end() ? -1 : hash[j];
if(k >= 0 && (i * t - k) >= 0) return (ll)i * t - k;
j = (ll)j * a % p;
}
return -1;
}
int T, K;
int main(){
scanf("%d%d", &T, &K);
if(K == 3)
while(T--){
scanf("%d%d%d", &a, &b, &p);
int ans = BSGS();
ans == -1 ? puts("Orz, I cannot find x!") : printf("%d\n", ans);
}
else if(K == 1)
while(T--){
scanf("%d%d%d", &a, &b, &p);
printf("%d\n", fast_pow(a, b));
}
else
while(T--){
scanf("%d%d%d", &a, &b, &p);
if(b % (z = exgcd(a, p, x, y))) puts("Orz, I cannot find x!");
else printf("%d\n", ((ll)x * b / z % p + p) % p);
}
return 0;
}

【洛谷 P2485】 [SDOI2011]计算器 (BSGS)的更多相关文章

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

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

  2. 洛谷P2485 [SDOI2011]计算器(exgcd+BSGS)

    传送门 一题更比三题强 1操作直接裸的快速幂 2操作用exgcd求出最小正整数解 3操作用BSGS硬上 然后没有然后了 //minamoto #include<cstdio> #inclu ...

  3. 【洛谷P2485】计算器

    BSGS模板题 代码如下 #include <bits/stdc++.h> using namespace std; typedef long long LL; LL fpow(LL a, ...

  4. BZOJ 2242 / Luogu P2485 [SDOI2011]计算器 (BSGS)

    type 1type\ 1type 1 就直接快速幂 type 2type\ 2type 2 特判+求逆元就行了. type 3type\ 3type 3 BSGS板 CODE #include< ...

  5. P2485 [SDOI2011]计算器

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

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

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

  7. [洛谷P2491] [SDOI2011]消防

    洛谷题目链接:[SDOI2011]消防 题目描述 某个国家有n个城市,这n个城市中任意两个都连通且有唯一一条路径,每条连通两个城市的道路的长度为zi(zi<=1000). 这个国家的人对火焰有超 ...

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

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

  9. BZOJ2243 洛谷2486 [SDOI2011]染色 树链剖分

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ2243 题目传送门 - 洛谷2486 题意概括 一棵树,共n个节点. 让你支持以下两种操作,共m次操 ...

  10. 洛谷 P2495 [SDOI2011]消耗战(虚树,dp)

    题面 洛谷 题解 虚树+dp 关于虚树 了解一下 具体实现 inline void insert(int x) { if (top == 1) {s[++top] = x; return ;} int ...

随机推荐

  1. javac一次性编译多个包下的.java文件

    如题是我想要知道的,然后在网上搜了一下 下面是在某些帖子里看到别人说的只言片语 =========================================================== ...

  2. 生产者与消费者-N:1-基于list

    多个生产者/一个消费者: /** * 生产者 */ public class P { private MyStack stack; public P(MyStack stack) { this.sta ...

  3. 开发react的一些记录

    1.keyboard事件返回的对象SyntheticKeyboardEvent全部是null 解决方法:SyntheticKeyboardEvent的type,which,timeStamp可以得到你 ...

  4. Ubuntu 进阶命令——长期不定时更新

    有时候远程连接服务器忽然中断或者不小心关掉了终端界面,正在运行的命令或者程序就会被强制停止.这时候,我们可以借助一些命令来避免这种情况的发生. nohup 不挂断地运行命令 & 在后台运行命令 ...

  5. 关于react-redux中Provider、connect的解析

    Provider 是什么 react-redux 提供的一个 React 组件 作用 把 store 提供给其子组件 //使用 redux 的 createStore 方法创建的一个 store co ...

  6. Mininet实验 动态改变转发规则

    介绍 拓扑如下: 在该环境下,假设H1 ping H4,初始的路由规则是S1-S2-S5,一秒后,路由转发规则变为S1-S3-S5,再过一秒,规则变为S1-S4-S5,然后再回到最初的转发规则S1-S ...

  7. Internet History,Technology and Security

    Internet History,Technology and Security(简单记录) First Week High Stakes Research in Computing,and Comm ...

  8. Solr的搭建和部署

    1.概述 简介 Solr,全称Search On Lucene Replication.一个开源的搜索服务器,对外提供类似于WebService的API接口. 用户可以通过http请求,向搜索引擎服务 ...

  9. c# 以多个字符串分隔字符串数据 分组 分隔 split 正则分组

    string str="aaa[##]ccc[##]ddd[##]bb" Regex regex=new Regex("[##]");//以 [##] 分割 s ...

  10. 【考试记录】4.8 Table ( 数论数学 --组合数 & 杨辉三角)

    陆陆续续的开始考很多的试,也会更新这些题目记录下来,免得做完了之后毫无印象,就这么水过去了(以前的考试都是如此,哎……) Table (T1) : 样例: 出于对数学题本能的恐惧考场上放弃了此题专攻T ...