给a^x == b (mod c)求满足的最小正整数x,

用BSGS求,令m=ceil(sqrt(m)),x=im-j,那么a(i*m)=b*aj%p;,

我们先枚举j求出所有的b
aj%p,1<=j<m复杂度O(sqrt(c)),然后枚举1<=i<=m,求出a(im)在ba^j找满足条件的答案,最后的答案就是第一个满足条件的i*m-j,复杂度O(sqrt(c))

//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define cd complex<double>
#define ull unsigned long long
#define base 1000000000000000000
#define fio ios::sync_with_stdio(false);cin.tie(0) using namespace std; const double eps=1e-6;
const int N=1000000+10,maxn=5000+10,inf=0x3f3f3f3f,INF=0x3f3f3f3f3f3f3f3f; map<ll,ll>ma;
inline ll quick(ll a,ll b,ll p)
{
ll ans=1;
while(b)
{
if(b&1)ans=ans*a%p;
a=a*a%p;b>>=1;
}
return ans;
}
int main()
{
ll p,a,b;
while(~scanf("%lld%lld%lld",&p,&a,&b))
{
if(a%p==0)
{
puts("no solution");
continue;
}
ma.clear();
ll m=ceil(sqrt(p));
ll now=b%p;
ma[now]=0;
for(int i=1;i<=m;i++)
{
now=(now*a)%p;
ma[now]=i;
}
ll ans=-1,t=quick(a,m,p);now=1;
for(int i=1;i<=m;i++)
{
now=now*t%p;
if(ma[now])
{
ans=i*m-ma[now];
break;
}
}
if(ans!=-1)printf("%lld\n",(ans%p+p)%p);
else puts("no solution");
}
return 0;
}
/******************** ********************/

poj2417 Discrete Logging BSGS裸题的更多相关文章

  1. 【BZOJ3239】Discrete Logging BSGS

    [BZOJ3239]Discrete Logging Description Given a prime P, 2 <= P < 231, an integer B, 2 <= B ...

  2. POJ2417 Discrete Logging【BSGS】

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5577   Accepted: 2494 ...

  3. [POJ2417]Discrete Logging(指数级同余方程)

    Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...

  4. POJ2417 Discrete Logging

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  5. POJ2417 Discrete Logging【BSGS】(模板题)

    <题目链接> 题目大意: P是素数,然后分别给你P,B,N三个数,然你求出满足这个式子的L的最小值 : BL== N (mod P). 解题分析: 这题是bsgs算法的模板题. #incl ...

  6. POJ2417 Discrete Logging | A,C互质的bsgs算法

    题目: 给出A,B,C 求最小的x使得Ax=B  (mod C) 题解: bsgs算法的模板题 bsgs 全称:Baby-step giant-step 把这种问题的规模降低到了sqrt(n)级别 首 ...

  7. POJ 2417 Discrete Logging BSGS

    http://poj.org/problem?id=2417 BSGS 大步小步法( baby step giant step ) sqrt( p )的复杂度求出 ( a^x ) % p = b % ...

  8. BZOJ 3239 Discrete Logging(BSGS)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3239 [题目大意] 计算满足 Y^x ≡ Z ( mod P) 的最小非负整数 [题解 ...

  9. 【BZOJ】3239: Discrete Logging

    http://www.lydsy.com/JudgeOnline/problem.php?id=3239 题意:原题很清楚了= = #include <bits/stdc++.h> usi ...

随机推荐

  1. 基于GIT的管理

    常用命令 git init : 初始化仓库git add 文件名 :把文件添加到暂存区git commit -m "操作记录" : 提交到仓库,设置相关操作的记录 git stat ...

  2. C++ 电路布线/最短路径问题

    问题描述 用二维数组表示地图,若值为 1 则表示有障碍物,若值为 0 则表示可以通行. 输入: m*n 的二维数组,布线起点坐标,布线终点坐标. 输出: 最短布线距离以及对应的布线路径. 问题分析 从 ...

  3. Centos下yum安装Nginx报错 No package nginx available.

    在Centos6下使用yum安装Nginx报错 解决方案: yum install epel-release

  4. 20145216史婧瑶《网络对抗》Web基础

    20145216史婧瑶<网络对抗>Web基础 实验问题回答 (1)什么是表单 表单在网页中主要负责数据采集功能.一个表单有三个基本组成部分: 表单标签.表单域.表单按钮. (2)浏览器可以 ...

  5. NOIP模拟题 2017.7.3 - 模拟 - 贪心 - 记忆化搜索

    直接暴力模拟,注意判数据结构为空时的取出操作. Code #include<iostream> #include<cstdio> #include<ctime> # ...

  6. VC++禁止标题栏鼠标双击事件

    转载:http://blog.sina.com.cn/s/blog_4ad042e50102dwv0.html 重载DefWindowProc,在里面截获WM_NCLBUTTONDBLCLK消息进行处 ...

  7. Python3基础 str split 用指定的字符将字符串分割

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. linux下精确替换某个字符串

    1.linux下精确替换某个字符串 sed -i 's/\<old\>/new/g' filename.txt 2.举例: 2.1有个文件名为filename.txt,内容如下: newd ...

  9. mysql 通过binlog 查看执行日志

    环境:linux deepin15.7   mysql 5.7 1.开启binlog vim /etc/mysql/mysql.conf.d/mysqld.cnf 添加 log_bin = /var/ ...

  10. 【Android实验】UI设计-Android计算器

    目录 实验目的 实验要求 实验过程 1. 界面设计 2. 功能设计 3. 运算处理 实验目的 自主完成一个简单APP的设计工作,综合应用已经学到的Android UI设计技巧,重点注意合理使用布局 实 ...