http://acm.hdu.edu.cn/showproblem.php?pid=2815

题意:裸题。。。

关于拓展BSGS的详细解释我写了一篇博文:http://www.cnblogs.com/KonjakJuruo/p/5178600.html

题解:又有一个坑,就是N>=P的时候输出无解。

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<iostream>
 5 #include<cmath>
 6 #include<algorithm>
 7 using namespace std;
 8
 9 typedef long long LL;
10 const int SIZE=40000;
11 LL K,P,N;
12 int bl;
13 struct node{
14     LL d,id;
15 }bit[SIZE];
16
17 bool cmp(node x,node y){
18     if(x.d==y.d) return x.id<y.id;
19     return x.d<y.d;
20 }
21
22 LL exgcd(LL a,LL b,LL &x,LL &y)
23 {
24     if(b==0) {x=1,y=0;return a;}
25     LL tx,ty;
26     LL d=exgcd(b,a%b,tx,ty);
27     x=ty;y=tx-(a/b)*ty;
28     return d;
29 }
30
31 LL find(LL x)
32 {
33     int l=0,r=bl;
34     while(l<=r)
35     {
36         int mid=(l+r)>>1;
37         if(bit[mid].d==x) return bit[mid].id;
38         if(bit[mid].d<x) l=mid+1;
39         if(bit[mid].d>x) r=mid-1;
40     }
41     return -1;
42 }
43
44 LL exBSGS()
45 {
46     if(N>=P) return -1;
47     LL t,m,a,b,c,g,k,x,y,add,pm,am;
48     k=1;a=K;b=N;c=P;add=0;t=1;
49     for(int i=0;i<=100;i++)//在约分前
50     {
51         if(t%c==b) return i;
52         t=t*a%c;
53     }
54     while((g=exgcd(K,c,x,y)) != 1)
55     {
56         k=(k*K/g)%c;
57         c/=g;//不是mod
58         if(b%g) return -1;
59         b/=g;add++;
60     }
61     m=(LL)(ceil((double)sqrt((double)c)));
62     pm=1%c;bit[0].d=k%c;bit[0].id=0;
63     for(int i=1;i<=m;i++)
64     {
65         bit[i].d=bit[i-1].d*a%c;
66         bit[i].id=i;
67         pm=pm*a%c;
68     }
69     sort(bit,bit+1+m,cmp);
70     bl=0;
71     for(int i=1;i<=m;i++)
72     {
73         if(bit[i].d!=bit[bl].d) bit[++bl]=bit[i];
74     }
75     exgcd(pm,c,x,y);
76     am=x%c+c;//x
77     t=b%c;
78     for(int i=0;i<=m;i++)
79     {
80         x=find(t);
81         if(x!=-1) return i*m+x+add;
82         t=t*am%c;
83     }
84     return -1;
85 }
86
87 int main()
88 {
89     // freopen("a.in","r",stdin);
90     // freopen("a.out","w",stdout);
91     while(scanf("%I64d%I64d%I64d",&K,&P,&N)!=EOF)
92     {
93         LL ans=exBSGS();
94         if(ans==-1) printf("Orz,I can’t find D!\n");
95         else printf("%I64d\n",ans);
96     }
97     return 0;
98 }

hdu2815

转载于:https://www.cnblogs.com/KonjakJuruo/p/5180909.html

【hdu2815-Mod Tree】高次同余方程-拓展BadyStepGaintStep相关推荐

  1. (扩展)BSGS与高次同余方程

    前言: 今天更BSGS算法.俗称大步小步算法(Big-Step G--Step),又称拔山盖世.北上广深.白色狗屎 . 问题: 求解指数同余方程:ax≡b(modp)a^{x}\equiv b(mod ...

  2. 数学 ( 解高次同余方程 )——Discrete Logarithm Problem ( UVA 7457 )

    题目链接: https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show ...

  3. 信息安全数学基础-素数模高次同余方程 2021-10-09

    8.素数模高次同余方程 1. 素数模的高次同余方程 问题的引出 一般素数p模同余方程 f ( x ) = a 0 x n + . . . + a n ≡ 0 ( m o d p ) , p 不 整 除 ...

  4. 同余——同余方程+线性同余方程+高次同余方程

    传送门:203. 同余方程 - AcWing题库 思路:应用欧几里得算法求 代码: #include<bits/stdc++.h> using namespace std; typedef ...

  5. POJ3243 Clever Y 解 高次同余方程

    解高次同余方程A^x≡B(mod C)算法流程 S1:i从0到100循环,如果满足A^i≡B(mod C),那么i就为所求,否则继续S2: S2:令d=0,D=1,执行如下循环: while((tmp ...

  6. 数论 —— 高次同余方程与 BSGS 算法

    [概述] BSGS(Baby Step Giant Step)算法,又称大小步算法,其主要用于解形如  的高次同余方程中的 x,其核心思想是分块. 当 A 与 C 互质时,通过费马小定理: 可知,当  ...

  7. 高次同余方程式的解数及解法

    定理一: 若是k个两两互质的正整数,,则同余式                                            (1) 与同余式组                   (i=1, ...

  8. 全志 android 编译,全志Android SDK编译详解(二)

    注意要确定安装了jdk) 第一步: cd  lichee; ./build.sh  -p sun5i_elite -k 3.0  (apt-get install uboot-mkimage需要安装m ...

  9. 第4章 同余问题《信息学奥赛一本通 提高篇》

    一.同余问题 常用的数论算法(C++描述) 常用的数论算法(C++描述)_linyq@BambooFan ~-CSDN博客_c++数论 数论中的一些基础算法 数论中的一些基础算法_Every day- ...

  10. 省选+NOI 第八部分 数论

    1.线性基 线性回归-线性基函数模型 线性回归-线性基函数模型_哔哩哔哩_bilibili 0219数论寒假作业选讲2[线性基] 0219数论寒假作业选讲2[线性基]_哔哩哔哩_bilibili 线性 ...

最新文章

  1. 刻意练习:LeetCode实战 -- Task26.判断子序列
  2. 面试官问:为什么SpringBoot的 jar 可以直接运行?
  3. Swift 注释规范和文档注释
  4. gradle编译出错:Execution failed for task #39;:app:compileTestDebugJava#39;.
  5. Hive DML操作
  6. oracle 监听拒绝连接,报mybatis映射文件错误Listener refused the connection with the following error: ORA-12519, TNS
  7. python简写匹配_Python简写
  8. inno setup相关 (二)
  9. 礼县职业中等专业学校计算机,礼县职业中等专业学校
  10. 从心理学角度看,如何通过故事影响客户的决策?
  11. 【android自定义控件】属性动画 五
  12. php 工资 2018,2018年PHP程序员的进阶之路
  13. python成语接龙_python——成语接龙小游戏
  14. java ArrayList 排序
  15. linux导航改端口波特率,各大导航端口速率修改方法
  16. goproxy代理软件 v10.3 轻量级高性能代理软件+链式代理+正向代理+反向代理
  17. 属于python语言中合法的二进制整数是_以下选项属于 Python 整数类型的是( )
  18. 小学计算机小蘑菇教案,《会走路的小蘑菇》教案
  19. SUMO仿真教程(3)—— 仿真运行(net file、rou file、sumocfg file)
  20. 音视频直播如何实现低延迟

热门文章

  1. java滥用接口_接口滥用问题
  2. table切换数据 vue_Vue实现表格中对数据进行转换、处理的方法
  3. 使用卡方分箱进行数据离散化-python实现
  4. html每访问一次显示1到10,开方表1到100 根号1到10 分别约等于多少
  5. 2021-09-14基于用 户 行为 序列建模的推荐算法研究
  6. 176.第二高的薪水
  7. java aws批量_通过 S3 Batch Operations (批量操作)功能轻松处理海量对象
  8. matlab频谱分析_想知道有限精度对FFT的影响?Matlab仿真看看
  9. 凸优化有关的数值线性代数知识 作业题
  10. conda安装tensorflow-GPU出现CondaHTTPError: HTTP 000 CONNECTION FAILED for url错误