嘟嘟嘟

黄题 + 绿题 + 蓝题 = 紫题……
对于询问1,直接快速幂。
对于询问2,\(exgcd\)。
对于询问3,\(bsgs\),但要特判一下\(a \ \ mod \ \ c = 0\)且 \(b \ \ mod \ \ c \neq 0\)的时候应该无解。
\(bsgs\)不懂的可以看我的这篇博客

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
const int base = 999979;
inline ll read()
{ll ans = 0;char ch = getchar(), last = ' ';while(!isdigit(ch)) last = ch, ch = getchar();while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();if(last == '-') ans = -ans;return ans;
}
inline void write(ll x)
{if(x < 0) x = -x, putchar('-');if(x >= 10) write(x / 10);putchar(x % 10 + '0');
}ll y, z, p;void work1(ll a, ll b, ll mod)
{a %= mod; ll ret = 1;for(; b; b >>= 1, a = a * a % mod)if(b & 1) ret = ret * a % mod;write(ret), enter;
}void exgcd(ll a, ll b, ll& d, ll& x, ll& y)
{if(!b) d = a, x = 1, y = 0;else exgcd(b, a % b, d, y, x), y -= x * (a / b);
}
void work2(ll a, ll c, ll b)
{ll x, y, d;exgcd(a, b, d, x, y);if(c % d) {puts("Orz, I cannot find x!"); return;}ll t = b / d;write((x * (c / d) % t + t) % t), enter;
}struct Hash
{int nxt; ll to; int w;
}e[maxn];
int head[base], hcnt = 0;
int st[maxn], top = 0;
void init()
{hcnt = 0;while(top) head[st[top--]] = 0;
}
void insert(ll x, int y)
{int h = x % base;if(!head[h]) st[++top] = h;e[++hcnt] = (Hash){head[h], x, y};head[h] = hcnt;
}
int query(ll x)
{int h = x % base;for(int i = head[h]; i; i = e[i].nxt)if(e[i].to == x) return e[i].w;return -1;
}int bsgs(ll a, ll b, ll c)
{if(a % c == 0 && b % c) return -1;init();int s = sqrt(c);ll p = 1;for(int i = 0; i < s; ++i){if(p == b) return i;insert(p * b % c, i);p = p * a % c;}ll q = p;for(int i = 1; i <= s; ++i){int t = query(q);if(t != -1) return i * s - t;q = q * p % c;}return -1;
}
void work3(ll a, ll b, ll c)
{b %= c;ll ans = bsgs(a, b, c);if(ans == -1) puts("Orz, I cannot find x!");else write(ans), enter;
}int main()
{int T = read(), K = read();while(T--){y = read(); z = read(); p = read();if(K == 1) work1(y, z, p);else if(K == 2) work2(y, z, p);else work3(y, z, p);}return 0;
}

转载于:https://www.cnblogs.com/mrclr/p/9969918.html

[SDOI2011]计算器相关推荐

  1. BZOJ 2242: [SDOI2011]计算器 [快速幂 BSGS]

    2242: [SDOI2011]计算器 题意:求\(a^b \mod p,\ ax \equiv b \mod p,\ a^x \equiv b \mod p\),p是质数 这种裸题我竟然WA了好多次 ...

  2. [bzoj2242] [SDOI2011]计算器

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

  3. BZOJ 2242([SDOI2011]计算器-Baby Step Giant Step第1题)

    2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 744  Solved: 289 [Submit][Statu ...

  4. 【bzoj2242】[SDOI2011]计算器 EXgcd+BSGS

    题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给定y,z,p, ...

  5. luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法

    BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...

  6. [bzoj2242][SDOI2011]计算器

    题目大意:三合一,给你$y,z,p$,求$x$,三种询问 $y^z\bmod{p}$ $xy\equiv z\pmod p$的最小非负整数 $y^z\equiv z\pmod p$的最小非负整数 题解 ...

  7. [SDOI2011] 计算器

    题目链接:戳我 三合一?? 第一问是..快速幂. 第二问求逆元.最后乘上z即可. 第三问bsgs模板了. bsgs不会?戳我 代码如下: #include<cstdio> #include ...

  8. [SDOI2011]计算器 BSGS

    前两个操作都看出来做法了,第三个要用到BSGS这个算法 BSGS主要可以解 a^x=b(mod n)的 0<=x<n 的解 暂时先拿kb和hzwer的板子当黑盒吧..数论细节短时间内没有学 ...

  9. bzoj2242 [SDOI2011]计算器 exgcd+ksm+bsgs

    三个板子,注意bsgs的时候要特判无解,即a%p==0||b%p==0(bsgs还是很巧妙的..) 码: #include<iostream> #include<cstdio> ...

最新文章

  1. Java项目:在线课程会员系统(java+Springboot+Maven+JSP+Spring+Mysql+layui)
  2. iphone导出照片到电脑_如何更改 iPhone 照片格式?
  3. iOS 学习记录----动画
  4. spring ref historydesign philosophy
  5. python3爬虫(2)下载有固定链接的视频
  6. 【转】logback 常用配置详解(序)logback 简介
  7. 内存映射获取行数_使用内存映射文件获取巨大的矩阵
  8. keil5函数 默认返回值_C++ 函数的定义
  9. 酒店房间和 C++ 局部变量的作用域
  10. Magento教程 7:客制化网站
  11. 虚拟化技术天书:九宫格图解虚拟化——此文多风险,阅读需谨慎
  12. 关于vscode CloudMusic 插件无法登陆
  13. 40万亿全球最大资管来A股建仓了交易策略竟是这个
  14. VirtulBox安装虚拟机(鼠标点击时)0x00000000指令引用的0x00000000内存该内存不能为written错误解决方案...
  15. 【系统】Win10 新装系统提示 OOBEIDPS
  16. python爬虫scrapy比较常用的三个命令
  17. oss :Request has expired.
  18. StataJournal来啦!随便看(2001-2020)更新到2020年第2期
  19. linux下网页制作,linux网页制作
  20. EDMA 和QDMA 还看不懂,先收藏着

热门文章

  1. lync显示无法找到服务器,Lync 2013 已知问题
  2. 【深度优先搜索】计蒜客:王子救公主
  3. PYTHON 笔记:函数的参数(关键字参数,默认参数,可变长参数,可变长的关键字参数)
  4. 全参考视频质量评价方法(PSNR,SSIM)以及相关数据库
  5. Error starting ApplicationContext. To display the auto-configuration report re-run your application
  6. Incorrect username or password (access token)
  7. uml边界类例子_面向对象UML笔记
  8. swift python 性能_Swift 性能探索和优化分析
  9. eclipse配置python开发环境_Eclipse中配置python开发环境详解
  10. 第四章 ASP.NET MVC (表单和HTML辅助方法)