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 (我什么时候看到二进制贡献才能条件反射想到按位处理贡献呢……) 参 ...
随机推荐
- Windows Server 2008 R2 安装域
在Windows Server 2008 R2里面安装域. 1.首先在"服务"里面添加"角色": 2.选择对应的域角色 3.安装完成后要启动配置向导 4.选择新 ...
- hdu2199Can you solve this equation?(解方程+二分)
Can you solve this equation? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- Unity编辑器 - 自动排版
Unity编辑器 - 自动排版 使用花括号提高可读性 //一组横向排列的控件 GUILayout.BeginHorizontal(); { GUILayout.BeginVertical(); { / ...
- MySQL三方面优化
第一方面:30种mysql优化sql语句查询的方法1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中使用 ...
- 在github上面创建属于自己的个性主页
圈子里面越来越多的同事在github上面创建自己的项目文档,那里确实高手云集,海内外的技术大牛小牛们都在那儿有一席之地,为“helloword”贡献自己. 以上感慨略过... 这几日正想创建一个自己的 ...
- 人艰不拆之破解低版本IE不兼容mediaQuery
先放个链接 大家预览下 http://scottjehl.github.io/Respond/test/test.html 值得注意的是 将页面源代码下载到本地时,直接用IE打开是没有效果的.需要把静 ...
- Learning Spatial-Temporal Regularized Correlation Filters for Visual Tracking---随笔
Learning Spatial-Temporal Regularized Correlation Filters for Visual Tracking DCF跟踪算法因边界效应,鲁棒性较差.SRD ...
- LeetCode 240——搜索二维矩阵 II
1. 题目 2. 解答 2.1. 方法一 从矩阵的左下角开始比较 目标值等于当前元素,返回 true: 目标值大于当前元素,j 增 1,向右查找,排除掉此列上边的数据(都比当前元素更小): 目标值小于 ...
- HADOOP/HDFS Essay
HDFS架构 the core of HADOOP/distributed systems is storeage(HDFS) and resource manager(YARN) for compu ...
- 2018科大讯飞AI营销算法大赛全面来袭,等你来战!
AI技术已成为推动营销迭代的重要驱动力.AI营销高速发展的同时,积累了海量的广告数据和用户数据.如何有效应用这些数据,是大数据技术落地营销领域的关键,也是检测智能营销平台竞争力的标准. 讯飞AI营销云 ...