POJ2417 Discrete Logging (离散对数取模)

2月26日 Discrete logging(哈希表+离散对数+BSGS算法) 开学第一天,有点困,不过天气不错,手脚能放开了。今天学习了离散对数的几类问题,也是开始涉及密码学的核心:对于离散对数:$A^x\equiv B \mod p$如果要求B,那就是幂模问题,用快速幂求模。如果未知x,就是一个重要的离散对数/密码学问题,直观的我们想:是不是可以从1枚举到p-1(欧拉函数)?不过这个开销比较大,有一种算法(BSGS,大步小步/北上广深(!?)算法)可以解决这类问题。将x写... 阅读详情

Discrete Logging

Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that

    BL == N (mod P)

Input
Read several lines of input, each containing P,B,N separated by a space.
Output
For each line print the logarithm on a separate line. If there are several, print the smallest; if there is none, print “no solution”.
Sample Input

5 2 1
5 2 2
5 2 3
5 2 4
5 3 1
5 3 2
5 3 3
5 3 4
5 4 1
5 4 2
5 4 3
5 4 4
12345701 2 1111111
1111111121 65537 1111111111

Sample Output

0
1
3
2
0
3
1
2
0
no solution
no solution
1
9584351
462803587

Hint
The solution to this problem requires a well known result in number theory that is probably expected of you for Putnam but not ACM competitions. It is Fermat’s theorem that states

   B(P-1) == 1 (mod P)


for any prime P and some other (fairly rare) numbers known as base-B pseudoprimes. A rarer subset of the base-B pseudoprimes, known as Carmichael numbers, are pseudoprimes for every base between 2 and P-1. A corollary to Fermat's theorem is that for any m

   B(-m) == B(P-1-m) (mod P) .

直接套模板

code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
ll q_pow(ll a, ll b, ll mod){
    ll ans = 1;
    while(b){
        if(b & 1) ans = ans * a % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return ans;
}

ll gcd(ll a,ll b){
    return b ? gcd(b,a%b) : a;
}

void ex_gcd(ll a,ll b,ll &x,ll &y){
    if(!b){
        x = 1;
        y = 0;
        return;
    }
    ex_gcd(b,a%b,y,x);
    y -= a / b * x;
    return;
}

ll BSGS(ll a, ll b, ll p) {
    a %= p; b %= p;
    map<ll, ll> h;
    map<ll,bool> vis;//这道题必须多加一个map作为标记,否则直接查找会超时!!
    ll m = ceil(sqrt(p)), x, y, d, t = 1, v = 1;
    for(ll i = 0; i < m; ++i) {
        if(h.count(t)) h[t] = min(h[t], i);
        else h[t] = i,vis[t] = 1;
        t = (t*a) % p;
    }
    ex_gcd(t,p,x,y);
    ll inv = x > 0 ? x % p : x % p + p;
    for(ll i = 0; i < m; ++i) {
        if(vis[b]) return i*m + h[b];
        b = b * inv % p;
    }
    return -1;
}
int main(){
    ll b,p,n;
    while(~scanf("%lld%lld%lld",&p,&b,&n)){
        if(n >= p){
            puts("no solution");
            continue;
        }
        ll ans = BSGS(b,n,p);
        if(ans == -1) puts("no solution");
        else printf("%lld\n",ans);
    }
    return 0;
}
万字长文告诉新手如何学习Python图像处理(上篇完结 四十四) | 「Python」有奖征文 这篇文章是图像处理的最后一篇文章,后面我们将进入新的章节。图像处理文章主要讲解的图像处理方法包括图像几何运算、图像量化采样、图像点运算、图像形态学处理、图像增强、图像平滑、图像锐化、图像特效、图像分割、傅里叶变换与霍夫变换、图像分类等。个人感觉如果你是编程初学者、Python初学者或图像处理爱好者,这个系列真心适合你学习,并且这篇文章算是Python图像处理的学习路线,希望您喜欢。 阅读详情

相关推荐

别再为下载速度发愁了!手把手教你给qBittorrent/Transmission添加最新可用Tracker(附2024年实测地址清单)

本文提供2024年BT下载加速实战指南,详细解析Tracker工作原理与速度瓶颈,手把手教你为qBittorrent/Transmission添加最新可用Tracker,并附实测地址清单。通过优化配置和精选Tracker列表,显著提升下载速度和稳定性,解决连接延迟和peer数量限制问题。

weixin_28481133的博客 1259

离散对数和椭圆曲线加密原理

序言 现代公钥加密系统中,常用的加密算法除了RSA还有离散对数加密和椭圆曲线加密。这两者原理比较相似,在这里一并介绍。 离散对数问题 我们在中学里学的对数问题是指, 给定正实数a” role=”presentation” style=”position: relative;”&gt;aa。 ...

LiangJishengBlog 2408

别再纠结高侧低侧了!用INA199/INA219这类集成电流检测芯片,5分钟搞定精准电流测量

本文详细解析了使用INA199/INA219等集成电流检测芯片实现精准电流测量的高效方案。通过对比传统分立元件方案,突出集成芯片在精度、温度稳定性和设计简化方面的优势,并提供硬件设计避坑指南和软件实现最佳实践,帮助开发者快速掌握电流检测技术。

weixin_42685438的博客 470

密码学(一)

目录模运算离散对数难题迪菲-赫尔曼秘钥交换 模运算 取模(mod)和取余(rem)的区别 取模运算(mod)和取余运算(rem)两个概念有重叠的部分,但又不完全一致。主要区别在于对负整数进行除法运算时操作不同: 取模运算 在计算商值时 商值向 负无穷方向 舍入,即商取小原则 取余运算 在计算商值时 商值向 0方向 舍入,即商靠0原则 取模主要是用于计算机术语中,取余则更多是数学概念 计算步骤 假设有整数 A 和 B,那么取模(或取余)运算可以分为两步进行: 计算整数商:C = A / B 计算模/

Airths 的博客 1666

python中frida的安装+frida-server(雷电模拟器)保姆级安装教程

frida的安装,安卓逆向,adb

2301_80224944的博客 4579

xenomai内核解析--嵌入式实时linux概述

Linux系统作为一个GPOS(通用操作系统)发展至今已经非常成熟可靠了,并且由于遵循GPL协议,开放所有系统源代码,非常易于裁剪。更重要的是,与其他开源的GPOS或RTOS相比,Linux系统支持多种处理器、开发板,提供多种软件开发工具,同时Linux系统对网络和图形界面的支持非常出色。显然,选择Linux操作系统在产品的开发周期和成本控制方面都有巨大优势。 虽然Linux系统功能强大、实用性强...

wsg1100 3699

取模运算的理解和应用

软件开发过程中,总会遇到取余,取模,取整的计算或者思想,这里小结一下。 取模和取余,这两个运算在真整数的时候是一个概论,在负整数进行除法运算时不同。符号:“ % ”。 取整:即求商。符号:“ / ”。 取模和取余的区别 取余运算,在计算商值时,商值向0方向舍入,靠近0原则。 取模运算,在计算商值时,商值向负无穷方向舍入;尽可能让商值小的原则(不超多商值的最大值)。 例子: 取模 简述 商值 取模值 5 mod 3 = 2 5/3 = 1.66 商取小原则 商=1.

u011749929的博客 1万+

JSP期末考试复习习题及答案

以下习题是对于平时课堂小测的整理,如有错误敬请指正! 欢迎一起交流~ 预祝各位小伙伴们考试顺利! Test1 一. 单选题 – 1.(单选题)当用户请求jsp页面时,JSP引擎就会执行该页面的字节码文件响应客户的请求,执行字节码文件的结果是() A. 发送一个JSP源文件到客户端 B. 发送一个Java文件到客户端 C. 发送一个HTML页面到客户端 D. 什么都不做 – 2.(单选题)下列动态网页和静态网页的根本区别描述错误的是( D) A. 静态网页服务器端返回的HTML文件是事先存储好的 B. 动.

技术小白 2万+

UVA - 11916 Emoogle Grid (离散对数取模)

You have to color an M x N (1M, N108) two dimensional grid. You will be provided K (2K108) different colors to do so. You will also be provided a list of B (0B500)list of blocked cells of this

那些年 1855

微动探测原理及仪器介绍

微动探测原理及仪器介绍 1、微动探测原理 1.1 基本理论 微动是由体波和面波组成,其中微动能量的百分之七十以上都来自于面波,并且它与体波的主要区别是在不均匀的介质中传播时面波会发生频散,而体波不存在频散现象,因此面波是微动探测的主要对象。 对于介质的弹性参数(如横波速度、层薄厚、压缩波速度、密度等),面波传播速度相对比较敏感,特别是对横波的敏感性很高,因此可以利用面波来反演横波速度。 在时间和空间上,微动信号具有以下特性:高度变化、无规律性、无重复性等,有研究发现全世界各处记录的微动信号都有相似

zw461450141的博客 1万+

windows下玩转vllm:在wsl下安装vllm

当前,部署通义千问2.5-vl已经是一件箭在弦上,不得不发的事儿了。网上搜了一下,发现还是用vllm用的比较多。于是乎,这就开始尝试部署vllm。但是我们之前说了,vllm是不支持直接在windows上部署的,我们还得搞wsl或者docker。咱今天先尝试使用wsl进行安装哈。

xiezhipu的博客 4137

[学习笔记] 多项式求逆、带余除法、取模、求对数、求指数

一晚上写完求逆取模求ln求exp真酸爽 明天再顺手把求值插值写了人生就圆满了 求逆: #include&amp;lt;iostream&amp;gt; #include&amp;lt;cstring&amp;gt; #include&amp;lt;cstdio&amp;gt; #include&amp;lt;algorithm&amp;gt; #define gc getchar() #define lint long long #d

Mys_C_K的博客 720

计算机组成原理(第三版)唐朔飞-第一章第二章-课后习题

计算机组成原理

weixin_45735391的博客 7万+

poj2417 Discrete Logging (计算离散对数)

Given a prime P, 2 31, an integer B, 2 BL == N (mod P) Input Read several lines of input, each containing P,B,N separated by a space. Output For each line print the logarithm on a sepa

樱花满地集于我心,楪舞纷飞祈愿相随 503

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

efm32 入门.pdf

本PDF共计22页,介绍的是操作驱动API,下载与否自己斟酌。

POJ 2417 Discrete Logging【BSGS】【离散对数】

题目链接:http://poj.org/problem?id=2417 AC代码: #include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<algorithm> #include<queue> #include<map&...

weixin_43499182的博客 263

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

POJ 2417 Discrete Logging BSGS

Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 4011   Accepted: 1849 Description Given a prime P, 2 31, an integer B, 2 BL == N (mo

码代码的猿猿 919

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

POJ2417 Discrete Logging

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/转载请注明出处,侵权必究,保留最终解释权! Description Given a prime P, 2 <= P < 231...

dgoh41514的博客 161

POJ 2417:Discrete Logging——Baby Step, Giant Step

题意:求最小的Y,满足XY mod Z = K高次同余方程,用hash做,套模板。。从网上找的模板与POJ 3243的区别在于:POJ 2417 保证P是素数且B POJ 3243 就没有这些好条件。这会导致麻烦,待会再说。一:#include #include #include #define maxn 65535 using namespace std; struct hash

Booky的专栏 537

[POJ2417]Discrete Logging

离散对数裸题,给定a,b,p,求 a^x=b(mod p) 其中p为质数.经典的Baby-Step-Giant-Step算法.p为非质数时也能做,不过没找到类似的题- -

Zvezda_的博客 1398

【POJ 2417】 Discrete Logging

【题目链接】 http://poj.org/problem?id=2417 【算法】 Baby-Step,Giant-Step算法 【代码】 #include <algorithm> #include <bitset> #include <cctype&gt...

119

PoJ 2417 Discrete Logging BSGS裸题

DescriptionGiven a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an integer L such that B^L == N

Monica 677

POJ 2417 Discrete Logging 离散对数

链接:http://poj.org/problem?id=2417 题意: 思路:求离散对数,Baby Step Giant Step算法基本应用。 以下转载自:AekdyCoin 【普通Baby Step Giant Step】 【问题模型】 求解 A^x = B (mod C) 中 0 【思路】 我们可以做一个等价 x = i * m + j  ( 0 而

看曙光 1133

【poj2417】Discrete Logging

Time Limit: 5000MS Memory Limit: 65536K Description Given a prime P, 2 &lt;= P &lt; 231, an integer B, 2 &lt;= B &lt; P, and an integer N, 1 &lt;= N &lt; P, compute the discrete loga...

StrideTG的博客 416

POJ 2417 Discrete Logging【大步小步

裸的BSGS 顺便BSGS真的不用求逆元,既然可以设x=ka+b,不如直接设成x=ka-b,乘到那边就完事了 不用求但是还是要用到逆元的思想 以上 #include #include #include #include #include #include #include using namespace std; long long B,N,P; long long mult(lo

Flaze_的博客 547

Discrete Logging(POJ-2417)

Problem Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B < P, and an integer N, 1 <= N < P, compute the discrete logarithm of N, base B, modulo P. That is, find an i...

Alex_McAvoy的博客 378

poj2417 Discrete Logging

http://www.elijahqi.win/archives/3005 Description Given a prime P, 2 &lt;= P &lt; 231, an integer B, 2 &lt;= B &lt; P, and an integer N, 1 &lt;= N &lt; P, compute the discrete logarithm of N, base B...

Elijahqi 171
上一篇: POJ - 2447 RSA (RSA公钥密码体制+PollarRho大整数的因数分解)
下一篇: Mod Tree(扩展BSGS)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值