51NOD 1038:X^A Mod P——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1038
X^A mod P = B,其中P为质数。给出P和A B,求< P的所有X。
找了半天找到了一道模板题,死嗑了一晚上对着题解写完了。
道理应该都懂吧……不懂我也懒的再说一遍了,看其他人博客吧:https://blog.csdn.net/dreamzuora/article/details/52744666
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
typedef long long ll;
const int N=;
const int M=;
struct HASH{
int w,to,nxt;
}h[M];
int cnt,head[N];
inline void add(int x,int y){
int t=x%N;
for(int i=head[t];i;i=h[i].nxt){
int v=h[i].to;
if(v==x){
h[i].w=y;//记大的
return;
}
}
h[++cnt].to=x;h[cnt].w=y;h[cnt].nxt=head[t];head[t]=cnt;
}
inline int query(int x){
int t=x%N;
for(int i=head[t];i;i=h[i].nxt){
int v=h[i].to;
if(v==x)return h[i].w;
}
return -;
}
int gcd(int a,int b){
return (!b)?a:gcd(b,a%b);
}
int exgcd(int a,int b,int &x,int &y){
if(!b){
x=,y=;
return a;
}
int ans=exgcd(b,a%b,y,x);
y-=(ll)(a/b)*x;
return ans;
}
int inv(int a,int c){
int x,y;
exgcd(a,c,x,y);
return (x%c+c)%c;
}
//a^x=b(mod c);
int BSGS(int a,int b,int c){
if(!a){
if(!b)return ;
return -;
}
int tot=,g,d=;
while((g=gcd(a,c))!=){
if(b%g)return -;
++tot;b/=g,c/=g;
d=(ll)d*(a/g)%c;
}
b=(ll)b*inv(d,c)%c;
cnt=;memset(head,,sizeof(head));
int s=sqrt(c),p=;
for(int i=;i<s;i++){
if(p==b)return i+tot;
add((ll)p*b%c,i);
p=(ll)p*a%c;
}
int q=p;
for(int i=s;i-s+<c;i+=s){
int t=query(q);
if(t!=-)return i-t+tot;
q=(ll)q*p%c;
}
return -;
}
vector<int>v;
int qpow(int k,int n,int p){
int res=;
while(n){
if(n&)res=(ll)res*k%p;
k=(ll)k*k%p;
n>>=;
}
return res;
}
bool pan(int g,int p){
for(int i=;i<v.size();i++){
if(qpow(g,(p-)/v[i],p)==)return ;
}
return ;
}
int primitive(int p){
int res=p-;v.clear();
for(int i=;i*i<=res;i++){
if(res%i==){
v.push_back(i);
while(res%i==)res/=i;
}
}
if(res!=)v.push_back(res);
for(int i=;;i++){
if(pan(i,p))return i;
}
}
vector<int>X;
void solve(int p,int a,int b){
X.clear();
int g=primitive(p)%p;
int t=BSGS(g,b,p);
if(t==-)return;
int A=a,B=p-,C=t,x,y;
int d=exgcd(A,B,x,y);
if(C%d)return;
x=(ll)x*(C/d)%B;
int delta=B/d;
for(int i=;i<d;i++){
x=((x+delta)%B+B)%B;
X.push_back(qpow(g,x,p));
}
sort(X.begin(),X.end());
X.erase(unique(X.begin(),X.end()),X.end());
}
int main(){
int T;
scanf("%d",&T);
while(T--){
int p,a,b;
scanf("%d%d%d",&p,&a,&b);
solve(p,a,b);
if(!X.size()){puts("No Solution");continue;}
else{
for(int i=;i<X.size();i++)printf("%d ",X[i]);
puts("");
}
}
return ;
}
+++++++++++++++++++++++++++++++++++++++++++
+本文作者:luyouqi233。 +
+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/ +
+++++++++++++++++++++++++++++++++++++++++++
51NOD 1038:X^A Mod P——题解的更多相关文章
- 【51nod 1038】X^A Mod P
题目描述 X^A mod P = B,其中P为质数.给出P和A B,求< P的所有X. 例如:P = 11,A = 3,B = 5. 3^3 Mod 11 = 5 所有数据中,解的数量不超过Sq ...
- 51Nod 1046 A^B Mod C(日常复习快速幂)
1046 A^B Mod C 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = ...
- 51nod 1812 树的双直径 题解【树形DP】【贪心】
老了-稍微麻烦一点的树形DP都想不到了. 题目描述 给定一棵树,边权是整数 \(c_i\) ,找出两条不相交的链(没有公共点),使得链长的乘积最大(链长定义为这条链上所有边的权值之和,如果这条链只有 ...
- 51NOD 2026:Gcd and Lcm——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=2026 参考及推导:https://www.cnblogs.com/ivo ...
- 51nod 1421:最大MOD值
1421 最大MOD值 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 有一个a数组,里面有n个整数.现在要从中找到两个数字(可以 ...
- 51Nod 1046 A^B Mod C Label:快速幂
给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^ ...
- 计算幂 51Nod 1046 A^B Mod C
给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^ ...
- 51NOD 1046 A^B Mod C
给出3个正整数A B C,求A^B Mod C. 例如,3 5 8,3^5 Mod 8 = 3. Input 3个正整数A B C,中间用空格分隔.(1 <= A,B,C <= 10^9) ...
- 51NOD 1709:复杂度分析——题解
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1709 (我什么时候看到二进制贡献才能条件反射想到按位处理贡献呢……) 参 ...
随机推荐
- mongdb数据迁移导出与导入
导出: mongoexport --host localhost --port --username un1 --password pwd1 --db db1 --collection col1 -- ...
- cf#512 C. Vasya and Golden Ticket
题目链接 http://codeforces.com/contest/1058/problem/C 这题还是暴力最方便,和的情况最多有n*a[i] 900种把每种都试一遍 #include<b ...
- tpo-08 C2 A strategy for attracting customers
第 1 段 1.Listen to a conversation between a student and a business professor. 听一段学生和教授的对话. 第 2 段 1.So ...
- 407. Plus One【LintCode java】
Description Given a non-negative number represented as an array of digits, plus one to the number. T ...
- TensorFlow | ReluGrad input is not finite. Tensor had NaN values
问题的出现 Question 这个问题是我基于TensorFlow使用CNN训练MNIST数据集的时候遇到的.关键的相关代码是以下这部分: cross_entropy = -tf.reduce_sum ...
- STM32F4编程手册学习2_内存模型
STM32F4编程手册学习2_内存模型 1. 内存映射 MCU将资源映射到一段固定的4GB可寻址内存上,如下图所示. 内存映射将内存分为几块区域,每一块区域都有一个定义的内存类型,一些区域还有一些附加 ...
- Linux内核设计笔记8——下半部
# 下半部笔记 1. 软中断 软中断实现 软中断是在编译期间静态分配,其结构如下所示,结构中包含一个接受该结构体指针作为参数的action函数. struct softirq_action{ void ...
- svn服务器 备份,迁移,部署方案
这次做业务迁移,要从一个云厂商迁移到某云厂商,之前每天到全备svn排到用场了,需要搭建一个全新到svn服务并要做迁移,并实现我们开发机到时时代码同步 一.svn备份有很多种,优劣都不同,百度可查,我采 ...
- 解读:未来30年新兴科技趋势报告(AI Frist,IoT Second)
前段时间美国公布的一份长达35页的<未来30年新兴科技趋势报告>.该报告是在美国过去五年内由政府机构.咨询机构.智囊团.科研机构等发表的32份科技趋势相关研究调查报告的基础上提炼形成的. ...
- Thunder团队第六周 - Scrum会议3
Scrum会议3 小组名称:Thunder 项目名称:i阅app Scrum Master:李传康 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...