题目链接

\(BSGS\)模板题。。不会点这里

#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;
}

最新文章

  1. 初始Java DVD项目
  2. 【转】Win7 64bit Oracle 11g 使用PL/SQL Developer 连接时提示“SQL*Net not properly installed”
  3. C#学习之Stream
  4. Codeforces Gym 101138 D. Strange Queries
  5. node read file fs
  6. MyEclipse 2014GA 新建 Web Project 并配置 SSH
  7. 【HDOJ】1313 Round and Round We Go
  8. HBase MemStoreFlusher
  9. 第9课_1_cluster安装
  10. 通过案例掌握Spring 管理事务的步骤及配置
  11. 微信支付 v 3.3.6
  12. FZU 2086 餐厅点餐
  13. Hadoop 核心架构
  14. SQL Server-聚焦什么时候用OPTION(COMPILE)呢?
  15. Java高级特性之枚举
  16. IDEA导入项目jar包红线、依赖问题....
  17. ArrayList循环遍历并删除元素的常见陷阱
  18. Django学习笔记之表单验证
  19. 页面报错时隐藏Tomcat信息
  20. javascript对象的属性,方法,prototype作用范围分析.

热门文章

  1. 位运算 &amp; 网络序字节序
  2. session、token、cookie的区别
  3. Java IO学习--File类
  4. Python 学习笔记之 Numpy 库——文件操作
  5. HDU 4782 Beautiful Soup (模拟+注意细节)
  6. 【iOS开发】iOS开发CGRectGetMidX. CGRectGetMidY.CGRectGetMinY. CGRectGetMaxY. CGRectGetMinX. CGRectGetMaxX的使用
  7. Redis能做什么?不能做什么?
  8. Zebra - zebra command to get printer error and warning status
  9. DataGridView使用
  10. 正则表达式之旅_sed_awk