原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1038.html

题目传送门 - 51Nod1038

题意

题解

  在模质数意义下,求高次剩余,模板题。

UPD(2018-09-10):

  详见数论总结。

  传送门 - https://www.cnblogs.com/zhouzhendong/p/Number-theory-Residue-System.html

代码

优化了一下代码……原来的那个在这一份后面……

#include <bits/stdc++.h>
using namespace std;
const int N=100005;
int T,A,B,P;
int Fac_p[N],Fac_tot,g;
int prime[N],vis[N],pcnt=0;
void Get_prime(int n){
memset(vis,0,sizeof vis);
pcnt=0;
for (int i=2;i<=n;i++){
if (vis[i])
continue;
prime[++pcnt]=i;
for (int j=i+i;j<=n;j+=i)
vis[j]=1;
}
}
int Pow(int x,int y,int mod){
int ans=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1)
ans=1LL*ans*x%mod;
return ans;
}
bool Get_g_Check(int P,int x){
for (int i=1;i<=Fac_tot;i++)
if (Pow(x,(P-1)/Fac_p[i],P)==1)
return 0;
return 1;
}
int Get_g(int P){
Fac_tot=0;
int v=P-1;
for (int i=1;prime[i]*prime[i]<=v&&i<=pcnt;i++)
if (v%prime[i]==0){
Fac_p[++Fac_tot]=prime[i];
while (v%prime[i]==0)
v/=prime[i];
}
if (v>1)
Fac_p[++Fac_tot]=v;
for (int i=2;;i++)
if (Get_g_Check(P,i))
return i;
return -1;
}
struct hash_map{
static const int Ti=233,mod=1<<16;
int cnt,k[mod+1],v[mod+1],nxt[mod+1],fst[mod+1];
int Hash(int x){
int v=x&(mod-1);
return v==0?mod:v;
}
void clear(){
cnt=0;
memset(fst,0,sizeof fst);
}
void update(int x,int a){
int y=Hash(x);
for (int p=fst[y];p;p=nxt[p])
if (k[p]==x){
v[p]=a;
return;
}
k[++cnt]=x,nxt[cnt]=fst[y],fst[y]=cnt,v[cnt]=a;
return;
}
int find(int x){
int y=Hash(x);
for (int p=fst[y];p;p=nxt[p])
if (k[p]==x)
return v[p];
return 0;
}
int &operator [] (int x){
int y=Hash(x);
for (int p=fst[y];p;p=nxt[p])
if (k[p]==x)
return v[p];
k[++cnt]=x,nxt[cnt]=fst[y],fst[y]=cnt;
return v[cnt]=0;
}
}Map;
int BSGS(int A,int B,int P){
// Solve x : A^x = B (mod p)
// ans = aM+b
int M=max((int)(sqrt(1.0*P)),1),AM=Pow(A,M,P),AI=Pow(A,P-2,P);
Map.clear();
for (int b=0,pw=B;b<M;b++,pw=1LL*pw*AI%P)
Map.update(pw,b+1);
for (int a=0,pw=1;a<P;a+=M,pw=1LL*pw*AM%P){
int v=Map.find(pw);
if (v)
return a+v-1;
}
return -1;
}
int exgcd(int a,int b,int &x,int &y){
if (!b){
x=1,y=0;
return a;
}
int res=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return res;
}
vector <int> ans;
void Get_ans(int a,int c,int p){
// ax = c (mod p)
// ax + py = c
int x,y,g=exgcd(a,p,x,y);
ans.clear();
if (c%g)
return;
a/=g,c/=g;
int P=p/g;
x=(1LL*x*c%P+P)%P;
while (x<p)
ans.push_back(x),x+=P;
}
int main(){
Get_prime(1e5);
scanf("%d",&T);
while (T--){
scanf("%d%d%d",&P,&A,&B);
g=Get_g(P);
int t=BSGS(g,B,P);
Get_ans(A,t,P-1);
if (ans.size()<1)
puts("No Solution");
else {
for (vector <int> :: iterator i=ans.begin();i!=ans.end();i++)
(*i)=Pow(g,*i,P);
sort(ans.begin(),ans.end());
for (vector <int> :: iterator i=ans.begin();i!=ans.end();i++)
printf("%d ",*i);
puts("");
}
}
return 0;
}

  

#include <bits/stdc++.h>
using namespace std;
const int N=105;
int T,A,B,P;
int Fac_p[N],Fac_tot,g;
int Pow(int x,int y,int mod){
int ans=1;
for (;y;y>>=1,x=1LL*x*x%mod)
if (y&1)
ans=1LL*ans*x%mod;
return ans;
}
bool Get_g_Check(int P,int x){
for (int i=1;i<=Fac_tot;i++)
if (Pow(x,(P-1)/Fac_p[i],P)==1)
return 0;
return 1;
}
int Get_g(int P){
Fac_tot=0;
int v=P-1;
for (int i=2;i*i<=v;i++)
if (v%i==0){
Fac_p[++Fac_tot]=i;
while (v%i==0)
v/=i;
}
if (v>1)
Fac_p[++Fac_tot]=v;
for (int i=2;;i++)
if (Get_g_Check(P,i))
return i;
return -1;
}
unordered_map <int,int> Map;
int BSGS(int A,int B,int P){
// Solve x : A^x = B (mod p)
// ans = aM+b
int M=(int)sqrt(1.0*P),AM=Pow(A,M,P),AI=Pow(A,P-2,P);
Map.clear();
for (int b=0,pw=B;b<M;b++,pw=1LL*pw*AI%P)
Map[pw]=b+1;
int Alim=(P+M-1)/M;
for (int a=0,pw=1;a<Alim;a++,pw=1LL*pw*AM%P)
if (Map[pw])
return a*M+Map[pw]-1;
return -1;
}
int exgcd(int a,int b,int &x,int &y){
if (!b){
x=1,y=0;
return a;
}
int res=exgcd(b,a%b,y,x);
y-=(a/b)*x;
return res;
}
vector <int> ans;
void Get_ans(int a,int c,int p){
// ax = c (mod p)
// ax + py = c
int x,y,g=exgcd(a,p,x,y);
ans.clear();
if (c%g)
return;
a/=g,c/=g;
int P=p/g;
x=(1LL*x*c%P+P)%P;
while (x<p)
ans.push_back(x),x+=P;
}
int main(){
scanf("%d",&T);
while (T--){
scanf("%d%d%d",&P,&A,&B);
g=Get_g(P);
int t=BSGS(g,B,P);
Get_ans(A,t,P-1);
if (ans.size()<1)
puts("No Solution");
else {
for (vector <int> :: iterator i=ans.begin();i!=ans.end();i++)
(*i)=Pow(g,*i,P);
sort(ans.begin(),ans.end());
for (vector <int> :: iterator i=ans.begin();i!=ans.end();i++)
printf("%d ",*i);
puts("");
}
}
return 0;
}

  

51Nod1038 X^A Mod P 数论 原根 BSGS的更多相关文章

  1. 51Nod1039 N^3 Mod P 数论 原根 BSGS

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1039.html 题目传送门 - 51Nod1039 题意 题解 这题我用求高次剩余的做法,要卡常数. ...

  2. Codeforces 1106F Lunar New Year and a Recursive Sequence (数学、线性代数、线性递推、数论、BSGS、扩展欧几里得算法)

    哎呀大水题..我写了一个多小时..好没救啊.. 数论板子X合一? 注意: 本文中变量名称区分大小写. 题意: 给一个\(n\)阶递推序列\(f_k=\prod^{n}_{i=1} f_{k-i}b_i ...

  3. 51Nod1123 X^A Mod B 数论 中国剩余定理 原根 BSGS

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1123.html 题目传送门 - 51Nod1123 题意 $T$ 组数据. 给定 $A,B,C$,求 ...

  4. [数论]原根与指标,BSGS

    刚学了这方面的知识,总结一下.推荐学习数论方面的知识还是看书学习,蒟蒻看的是<初等数论>学的. 这里也推荐几个总结性质的博客,学习大佬的代码和习题. 原根:https://blog.csd ...

  5. BZOJ2219 数论之神 数论 中国剩余定理 原根 BSGS

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2219.html 题目传送门 - BZOJ2219 题意 求同余方程 $x^A\equiv B \pmo ...

  6. BZOJ2480 Spoj3105 Mod 数论 扩展BSGS

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2480.html 题目传送门 - BZOJ2480 题意 已知数 $a,p,b$ ,求满足 $a^x≡b ...

  7. 【BZOJ 1319】 Sgu261Discrete Rootsv (原根+BSGS+EXGCD)

    1319: Sgu261Discrete Roots Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 389  Solved: 172 Descriptio ...

  8. 【解高次同余方程】51nod1038 X^A Mod P

    1038 X^A Mod P 基准时间限制:1 秒 空间限制:131072 KB 分值: 320 X^A mod P = B,其中P为质数.给出P和A B,求< P的所有X. 例如:P = 11 ...

  9. 【CF913G】Power Substring 数论+原根

    [CF913G]Power Substring 题意:T组询问,每次给定一个数a,让你求一个k,满足$2^k$的10进制的后$min(100,length(k))$位包含a作为它的子串.你只需要输出一 ...

随机推荐

  1. WAV文件有多大?MP3文件有多大?使用Lame 压缩比是多少?

    一.说明: 录音文件大小多少?用什么存比较合?我有500G的硬盘存录音能存多久?...... 这些东西常用常忘,索性一次性就分析清楚记下来,方便以后查阅,如果能帮到大家那就更好了. 二.计算方法:   ...

  2. C++编程题

    1.不用系统提供的字符串转int的功能,将一个字符串转换为对应的值 #include <iostream> using namespace std; static int StringTo ...

  3. Confluence 6 Cron 表达式

    一个 cron 表达式是以 6-7 时间字段来定义一个计划任务是如何按照时间被执行的.每一个字段中的数据库而已为数字或者是一些特定的字符串来进行表达.每一个字段是使用空格或者 tab 进行分隔的. 下 ...

  4. Confluence 6 目录中的数据库

    所有的其他数据库,包括有页面,内容都存储在数据库中.如果你安装的 Confluence 是用于评估或者你选择使用的是 Embedded H2 Database 数据库.数据库有关的文件将会存储在 da ...

  5. mysql服务器没有响应

    第一步删除c:\windowns下面的my.ini(可以先改成其它的名字也行) 第二步打开对应安装目录下\mysql\bin\winmysqladmin.exe 输入用户名 和密码(也可以忽略此步) ...

  6. Max Sum (dp)

    Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. F ...

  7. 攻击WordPress和其他程序

    1.SAAS服务(Software as a Service )管理其他作为内容管理的一个人框架的软件,http://www.turnkeylinx.org 上发布了很多测试应用的程序.WordPre ...

  8. java测试

    //信1705-1 20173527 刘津鑫package money;import java.io.IOException;import java.io.Serializable;import ja ...

  9. C++ Primer 笔记——转发

    某些函数需要将其一个或多个实参连同类型不变的转发给其他函数,这种情况下我们需要保持被转发实参的所有性质,包括实参类型是否是const的以及实参是左值还是右值. template <typenam ...

  10. C++ Primer 笔记——变量

    1. 初始化不是赋值,初始化的含义是创建变量时赋予其一个初始值,而赋值的含义是把对象的当前值擦除,而以一个新值来代替. 2.使用列表初始化内置类型的变量时,如果初始值存在丢失信息的风险,则编译器将报错 ...