【POJ 3243】Clever Y 拓展BSGS
调了一周,我真制杖,,,
各种初始化没有设为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的更多相关文章
- POJ 3243 Clever Y 扩展BSGS
http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details ...
- 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 ...
- poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】
扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...
- POJ 3243 Clever Y | BSGS算法完全版
题目: 给你A,B,K 求最小的x满足Ax=B (mod K) 题解: 如果A,C互质请参考上一篇博客 将 Ax≡B(mod C) 看作是Ax+Cy=B方便叙述与处理. 我们将方程一直除去A,C的最大 ...
- POJ 3243 Clever Y(离散对数-拓展小步大步算法)
Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...
- [POJ 3243]Clever Y
Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...
- POJ 3243 Clever Y Extended-Baby-Step-Giant-Step
题目大意:给定A,B,C,求最小的非负整数x,使A^x==B(%C) 传说中的EXBSGS算法0.0 卡了一天没看懂 最后硬扒各大神犇的代码才略微弄懂点0.0 參考资料: http://quarter ...
- poj 3243 Clever Y 高次方程
1 Accepted 8508K 579MS C++ 2237B/** hash的强大,,还是高次方程,不过要求n不一定是素数 **/ #include <iostream> #inclu ...
- poj3243 Clever Y[扩展BSGS]
Clever Y Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8666 Accepted: 2155 Descript ...
随机推荐
- AC日记——独木桥 洛谷 p1007
题目背景 战争已经进入到紧要时间.你是运输小队长,正在率领运输部队向前线运送物资.运输任务像做题一样的无聊.你希望找些刺激,于是命令你的士兵们到前方的一座独木桥上欣赏风景,而你留在桥下欣赏士兵们.士兵 ...
- Android SDK和N多Android开发资源
开发资源 本文收集整理Android开发需要的Android SDK.工具.Android开发教程.Android设计规范,免费的设计素材等. 欢迎大家推荐自己在Android开发过程中用的好用的工具 ...
- Java写的斗地主游戏源码
源码下载在最后 我们的前年的课设要求做一个斗地主程序,当时正在愁如何做界面,当时刚好在学习C#,于是就用C#完成了这个程序.一方面,当时我C#功底还很差(其实现在也不怎么样),很多地方用了“笨办法”, ...
- 多个mysql解决方法
有的时候工作需要之类的,需要多个mysql数据库,而默认开启的服务为c盘windows里的my.ini 1.设置好要用的my.ini2.关闭当前的mysql数据库服务3.将my.ini拷贝到C:\WI ...
- jprofiler安装图解
环境: 1.sun jdk1.6.0 2.jprofiler_windows_6_0_2.exe 安装 1. jdk, 安装略... 2. jprofiler安装 一路next 到Enter lice ...
- JQuery 获取touchstart,touchmove,touchend 坐标
JQuery写法: $('#id').on('touchstart',function(e) { var _touch = e.originalEvent.targetTouches[0]; var ...
- sudo命令使用的几个场景
在linux系统下,普通用户无法直接执行root用户权限下的命令,如果想让普通用户执行只有root用户才能执行的操作命令.下面罗列下经常使用sudo命令的几个场景: 1.用户无权限执行root命令普通 ...
- header
本文分享几个php header函数的例子,有需要的朋友参考学习下. 转自:http://www.jbxue.com/article/php_header_x5hV63c.html 1,可以使用hed ...
- IO操作总结
1,读取文件将文件转换为二进制流 InputStream in = new FileInputStream("C:/test.png"); byte[] photo = new b ...
- GridView EmptyDataTemplate 动态显示
以下语句加在GridView.DataBind()之后: Table GridViewTable = ((Table)gvGridView.Controls[]); if (!isSearch) (( ...