• 题目链接:
    https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5479

  • 分析

    题目就是让我们解高次同余方程 ax≡b(modP)a^x ≡ b ( mod P) , 不过p最大为2的13次方,所以可以直接模拟做。但是解高次同余方程还是由模板的。

  • 解高次同余方程模板:(ax≡b(modP),0≤x<Pa^x ≡ b ( mod P), 0≤ x

    )的Baby Step Giant Step算法,参考此处

#include<iostream>
#include<map>
#include<cmath>
#include <cstdio>
using namespace std;
typedef long long LL;
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 P;
int a,b;
int main()
{scanf("%d", &P);while(~scanf("%d", &a) && a ){scanf("%d", &b);int Ans = BabyStep(a , b, P);if(Ans<0)cout << 0 << endl;elsecout << Ans << endl;}return 0;
}
  • 模拟做法:
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))const  int maxn = 10000;
int vis[maxn];
int P;
int a,b;int main()
{scanf("%d", &P);while(~scanf("%d", &a) && a ){scanf("%d", &b);CLR(vis);a%=P, b&=P;int flag = 0;int tmp = 1;for(int i=1;i<=maxn;i++ ){tmp = (tmp*a) %P;if(tmp == b){flag = i;break;}else if(vis[tmp]) break;else vis[tmp] = 1;}cout << flag << endl;}return 0;
}

数学 ( 解高次同余方程 )——Discrete Logarithm Problem ( UVA 7457 )相关推荐

  1. 证明与计算(2): 离散对数问题(Discrete logarithm Problem, DLP)

    本文链接,随时更新请不要转载以免过期:证明与计算(2): 离散对数问题(Discrete logarithm Problem, DLP) - ffl - 博客园 离散对数问题,英文是Discrete ...

  2. POJ3243 Clever Y 解 高次同余方程

    解高次同余方程A^x≡B(mod C)算法流程 S1:i从0到100循环,如果满足A^i≡B(mod C),那么i就为所求,否则继续S2: S2:令d=0,D=1,执行如下循环: while((tmp ...

  3. 数论 —— 高次同余方程与 BSGS 算法

    [概述] BSGS(Baby Step Giant Step)算法,又称大小步算法,其主要用于解形如  的高次同余方程中的 x,其核心思想是分块. 当 A 与 C 互质时,通过费马小定理: 可知,当  ...

  4. (扩展)BSGS与高次同余方程

    前言: 今天更BSGS算法.俗称大步小步算法(Big-Step G--Step),又称拔山盖世.北上广深.白色狗屎 . 问题: 求解指数同余方程:ax≡b(modp)a^{x}\equiv b(mod ...

  5. 信息安全数学基础-素数模高次同余方程 2021-10-09

    8.素数模高次同余方程 1. 素数模的高次同余方程 问题的引出 一般素数p模同余方程 f ( x ) = a 0 x n + . . . + a n ≡ 0 ( m o d p ) , p 不 整 除 ...

  6. 同余——同余方程+线性同余方程+高次同余方程

    传送门:203. 同余方程 - AcWing题库 思路:应用欧几里得算法求 代码: #include<bits/stdc++.h> using namespace std; typedef ...

  7. c++函数模板_高考数学解答题得分模板——三角函数与解三角形

    数学解答题是高考数学试卷中非常重要的题型,通常有 6 个大题,分值在 70 分及以上,例如历年的课标全国卷,解答题为 6 道题,分值为 70 分,几乎占总分 150 分的一半.解答题的考点相对较多.综 ...

  8. Practical Zero-Knowledge Protocols Based on the Discrete Logarithm Assumption 学习笔记 1

    1. 引言 Stephanie Bayer 2013年博士论文 <Practical Zero-Knowledge Protocols Based on the Discrete Logarit ...

  9. 管理联考数学-“穿针引线法”解高次不等式

    步骤: (1)移项,使等式一侧为零. (2)因式分解,并使每个因式的最高次项均为正数. (3)令每个因式都为零,得到零点,并标注在数轴上. (4)如果有恒大于零的项,直接删掉. (5)穿线:从右往左, ...

最新文章

  1. an unsupported operation was attempted问题解决
  2. QT的QBrush类的使用
  3. [LeetCode]29 两数相除和一个小坑点
  4. 2021-03-14Java大数据Week2
  5. 九爷 带你了解 Memcache工作原理总结
  6. 深度学习行人检测简介_深度学习简介
  7. signature=0fa666ae90cad1ed3ef6de6b7db4e5a3,A meta-analysis on correlations of
  8. 《自己动手写网络爬虫》读书笔记
  9. 用R语言进行Cox回归生存分析
  10. AUBO E系列教育科研型机器人QA--持续更新中
  11. 如何获取easyclick手机安装包
  12. Java中类对象为空是什么意思?
  13. 论文精读《Prototypical Networks for Few-shot Learning》
  14. 计算机系统基础 - Lab1
  15. windows 快速搭建EMQ 教程
  16. Aspose.word java 实现word转pdf
  17. 【ucharts】超详细介绍使用ucharts图表
  18. MySQL MySQL进阶路:从小工到专家的必读书籍和必备工具
  19. 数据库关系建模(ER图设计关系表)
  20. 神秘国度的爱情故事--数据结构课程设计

热门文章

  1. 【CEGUI】CEGUI入门篇之初始化(一)
  2. 电商搜索引擎的架构设计和性能优化
  3. 如何配置一台深度学习主机?
  4. 软件工程--详细设计说明书格式
  5. 360网盟邀请码有钱联盟百度有钱联盟
  6. 【技术干货】PCB焊盘设计之问题详解
  7. cmsv6服务器提示超过系统管理数目,科汛KesionCMS V6广告管理系统使用手册
  8. stm32 薄膜键盘原理_6.4 STM32F103ZET独立按键功能深入剖析(神舟III号)
  9. VS2022兼容.net framework 4.0等版本
  10. 有用facs做计算机表情识别的嘛,基于肌肉运动的人脸表情识别-计算机应用技术专业论文.docx...