题目大意:给定A,B,C,求最小的非负整数x,使A^x==B(%C)

传说中的EXBSGS算法0.0 卡了一天没看懂 最后硬扒各大神犇的代码才略微弄懂点0.0

參考资料: http://quartergeek.com/bsgs/

http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4

这两位写的比較具体0.0 能够用于參考

对拍时发现自己代码各种脑残0.0 伤不起啊

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define M 1001001
using namespace std;
typedef long long ll;
typedef pair<ll,ll> abcd;
ll A,B,C,D,hash_table[M],val[M],tim[M],tot;
int Hash(ll x)
{
int pos=x%M;
while(1)
{
if(tim[pos]!=tot)
tim[pos]=tot,hash_table[pos]=-1,val[pos]=0x3f3f3f3f;
if(hash_table[pos]==-1||hash_table[pos]==x)
return hash_table[pos]=x,pos;
else
++pos,pos%=M;
}
}
int Get_Hash(ll x)
{
int pos=x%M;
while(1)
{
if(tim[pos]!=tot)
tim[pos]=tot,hash_table[pos]=-1;
if(hash_table[pos]==-1)
return -1;
if(hash_table[pos]==x)
return pos;
else
++pos,pos%=M;
}
}
ll GCD(ll x,ll y)
{
return y?GCD(y,x%y):x;
}
abcd EXGCD(ll x,ll y)
{
if(!y) return abcd(1,0);
abcd temp=EXGCD(y,x%y);
return abcd(temp.second,temp.first-x/y*temp.second);
}
ll Inverse(ll x)
{
ll temp=EXGCD(x,C).first;
return (temp%C+C)%C;
}
ll Extended_Big_Step_Giant_Step()
{
ll i,m,cnt=0,temp,base=1;
int pos;
B%=C;
for(i=0,temp=1%C;i<=50;i++,temp*=A,temp%=C)
if(temp==B)
return i;
D=1;
while(temp=GCD(A,C),temp!=1)
{
if(B%temp)
return -1;
++cnt;
B/=temp;
C/=temp;
D*=A/temp;
D%=C;
}
B*=Inverse(D);B%=C;
m=(ll)ceil(sqrt(C)+1e-5);
++tot;
for(i=0,temp=1%C;i<m;i++,temp*=A,temp%=C)
pos=Hash(temp),val[pos]=min(val[pos],i);
for(i=1,base=1%C;i<=m;i++,base*=A,base%=C);
for(i=0,D=1%C;i<m;i++,D*=base,D%=C)
{
temp=EXGCD(D,C).first*B;
temp=(temp%C+C)%C;
pos=Get_Hash(temp);
if(~pos)
return i*m+val[pos]+cnt;
}
return -1;
}
int main()
{
memset(hash_table,0xff,sizeof hash_table);
while(cin>>A>>C>>B,A||B||C)
{
ll ans=Extended_Big_Step_Giant_Step();
if(ans==-1)
puts("No Solution");
else
cout<<ans<<endl;
}
}

POJ 3243 Clever Y Extended-Baby-Step-Giant-Step的更多相关文章

  1. POJ 3243 Clever Y (求解高次同余方程A^x=B(mod C) Baby Step Giant Step算法)

    不理解Baby Step Giant Step算法,请戳: http://www.cnblogs.com/chenxiwenruo/p/3554885.html #include <iostre ...

  2. POJ 3243 Clever Y 扩展BSGS

    http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details ...

  3. poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】

    扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...

  4. POJ 3243 Clever Y(离散对数-拓展小步大步算法)

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

  5. poj 3243 Clever Y 高次方程

    1 Accepted 8508K 579MS C++ 2237B/** hash的强大,,还是高次方程,不过要求n不一定是素数 **/ #include <iostream> #inclu ...

  6. [POJ 3243]Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

  7. POJ 3243 Clever Y | BSGS算法完全版

    题目: 给你A,B,K 求最小的x满足Ax=B (mod K) 题解: 如果A,C互质请参考上一篇博客 将 Ax≡B(mod C) 看作是Ax+Cy=B方便叙述与处理. 我们将方程一直除去A,C的最大 ...

  8. 解高次同余方程 (A^x=B(mod C),0<=x<C)Baby Step Giant Step算法

    先给出我所参考的两个链接: http://hi.baidu.com/aekdycoin/item/236937318413c680c2cf29d4 (AC神,数论帝  扩展Baby Step Gian ...

  9. POJ 2417 Discrete Logging ( Baby step giant step )

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 3696   Accepted: 1727 ...

随机推荐

  1. python supper()函数

    参考链接:https://www.runoob.com/python/python-func-super.html super() 函数是用于调用父类(超类)的一个方法. class Field(ob ...

  2. 用JS中的cookie实现商品的浏览记录

    最近在做一个购物车效果,为了实现商品的浏览记录效果可是让我百般周折,避免以后忘记特写此随笔与大家共享,希望博友们看后有所收获. 第一步:在一个公用的js文件下getCookie(“liulan”),c ...

  3. jquery中$.get()提交和$.post()提交有区别

    jquery中$.get()提交和$.post()提交有区别吗? 相同点:都是异步请求的方式来获取服务端的数据: 异同点: 1.请求方式不同:$.get() 方法使用GET方法来进行异步请求的.$.p ...

  4. 【Codeforces Round #462 (Div. 1) B】A Determined Cleanup

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设\(设f(x)=a_d*x^{d}+a_{d-1}*x^{d-1}+...+a_1*x+a_0\) 用它去除x+k 用多项式除法除 ...

  5. Linux下Rootkit的另类检测

     Linux下Rootkit的另类检测 当黑客获取管理员权限时,首先是抹掉入侵系统的相关记录,并且隐藏自己的行踪,要实现这一目的最常用的方法就是使用Rootkits,简单的说,Rootkits是一种经 ...

  6. [Chromium文档转载,第006章]Chrome IPC To Mojo IPC Cheat Sheet

    For Developers‎ > ‎Design Documents‎ > ‎Mojo‎ > ‎ Chrome IPC To Mojo IPC Cheat Sheet 目录 1 O ...

  7. secureCRT 小技巧

    破解: keygen.exe 放到安装目录下填好姓名公司 ,patch后generate就行了 连接问题: 连接出现socket error很有可能是你的防火墙没关,今天排查了很久才发现是这个问题,浪 ...

  8. java关于File.separator

    写好代码在模拟环境下测试,完全没问 题:但linux+tomcat下用的时候,却老是报告“No such file or diretory ”的异常,上传不了.后来发现是文件路径的问题.我的模拟测试环 ...

  9. python爬虫批量抓取ip代理

    使用爬虫抓取数据时,经常要用到多个ip代理,防止单个ip访问太过频繁被封禁.ip代理可以从这个网站获取:http://www.xicidaili.com/nn/.因此写一个python程序来获取ip代 ...

  10. 彻查网络局部网段内Ping时断时续的问题

    前两天须要安装2台server,结果前期一直有问题的网络又来了,明明vlan内能ping通,可是与vlan外却ping不同. 这个现象非常像是arp病毒,于是周末的时间我们就进行了一次彻底的排查,一定 ...