【洛谷 P3846】 [TJOI2007]可爱的质数 (BSGS)
2024-09-04 14:56:14
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
typedef long long ll;
int a, b, p;
int fast_pow(int n, int k){ //n^k%p
int ans = 1;
while(k){
if(k & 1) ans = (ll)ans * n % p;
n = (ll)n * n % p;
k >>= 1;
}
return ans;
}
int BSGS(){ //a^x≡b(mod p)
map <int, int> hash; hash.clear();
int t = ceil(sqrt(p)), val = b, j = 1;
for(int i = 0; i < t; ++i){
hash[val] = i;
val = (ll)val * a % p;
}
a = fast_pow(a, t);
if(!a) return !b ? 1 : -1;
for(int i = 0; i <= t; ++i){
int k = hash.find(j) == hash.end() ? -1 : hash[j];
if(k >= 0 && (i * t - k) >= 0) return i * t - k;
j = (ll)j * a % p;
}
return -1;
}
int main(){
scanf("%d%d%d", &p, &a, &b);
int ans = BSGS();
if(ans == -1) printf("no solution\n");
else printf("%d\n", ans);
return 0;
}
最新文章
- 初始Java DVD项目
- 【转】Win7 64bit Oracle 11g 使用PL/SQL Developer 连接时提示“SQL*Net not properly installed”
- C#学习之Stream
- Codeforces Gym 101138 D. Strange Queries
- node read file fs
- MyEclipse 2014GA 新建 Web Project 并配置 SSH
- 【HDOJ】1313 Round and Round We Go
- HBase MemStoreFlusher
- 第9课_1_cluster安装
- 通过案例掌握Spring 管理事务的步骤及配置
- 微信支付 v 3.3.6
- FZU 2086 餐厅点餐
- Hadoop 核心架构
- SQL Server-聚焦什么时候用OPTION(COMPILE)呢?
- Java高级特性之枚举
- IDEA导入项目jar包红线、依赖问题....
- ArrayList循环遍历并删除元素的常见陷阱
- Django学习笔记之表单验证
- 页面报错时隐藏Tomcat信息
- javascript对象的属性,方法,prototype作用范围分析.
热门文章
- 位运算 &; 网络序字节序
- session、token、cookie的区别
- Java IO学习--File类
- Python 学习笔记之 Numpy 库——文件操作
- HDU 4782 Beautiful Soup (模拟+注意细节)
- 【iOS开发】iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用
- Redis能做什么?不能做什么?
- Zebra - zebra command to get printer error and warning status
- DataGridView使用
- 正则表达式之旅_sed_awk