LOJ 10214 计算器 题解
k==1时,快速幂就好了;
k==2时,exgcd就好了,但要注意取模范围的控制;
k==3时,BSGS可以解决高次同余方程:
然后就可以开心的A掉了,但要注意特殊情况的特判
#include <bits/stdc++.h>
using namespace std;
long long KSM(long long a,long long b,long long p)
{
long long res=;
while(b){
if(b&) res=res*a%p;
a=a*a%p;
b/=;
}
return res%p;
}
void solve1(int t)
{
while(t--){
long long y,z,p;
cin>>y>>z>>p;
printf("%lld\n",KSM(y,z,p)%p);
}
}
long long d;
void exgcd(long long a,long long b,long long &x,long long &y)
{
if(b==){
x=;
y=;
d=a;
return;
}
exgcd(b,a%b,y,x);
y-=(a/b)*x;
}
void solve2(int t)
{
while(t--){
long long y,z,p;
cin>>y>>z>>p;
long long ha,la;
exgcd(y,p,ha,la);
if(z%d!=){
cout<<"Orz, I cannot find x!"<<endl;
}
else{
cout<<(((z*ha/d)%p+p)%p+p)%p<<endl;
}
}
}
void BSGS(long long a,long long ans,long long p)
{
map<long long ,long long> Myhash;
ans%=p;
int tmp=sqrt(p)+;
for(int i=;i<tmp;i++){
Myhash[(ans*KSM(a,i,p))%p]=i;
}
a=KSM(a,tmp,p)%p;
if(a==&&ans==){
cout<<""<<endl;
return;
}
if(a==&&ans!=){
cout<<"Orz, I cannot find x!"<<endl;
return;
}
for(int i=;i<=tmp;i++){
if(Myhash.find(KSM(a,i,p))!=Myhash.end()&&(i*tmp-Myhash[KSM(a,i,p)]>=)){
cout<<i*tmp-Myhash[KSM(a,i,p)]<<endl;
return;
}
}
cout<<"Orz, I cannot find x!"<<endl;
}
void solve3(int t)
{
while(t--){
long long y,z,p;
cin>>y>>z>>p;
BSGS(y,z,p);
}
}
int main()
{
int t,k;
cin>>t>>k;
if(k==){
solve1(t);
}
else if(k==){
solve2(t);
}
else solve3(t);
}
LOJ 10214 计算器 题解的更多相关文章
- LOJ 一本通一句话题解系列:
第一部分 基础算法 第 1 章 贪心算法 1):「一本通 1.1 例 1」活动安排:按照结束时间排序,然后扫一遍就可以了. 2):「一本通 1.1 例 2」种树:首先要尽量的往区间重叠的部分种树,先按 ...
- BZOJ2242:[SDOI2011]计算器——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2242 https://www.luogu.org/problemnew/show/P2485 你被 ...
- LOJ P10249 weight 题解
每日一题 day58 打卡 Analysis 这道题搜索的想法非常巧妙,从两端向中间找,这样可以保证仅仅对于head或tail而言,需要用到的前缀和与后缀和是单调递增的,这样排个序就解决了. 值得一提 ...
- LOJ P10013 曲线 题解
每日一题 day38 打卡 Analysis 这道题运用的是三分,就是说具有一定的单调性,找最大最小值,然后和二分基本类似,就是说特性就是说当前两个点比较,较优的点和最优点在相对了较差点的同侧,就是说 ...
- LOJ P10002 喷水装置 题解
每日一题 day35 打卡 Analysis 先将不符合条件的区间去掉(即半径小于W,不然宽度无法符合),将符合条件的按区间存入节点中.区间的左边界是x-sqrt(r*r-W*W/4.0),要计算x轴 ...
- LOJ P10016 灯泡 题解
每日一题 day50 打卡 Analysis 用初中学的相似推一波式子,再用三分一搞就好了. #include<iostream> #include<cstdio> #incl ...
- LOJ P10015 扩散 题解
每日一题 day49 打卡 Analysis 用dis数组记录每两个点之间的时间,再用一个传递闭包来维护最小的时间就好了 #include<iostream> #include<cs ...
- 【网络流24题】最长k可重线段集(费用流)
[网络流24题]最长k可重线段集(费用流) 题面 Cogs的数据有问题 Loj 洛谷 题解 这道题和最长k可重区间集没有区别 只不过费用额外计算一下 但是,还是有一点要注意的地方 这里可以是一条垂直的 ...
- 【网络流24题】最长k可重区间集(费用流)
[网络流24题]最长k可重区间集(费用流) 题面 Cogs Loj 洛谷 题解 首先注意一下 这道题目里面 在Cogs上直接做就行了 洛谷和Loj上需要判断数据合法,如果\(l>r\)就要交换\ ...
随机推荐
- Mybatis 结果映射下划线转驼峰
mybatis 结果映射下划线转驼峰 Spring Boot 配置: #下划线转驼峰 mybatis.configuration.map-underscore-to-camel-case=true m ...
- DVWA--File Inclusion
0x01了解什么叫file inclusion File Inclusion,意思是文件包含(漏洞),是指当服务器开启allow_url_include选项时,就可以通过php的某些特性函数(incl ...
- 191022Django模板
一.变量和句点符深度查找 字符串变量引用 def show_time(request): now_time = datetime.datetime.now() return render(reques ...
- 汽车Lin总线特点
串行通信:线间干扰小,节省线束,传输距离长 单线传输:增强的ISO 9141 (ISO 15765-1),总线电压基于VBAT 最高速率:20kbit/s 单主多从结构,无需仲裁:主节点同时包含主任务 ...
- leetcode 135分发糖果
这是一道hard题,不好想,但最后还是想出来了,私以为还是根据一些思想方法自己想出来做法印象比较深刻,其次看人家的做法思想自己写代码,其次看代码理解默写,其次直接抄代码: 首先,给每个孩子都发一个糖果 ...
- Jenkins发布
右键查看图片显示全图
- [VBA]合并工作簿优化版
Sub 合并工作簿数据()Dim arrDim i As Integer, j As Integer, x As IntegerDim f As String, m As String, n As S ...
- iOS 图表工具charts之CombinedChartView
关于charts的系列视图介绍传送门: iOS 图表工具charts介绍 iOS 图表工具charts之LineChartView iOS 图表工具charts之BarChartView iOS 图表 ...
- Python_编程特色
目录 目录 前言 软件环境 列表推导式 字典的默认值 forelse语句 交换两个变量的值 链式比较 真值测试 序列类型元素反转 连接字符串和列表 内置算术函数 利用zip来创建键值对 最后 前言 P ...
- 简单方法让input date支持placeholder(包含ios手机端方法)
<input class="baixin-quan-info-box-time" type="text" onfocus="(this.type ...