poj3243
来源:互联网 发布:网络摄像头 监控软件 编辑:程序博客网 时间:2024/05/02 04:40
解高次同余方程的应用题
#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>#define LL long longusing namespace std;const int maxn = 65535;struct hash{ int a,b,next;} Hash[maxn << 1];int flg[maxn + 66];int top,idx;void ins(int a,int b){ int k = b & maxn; if(flg[k] != idx) { flg[k] = idx; Hash[k].next = -1; Hash[k].a = a; Hash[k].b = b; return ; } while(Hash[k].next != -1) { if(Hash[k].b == b) return ; k = Hash[k].next; } Hash[k].next = ++ top; Hash[top].next = -1; Hash[top].a = a; Hash[top].b = b;}int find(int b){ int k = b & maxn; if(flg[k] != idx) return -1; while(k != -1) { if(Hash[k].b == b) return Hash[k].a; k = Hash[k].next; } return -1;}int gcd(int a,int b){ return b?gcd(b,a%b):a;}int ext_gcd(int a,int b,int& x,int& y){ int t,ret; if (!b) { x=1,y=0; return a; } ret=ext_gcd(b,a%b,x,y); t=x,x=y,y=t-a/b*y; return ret;}int Inval(int a,int b,int n){ int x,y,e; ext_gcd(a,n,x,y); e=(LL)x*b%n; return e<0?e+n:e;}int pow_mod(LL a,int b,int c){ LL ret=1%c; a%=c; while(b) { if(b&1)ret=ret*a%c; a=a*a%c; b>>=1; } return ret;}int BabyStep(int A,int B,int C){ top = maxn; ++ idx; LL buf=1%C,D=buf,K; int i,d=0,tmp; for(i=0; i<=100; buf=buf*A%C,++i)if(buf==B)return i; while((tmp=gcd(A,C))!=1) { if(B%tmp)return -1; ++d; C/=tmp; B/=tmp; D=D*A/tmp%C; } int M=(int)ceil(sqrt((double)C)); for(buf=1%C,i=0; i<=M; buf=buf*A%C,++i)ins(i,buf); for(i=0,K=pow_mod((LL)A,M,C); i<=M; D=D*K%C,++i) { tmp=Inval((int)D,B,C); int w ; if(tmp>=0&&(w = find(tmp)) != -1)return i*M+w+d; } return -1;}int main(){ int a,b,c; while(~scanf("%d%d%d",&a,&c,&b)&&a&&b&&c) { int temp = BabyStep(a,b,c); if(temp<0) printf("No Solution\n"); else printf("%d\n",temp); } return 0;}
0 0
- poj3243
- POJ3243 Clever Y
- POJ3243 EXT-BSGS算法
- poj3243:Clever Y(BSGS)
- poj3243 Clever Y【BSGS模板】
- 【Baby-step giant-step Algorithm】poj3243,hdu2815
- poj3243 Clever Y 解高次同余方程
- poj 2417 && poj3243(Baby-Step Giant-Step)
- POJ3243 Clever Y【高次同余方程】
- POJ3243 Clever Y 解 高次同余方程
- POJ3243 Clever Y 高次同余方程
- 【BSGS】POJ2417[Discrete Logging]&POJ3243[Clever Y]题解
- POJ3243(Clever Y),POJ2417(Discrete Logging) && HDU2815(高次同余定律)
- ******************** 野指针和内存泄露 ********************
- WPF依赖属性的当前值(Current Value),基值(Base Value)和本地值(Local Value) .
- Struts2中配置处理结果及异常处理
- tomcat dm 连不上数据库
- Redis安装与配置
- poj3243
- AIDL绑定到对方进程后,进程被杀后的现象调研
- Divein _ Trim函数的实现,去除字符串前后空格
- 读图:邮件营销vs社交媒体营销背后不可不知的事实
- ******************** const ********************
- wamp出现问题,logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
- STL源码剖析读书笔记
- 面向对象编程的理解
- find命令总结