【BZOJ】【3239】Discrete Logging
BSGS
BSGS裸题,嗯题目中也有提示:求a^m (mod p)的逆元可用快速幂,即 pow(a,P-m-1,P) * (a^m) = 1 (mod p)
/**************************************************************
Problem: 3239
User: Tunix
Language: C++
Result: Accepted
Time:208 ms
Memory:2872 kb
****************************************************************/ //BZOJ 3239
#include<cmath>
#include<map>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
int getint(){
int v=,sign=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') sign=-; ch=getchar();}
while(isdigit(ch)) {v=v*+ch-''; ch=getchar();}
return v*sign;
}
/*******************template********************/
typedef long long LL;
LL pow(LL a,LL b,LL P){
LL r=,base=a%P;
while(b){
if (b&) r=r*base%P;
base=base*base%P;
b>>=;
}
return r;
}
LL log_mod(LL a,LL b,LL P){
LL m,v,e=;
m=ceil(sqrt(P+0.5));
v=pow(a,P-m-,P);
map<LL,LL>x;
x[]=;
for(int i=;i<m;++i){
e=e*a%P;
if(!x.count(e)) x[e]=i;
}
rep(i,m){
if (x.count(b)) return (LL) i*m+x[b];
b=b*v%P;
}
return -;
}
int main(){
LL P,b,n;
while(scanf("%lld%lld%lld",&P,&b,&n)!=EOF){
b%=P; n%=P;
if (!b && !n) {printf("1\n"); continue;}
if (!b) {printf("no solution\n"); continue;}
LL ans=log_mod(b,n,P);
if (ans==-){printf("no solution\n");continue;}
printf("%lld\n",ans);
}
return ;
}
【BZOJ】【3239】Discrete Logging的更多相关文章
- 【Bzoj 1835 基站选址】
基站选址的区间里隐藏着DP优化的机密…… 分析: 不论是做过乘积最大还是石子合并,或者是其他的入门级别的区间DP题目的人呐,大米并认为读题后就能够轻松得出一个简洁明了的Dp转移方程. ...
- 【BZOJ 2744 朋友圈】
Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 1570 Solved: 532[Submit][Status][Discuss] Descripti ...
- 【BZOJ 5038 不打兔子】
Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 22 Solved: 8[Submit][Status][Discuss] Description 勤 ...
- 【BZOJ 1088 扫雷Mine】模拟
http://www.lydsy.com/JudgeOnline/problem.php?id=1088 2*N的扫雷棋盘,第二列的值a[i]记录第 i 个格子和它8连通的格子里面雷的数目. 第一列的 ...
- 【BZOJ做题记录】07.07~?
在NOI一周前重开一个坑 最后更新时间:7.08 07:38 7.06 下午做的几道CQOI题: BZOJ1257: [CQOI2007]余数之和sum:把k mod i写成k-k/i*i然后分段求后 ...
- 【bzoj5050】【bzoj九月月赛H】建造摩天楼
讲个笑话,这个题很休闲的. 大概是这样的,昨天看到这个题,第一眼星际把题目看反了然后感觉这是个傻逼题. 后来发现不对,这个修改一次的影响是很多的,可能导致一个数突然可以被改,也可能导致一个数不能被改. ...
- 【BZOJ 4151 The Cave】
Time Limit: 5 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 293 Solved: 144[Submit][Status][Di ...
- 【BZOJ 2458 最小三角形】
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1551 Solved: 549[Submit][Status][Discuss] Descripti ...
- 【BZOJ 5000 OI树】
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 107 Solved: 64[Submit][Status][Discuss] Description ...
- 【BZOJ 5047 空间传送装置】
Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 282 Solved: 121[Submit][Status][Discuss] Descriptio ...
随机推荐
- Salt 与Salt API配置
1.安装 [root@localhost yum.repos.d]# cat /etc/yum.repos.d/salt.repo [saltstack-repo] name=SaltStack re ...
- 006 python的面向对象基础
1.类 描述具有相同属性与方法的对象的集合. 2.创建类 使用class来创建一个新类,class之后为类的名称并以冒号结尾 3.程序 #!/usr/bin/python # -*- coding: ...
- CI框架的事务开启、提交和回滚
1.运行事务 $this->db->trans_start(); // 开启事务$this->db->query('一条SQL查询...');$this->db-> ...
- bootm命令移植
<bootm作用> 为linux内核的启动准备条件 <bootloader作用总结> (1)初始化软/硬件(内存硬件/外部设备/堆栈) (2)启动操作系统 <uImagi ...
- anaconda安装tensorflow后pip安装jieba出错的问题
安装jieba出错,参考https://www.cnblogs.com/minsons/p/7872647.html TypeError: parse() got an unexpected keyw ...
- [BZOJ5298][CQOI2018]交错序列(DP+矩阵乘法)
https://blog.csdn.net/dream_maker_yk/article/details/80377490 斯特林数有时并没有用. #include<cstdio> #in ...
- [BZOJ4487][JSOI2015]染色问题(容斥)
一开始写了7个DP方程,然后意识到这种DP应该都会有一个通式. 三个条件:有色行数为n,有色列数为m,颜色数p,三维容斥原理仍然成立. 于是就是求:$\sum_{i=0}^{n}\sum_{j=0}^ ...
- 1 Scala基本概念 +IDE
Scala基本概念 +IDE 推荐:<Scala编程> 1 基本概念 在Spark开发中,Scala被认为是目前和Spark兼容最好的语言. Scala运行在标准的java平台,可以与ja ...
- Remove-Invalid-Parentheses-题解
题意 Remove the minimum number of invalid parentheses in order to make the input string valid. Return ...
- Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心
E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...