题目大意:给定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. Oracle11g密码忘记处理方法

    c:\>sqlplus /nolog sql>connect / as sysdba sql>alter user 用户名 identified by 密码;(注意在这里输入的密码是 ...

  2. 【UVa 116】Unidirectional TSP

    [Link]:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...

  3. [React] Make Controlled React Components with Control Props

    Sometimes users of your component want to have more control over what the internal state is. In this ...

  4. cigarette

    #include<iostream> using namespace std; int main() { int N; cin>>N; while(N--) { int Who ...

  5. 惊叹计算机运行速度的提升---以n Queens 问题为例

    1 介绍 实现了书<Data Structures and Program design in C++>(Robert L. Kruse and Alexander J. Ryba, 20 ...

  6. Codeforces 528A Glass Carving STL模拟

    题目链接:点击打开链接 题意: 给定n*m的矩阵.k个操作 2种操作: 1.H x 横向在x位置切一刀 2.V y 竖直在y位置切一刀 每次操作后输出最大的矩阵面积 思路: 由于行列是不相干的,所以仅 ...

  7. lightoj--1043-- Triangle Partitioning (水题)

    Triangle Partitioning Time Limit: 500MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu S ...

  8. Sql Server通用分页存储过程

    Sql Server2005通用分页存储过程 CREATE PROCEDURE [dbo].[Common_GetPagedList] ( @TableName nvarchar(100), --表名 ...

  9. 将本地的代码上传到网上SVN库

    使用VisualSVN Server创建版本库: https://jingyan.baidu.com/article/db55b609f6aa724ba20a2f6c.html 最详细的教程: htt ...

  10. 如何解决winxp访问win10共享打印机提示凭据不足

    Winxp访问win10共享的打印机,提示凭据不足时,如何解决,本篇经验将和大家分享下解决的办法,希望对大家的工作有所帮助! 方法/步骤     在win10的电脑上对着始按钮点鼠标右键,点击运行,或 ...