调了一周,我真制杖,,,

各种初始化没有设为1,,,我当时到底在想什么???

拓展BSGS,这是zky学长讲课的课件截屏:

是不是简单易懂。PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那是因为聪哥早就会了,所以他觉得学这个没用,信他才怪233

#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
LL A,B,C;
struct node{
static const int mo=100007;
LL a[mo],v[mo];
void clear() {memset(a,-1,sizeof(a));}
LL find(LL val){
LL pos=(val%mo+mo)%mo;
while ((a[pos]!=val)&&(a[pos]!=-1)) pos=(pos+1)%mo;
return pos;
}
void insert(int val,int x){
LL pos=find(val);
if ((a[pos]==-1)||(a[pos]==val)){
a[pos]=val; v[pos]=x;
}
}
LL get(LL val){
LL pos=find(val);
return a[pos]==val?v[pos]:-1;
}
}hash;
LL gcd(LL x,LL y) {LL r=x%y; while (r!=0){x=y; y=r; r=x%y;} return y;}
void exgcd(LL aa,LL bb,LL &x, LL&y){
if (bb==0){
x=1; y=0; return;
}
exgcd(bb,aa%bb,x,y);
LL t=y;
y=x-aa/bb*y;
x=t;
}
LL ni(LL a,LL b){
LL x,y;
exgcd(a,b,x,y);
return (x+C)%C;
}
LL ipow(LL a,LL b,LL c){
LL ans=1,t=a;
while (b){
if (b%2==1) ans=(ans*t)%c;
t=(t*t)%C; b/=2;
}
return ans;
}
void bsgs(){
B%=C;
if (B==1) {puts("0"); return;}
else if ((A==0)&&(B==0)) {puts("1"); return;}
else if (B==0) {puts("No Solution"); return;}
LL t=1;
for(LL i=0;i<=50;++i){
if (t==B){
printf("%I64d\n",i);
return;
}
t=(t*A)%C;
}
LL g,ans1=0,D=1;
while ((g=gcd(A,C))!=1){
if (B%g){
puts("No Solution");
return;
}
++ans1;
B/=g;
C/=g;
D=(A/g*D)%C;
}
D=ni(D,C);
LL m=ceil(sqrt((double)C));
hash.clear();
t=B*D%C;
for(LL i=0;i<=m;++i){
hash.insert(t,i);
t=(t*A)%C;
}
LL basic=ipow(A,m,C);
t=1;
for(LL i=0;i<=m;++i){
int x=hash.get(t);
if (x!=-1){
printf("%I64d\n",ans1+i*m-x);
return;
}
t=(t*basic)%C;
}
puts("No Solution");
}
int main(){
while (scanf("%I64d %I64d %I64d\n",&A,&C,&B)){
if ((A==0)&&(B==0)&&(C==0)) break;
bsgs();
}
return 0;
}

这样就可以啦

【POJ 3243】Clever Y 拓展BSGS的更多相关文章

  1. POJ 3243 Clever Y 扩展BSGS

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

  2. 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 ...

  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 | BSGS算法完全版

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

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

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

  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 Extended-Baby-Step-Giant-Step

    题目大意:给定A,B,C,求最小的非负整数x,使A^x==B(%C) 传说中的EXBSGS算法0.0 卡了一天没看懂 最后硬扒各大神犇的代码才略微弄懂点0.0 參考资料: http://quarter ...

  8. poj 3243 Clever Y 高次方程

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

  9. poj3243 Clever Y[扩展BSGS]

    Clever Y Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8666   Accepted: 2155 Descript ...

随机推荐

  1. codeforces 732

    A. Buy a Shovel time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  2. POJ2184 Cow Exhibition[DP 状态负值]

    Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12420   Accepted: 4964 D ...

  3. Gvr SDK for Unity 分析(二)

    前言 关于google vr sdk的具体使用,传送门 Gvr SDK for Unity 分析(一) Google Daydream平台已经整合进Google VR SDK 本文环境:Unity5. ...

  4. 网页动物园2.0发布,经过几个月的努力,采用JAVA编写!

    网页动物园2.0发布,经过几个月的努力,采用JAVA编写! 网页动物园2.0 正式发布!游戏发布 游戏名称: 网页动物园插件 游戏来源: 原创插件 适用版本: Discuz! X1.5 - X3.5 ...

  5. SQL2012连接字符串

    安装了sqlserver2012数据库,测试一包代码,搜索网上的方法,死活连接不上,最后偶然测试通过,居然是连接字符串的问题. “如果开发软件的时候是在局域网或者是在本机,并且数据库不在远程计算机上面 ...

  6. [转] 如何设置Eclipse的上网代理

    from: http://blog.csdn.net/qq635785620/article/details/8191799 不同版本的eclipse有不同的设置方法 方式一:   默认的Eclips ...

  7. maya 专家模式

    maya中按ctrl + 空格 可以在普通模式与专家模式之间切换,如图 普通模式 按ctrl+空格后进入专家模式 来自为知笔记(Wiz)

  8. HTML 学习笔记 CSS样式(简介和语法)

    CSS概述 CSS指层叠样式表(Cascading Style Sheets) 样式定义如何显示HTML元素 样式通常存储在样式表中 把样式添加到HTML4.0中 是为了解决内容与表现分离的问题 外部 ...

  9. http协议(二)请求和响应报文的构成

    http协议用于客户端和服务器之间的通信,请求访问资源的一方称为客户端,而提供资源响应的一方称为服务器端. 下面就是客户端和服务端之间简单的通信过程 PS:请求必须从客户端建立通信,服务端没收到请求之 ...

  10. 表单 - Form - EasyUI提供的表单异步提交

    方案一 被提交的表单 <form id="loginForm" method="post"> <table align="cente ...