原文链接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. [PHP]curl上传多文件

    码一下curl上传多文件的行 5.5之前版本的写法 $file = array( 'pic[0]'=>"@E:\\wwwroot\\10003\\temp_56.ini;type=te ...

  2. awk-for循环简单用法

    文本: [root@VM_0_84_centos ~]# cat sshd.txt 1 2 3 4 5 6 7 8 9 循环打印上述文本 for 循环的固定格式   i=1设置i的初始变量  i< ...

  3. linq基本操作

    一.Linq有两种语法: 1.  方法语法 2.  查询语法 下面举个例子看看这两种方法的区别 比如现在有一个学生类 public class student { public string user ...

  4. html学习——基础分类总结

        1. html     超文本标记语言HyperText Markup Language.html文档基本结构: <!DOCTYPE html><head> <! ...

  5. Centos7 linux 卸载自带安装的jdk 并yum自动安装jdk1.8

    一:卸载系统自带安装的JDK 注:本文参考了< 使用CentOS7卸载自带jdk安装自己的JDK1.8> 通过xshell工具成功连接安装好的虚拟机之后可通过    rpm -qa | g ...

  6. python之通过thread来实现多进程

    代码如下: import threading, time class Test1(threading.Thread): def __init__(self, name): super().__init ...

  7. python之属性描述符与属性查找规则

    描述符 import numbers class IntgerField: def __get__(self, isinstance, owner): print('获取age') return se ...

  8. CF767C Garland--树形dp

    今天无聊的我又来切树形dp了,貌似我与树形dp有仇似的. n个节点的树 第i个节点权值为 n<=10^6 −100<=ai​<=100 问是否能够删除掉两条边,使得该树分成三个不为空 ...

  9. Python学习【第2篇】:Python数据结构

    Python数据结构 1.数字类型 2.字符串 3.列表 4.元组 5.字典 6.集合

  10. 深入分析Zookeeper的实现原理

    zookeeper 的由来 分布式系统的很多难题,都是由于缺少协调机制造成的.在分布式协调这块做得比较好的,有 Google 的 Chubby 以及 Apache 的 Zookeeper.Google ...