题目:Broot


题意:给出k,m,newx的值,求方程x^k(mod m)=newx的解,其中m为素数。


解法步骤:

(1)先暴力求m的原根g

(2)大步小步求g^t1(mod m)=newx

(3)则g^(t1+n*t2)(mod m)=newx,t2=m-1

(4)x=g^y(mod m),x^k=(g^y)^k=g^(yk)=g^(t1+n*t2)

那么就是求同于方程yk=t1(mod t2),求出y之后带入x=g^y(mod m)解出x


#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <map> using namespace std;
typedef long long LL; const int N=1000005; int p[N];
bool prime[N];
int num,cnt;
LL k,m,newx,g;
LL a[65],b[65]; void isprime()
{
num=0;
int i,j;
memset(prime,true,sizeof(prime));
for(i=2;i<N;i++)
{
if(prime[i])
{
p[num++]=i;
for(j=i+i;j<N;j+=i)
{
prime[j]=false;
}
}
}
} LL multi(LL a,LL b,LL m)
{
LL ans=0;
while(b)
{
if(b&1)
{
ans=(ans+a)%m;
b--;
}
b>>=1;
a=(a+a)%m;
}
return ans;
} LL quick_mod(LL a,LL b,LL m)
{
LL ans=1;
a%=m;
while(b)
{
if(b&1)
{
ans=multi(ans,a,m);
b--;
}
b>>=1;
a=multi(a,a,m);
}
return ans;
} void factor(LL n)
{
cnt=0;
for(int i=0;(LL)p[i]*p[i]<=n;i++)
{
if(n%p[i]==0)
{
a[cnt]=p[i];
int c=0;
while(n%p[i]==0)
{
c++;
n/=p[i];
}
b[cnt++]=c;
}
}
if(n>1)
{
a[cnt]=n;
b[cnt++]=1;
}
} LL extend_Euclid(LL a,LL b,LL &x,LL &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
LL gd=extend_Euclid(b,a%b,x,y);
LL temp=x;
x=y;
y=temp-(a/b)*y;
return gd;
} LL Inv(LL n,LL p)
{
return quick_mod(n,p-2,p);
} bool dfs(int dept,LL t)
{
if(dept==cnt)
{
LL ans=quick_mod(g,t,m);
if(ans==1&&t!=m-1) return false;
return true;
}
LL tmp=1;
for(int i=0;i<=b[dept];i++)
{
if(!dfs(dept+1,t*tmp)) return false;
tmp*=a[dept];
}
return true;
} void find()
{
factor(m-1);
for(g=2;;g++)
if(dfs(0,1)) break;
} LL log_x(LL a,LL b,LL p)
{
map<LL,int>x;
LL z=(LL)ceil(sqrt(p*1.0));
LL v=Inv(quick_mod(a,z,p),p);
LL e=1;
x[1]=0;
for(int i=1;i<z;i++)
{
e=multi(e,a,p);
if(!x.count(e))
x[e]=i;
}
for(int i=0;i<z;i++)
{
if(x.count(b))
return i*z+x[b];
b=multi(b,v,p);
}
return -1;
} LL sol[1005]; void Solve(LL a,LL b,LL n)
{
LL d,x,y;
d=extend_Euclid(a,n,x,y);
if(b%d) puts("-1");
else
{
n/=d;b/=d;
sol[0]=(x*b%n+n)%n;
for(int i=1;i<d;i++)
sol[i]=sol[i-1]+n;
for(int i=0;i<d;i++)
sol[i]=quick_mod(g,sol[i],m);
sort(sol,sol+d);
for(int i=0;i<d;i++)
cout<<sol[i]<<endl;
}
} int main()
{
int t=1;
isprime();
while(cin>>k>>m>>newx)
{
find();
LL t1=log_x(g,newx,m);
LL t2=m-1;
cout<<"case"<<t++<<":"<<endl;
Solve(k,t1,t2);
}
return 0;
}

HDU3930(离散对数与原根)的更多相关文章

  1. 原根&离散对数简单总结

    原根&离散对数 1.原根 1.定义: 定义\(Ord_m(a)\)为使得\(a^d\equiv1\;(mod\;m)\)成立的最小的d(其中a和m互质) 由欧拉定理可知: \(Ord\le\P ...

  2. 不忘初心,方得始终——NOIP2016前的感悟

    不忘初心,方得始终 袛园精舍钟声响,奏诸世事本无常.沙罗双树失花色,盛者转衰如沧桑.骄者难久,恰如春宵一梦.猛者遂灭,好似风前之尘.    ——题记   人生中最令人恐惧的恐怕就是选择了,现在的你拥有 ...

  3. 数论day1 —— 基础知识(们)

    [pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61632537 向大(hei)佬(e)势力学(di ...

  4. HDU3930 (原根)

    给定方程 X^A = B (mol C)  ,求 在[0,C) 中所有的解 , 并且C为质数. 设 rt 为 C 的原根 , 则 X = rt^x  (这里相当于求 A^x =B (mol C) 用大 ...

  5. BZOJ 3992: [SDOI2015]序列统计 [快速数论变换 生成函数 离散对数]

    3992: [SDOI2015]序列统计 Time Limit: 30 Sec  Memory Limit: 128 MBSubmit: 1017  Solved: 466[Submit][Statu ...

  6. 证明与计算(2): 离散对数问题(Discrete logarithm Problem, DLP)

    离散对数问题,英文是Discrete logarithm Problem,有时候简写为Discrete log,该问题是十几个开放数学问题(Open Problems in Mathematics, ...

  7. BZOJ.3992.[SDOI2015]序列统计(DP NTT 原根)

    题目链接 \(Description\) 给定\(n,m,x\)和集合\(S\).求\(\prod_{i=1}^na_i\equiv x\ (mod\ m)\)的方案数.其中\(a_i\in S\). ...

  8. 【数论】【原根】【动态规划】【bitset】2017四川省赛 K.2017 Revenge

    题意: 给你n(不超过200w)个数,和一个数r,问你有多少种方案,使得你取出某个子集,能够让它们的乘积 mod 2017等于r. 2017有5这个原根,可以使用离散对数(指标)的思想把乘法转化成加法 ...

  9. UOJ #86 mx的组合数 (数位DP+NTT+原根优化)

    题目传送门 matthew99神犇的题解讲得非常清楚明白,跪烂Orzzzzzzzzzzzzz 总结一下,本题有很多重要的突破口 1.Lucas定理 看到n,m特别大但模数特别小时,容易想到$lucas ...

随机推荐

  1. printf与++的puzzle

    int b = 0; int c = 0; int main(int argc, const char *argv[]) { printf("%d %d %d %d %d",b,b ...

  2. Utility Classes Are Evil

    原文地址:http://alphawang.com/blog/2014/09/utility-classes-are-evil/ This post is a summary of this arti ...

  3. git上自然框架源码

    [自然框架]终于把源码弄到git上吗了 2015-02-02 14:38 by 金色海洋(jyk)阳光男孩, 183 阅读, 6 评论, 收藏, 编辑 好久没写博客了,发现又从左面的排名里掉出去了. ...

  4. java中Integer包装类的具体解说(java二进制操作,全部进制转换)

    程序猿都非常懒,你懂的! 今天为大家分享的是Integer这个包装类.在现实开发中,我们往往须要操作Integer,或者各种进制的转换等等.我今天就为大家具体解说一下Integer的使用吧.看代码: ...

  5. 6. SQL Server数据库监控 - 如何告警

    原文:6. SQL Server数据库监控 - 如何告警 常用的告警方式大致有:短信.邮件.应用程序 (beep提示,图标提示,升窗提示等),可是不能一直坐在电脑前看着应用程序,或者用脚本部署监控,根 ...

  6. Spring IOC 之ApplicationContext的其他功能

    正如上面章节所介绍的那样, org.springframework.beans.factory 包提供了管理和操作beans的 基本功能. org.springframework.context包增加 ...

  7. Spring MVC 的 研发之路

    翻译器:intellij idea 一个.创建spring mvcproject   一个. 二. 三. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcX ...

  8. android App Widgets

    http://developer.android.com/guide/practices/ui_guidelines/widget_design.html#design http://develope ...

  9. 专业MySQL数据库管理专家SQL Manager for MySQL发布5.4版本

    SQL Manager for MySQL是一款针对MySQL数据库服务器系统的管理工具.深受数据库管理员的喜欢,其富有艺术感的图形用户界面,即使新手也可以不会为如何使用而感到困扰.近日EMSSoft ...

  10. 如何使用C API来操作UCI

    https://forum.openwrt.org/viewtopic.php?pid=183335#p183335 Compiling UCI as stand alone with an exam ...