看别人做的很简单我也不知道是怎么写出来的

自己拿到这道题的想法就是模为素数,那必然有原根r ,将a看做r^a , b看做r^b
那么只要求出幂a,b就能得到所求值a,b

自己慢慢化简就会发现可以抵消n
然后扩展欧几里得解决,一个个枚举所有模的情况。。。。

中间利用了欧拉准则可以知道 对所有奇素数而言: a^((p-1)/2) = -1(mod p)

利用上述准则,这样就不用baby_step giant_step的办法了

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N = ; LL P , k1,b1 , k2;
bitset<N> prime;
int p[N],pri[N];
int k,cnt; void isprime()
{
prime.set();
for(int i=; i<N; i++)
{
if(prime[i])
{
p[k++] = i;
for(int j=i+i; j<N; j+=i)
prime[j] = false;
}
}
} void Divide(int n)
{
cnt = ;
int t = (int)sqrt(1.0*n+0.5);
for(int i=; p[i]<=t; i++)
{
if(n%p[i]==)
{
pri[cnt++] = p[i];
while(n%p[i]==) n /= p[i];
}
}
if(n > )
pri[cnt++] = n;
} void gcd(LL a , LL b , LL &d , LL &x , LL &y)
{
if(!b){d=a;x=;y=;}
else{gcd(b,a%b,d,y,x);y-=x*(a/b);}
} LL inv(LL a , LL n)
{
LL d,x,y;
gcd(a,n,d,x,y);
return d==?(x+n)%n:-;
} LL quick_mod(LL a,LL b,LL m)
{
LL ans = ;
a %= m;
while(b)
{
if(b&)
{
ans = ans * a % m;
b--;
}
b >>= ;
a = a * a % m;
}
return ans;
} LL pow_mod(LL a , LL p , LL n)
{
LL ret= ;
while(p){
if(p&) ret = ret*a%n;
a = a*a%n;
p>>=;
}
return ret;
} LL mul_mod(LL a , LL b , int n)
{
return a*b%n;
} //int log_mod(int a , int b , int n)
//{
// int m , v , e=1 , i;
// m = (int)sqrt(n+0.5);
// v = inv(pow_mod(a,m,n) , n);
// map<int,int>x;
// x.clear();
// x[1] = 0;
// for(i=1 ; i<m ; i++){
// e = mul_mod(e , a , n);
// if(!x.count(e)) x[e]=i;
// }
// for(i=0 ; i<m ; i++){
// if(x.count(b)) return i*m+x[b];
// b = mul_mod(b,v,n);
// }
// return -1;
//} #define pii pair<int,int>
pii ans[N]; int main()
{
// freopen("a.in" , "r" , stdin);
int cas = ;
isprime();
//>>k1>>b1>>k2
while(cin>>P>>k1>>b1>>k2)
{
printf("Case #%d:\n" , ++cas);
int tot = ;
/*P==2另外处理*/
if(P==){
printf("%d %d\n" , ,);
continue;
}
if(P==){
printf("%d %d\n" , ,);
continue;
}
Divide(P-); for(int g=; g<P; g++)
{
bool flag = true;
for(int i=; i<cnt; i++)
{
int t = (P - ) / pri[i];
if(quick_mod(g,t,P) == )
{
flag = false;
break;
}
}
if(flag)
{
LL root = g;
LL val = (P-)/;
// cout<<root<<" "<<val<<endl;
LL d,x,y;
gcd(b1+k1 , - , d , x , y);
x = x*val , y = y*val; x = ((x%(P-))+(P-))%(P-);
y = ((y%(P-))+(P-))%(P-);
for(int i= ; i<P- ; i++){ if((x*k1-y*k2)%(P-)==){
LL a = pow_mod(root , x , P);
LL b = pow_mod(root , y , P);
if(a>&&b>) ans[tot++] = make_pair((int)a , (int)b);
}
x = (x+)%(P-);
y = (y+b1+k1)%(P-);
}
break;
}
}
sort(ans , ans+tot);
tot = unique(ans , ans+tot)-ans;
if(tot==) puts("-1");
else{
for(int i= ; i<tot ; i++) printf("%d %d\n" , ans[i].first , ans[i].second);
}
}
return ;
}

HDU5478 原根求解的更多相关文章

  1. 原根求解算法 && NTT算法

    原根求解算法: 获取一个数\(N\)的原根\(root\)的算法 #include<bits/stdc++.h> #define ll long long #define IL inlin ...

  2. bzoj2219: 数论之神

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  3. codeforces#536题解

    CodeForces#536 A. Lunar New Year and Cross Counting Description: Lunar New Year is approaching, and ...

  4. 【bzoj2219-数论之神】求解x^a==b(%n)-crt推论-原根-指标-BSGS

    http://www.lydsy.com/JudgeOnline/problem.php?id=2219 弄了一个晚上加一个午休再加下午一个钟..终于ac..TAT 数论渣渣求轻虐!! 题意:求解 x ...

  5. x^a=b(mod c)求解x在[0,c-1]上解的个数模板+原根求法

    /************************************* 求解x^a=b(mod c) x在[0,c-1]上解的个数模板 输入:1e9>=a,b>=1,1e9>= ...

  6. 51nod1135(求最小原根)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1135 题意:中文题诶- 思路:设m是正整数,a是整数,若a模 ...

  7. 数论算法 剩余系相关 学习笔记 (基础回顾,(ex)CRT,(ex)lucas,(ex)BSGS,原根与指标入门,高次剩余,Miller_Rabin+Pollard_Rho)

    注:转载本文须标明出处. 原文链接https://www.cnblogs.com/zhouzhendong/p/Number-theory.html 数论算法 剩余系相关 学习笔记 (基础回顾,(ex ...

  8. CF1106F Lunar New Year and a Recursive Sequence 原根、矩阵快速幂、BSGS

    传送门 好久没写数论题了写一次调了1h 首先发现递推式是一个乘方的形式,线性递推和矩阵快速幂似乎都做不了,那么是否能够把乘方运算变成加法运算和乘法运算呢? 使用原根!学过\(NTT\)的都知道\(99 ...

  9. 数论入门2——gcd,lcm,exGCD,欧拉定理,乘法逆元,(ex)CRT,(ex)BSGS,(ex)Lucas,原根,Miller-Rabin,Pollard-Rho

    数论入门2 另一种类型的数论... GCD,LCM 定义\(gcd(a,b)\)为a和b的最大公约数,\(lcm(a,b)\)为a和b的最小公倍数,则有: 将a和b分解质因数为\(a=p1^{a1}p ...

随机推荐

  1. 关于Socket的经验小结

    前言 IM通信在互联网发展到现在已经是码农的世界里人尽皆知的技术,特别在当下移动互联网迅猛发展的时代这种技术的开发也更加火热,其中老牌的代表作就有QQ和MSN,和最近新崛起的微信,默默,易信,来往等眼 ...

  2. ListView 使用详解

    在ListView中设置Selector为null会报空指针?mListView.setSelector(null);//空指针试试下面这种:mListView.setSelector(new Col ...

  3. jquery实现全选、反选、不选

    <!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8&quo ...

  4. 工作了3年的JAVA程序员应该具备什么技能?(zhuan)

    http://www.500d.me/article/5441.html **************************************** 来源:五百丁 作者:LZ2016-03-18 ...

  5. ios中javascript直接调用oc代码而非通过改变url回调方式(转)

    之前一个ios项目中,需要通过UIWebview来打开一个静态页面,并在静态页面中 调用相关object-c代码. 一.以前使用js调用object-c的方法 关于如何使用javascript调用ob ...

  6. Selenium Grid 运行报错 Exception thrown in Navigator.Start first time ->Error forwarding the new session Empty pool of VM for setup Capabilities

    Selenium Grid 运行报错 : Exception thrown in Navigator.Start first time ->Error forwarding the new se ...

  7. PhoneGap插件开发流程

    前几天写了一个PhoneGap插件,这个插件的功能很简单,就是开启viewport设置.不过与其它插件相比,有好几个有意思的地方,仔细读了PhoneGap的源码才搞定.这里记录一下PhoneGap插件 ...

  8. NOI LINUX装机记

    装了差不多一天啊!! 首先自己用虚拟光驱来运行,然后莫名其妙就炸了. 搞到最后刻了一个盘. 然后装完linux之后发现回不到windows7了. 网上找各种资料. 最后搞了个root,再启动的文件中加 ...

  9. 【hdu2196】Computer

    hdu 2196 computer 题意 给你一棵树,边有权值. 对于每一个点,求其与其距离最远的点的距离. 分析 思路1:树的直径 利用直径的性质进行求解,网上资料很多,这里不赘述. #includ ...

  10. 能源项目xml文件标签释义--DataSource

    <bean id="dataSource1" class="org.apache.tomcat.jdbc.pool.DataSource" destroy ...