【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod

Description

已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x。

Input

    每个测试文件中最多包含100组测试数据。
    每组数据中,每行包含3个正整数a,p,b。
    当a=p=b=0时,表示测试数据读入完全。

Output

    对于每组数据,输出一行。
    如果无解,输出“No Solution”(不含引号),否则输出最小自然数解。

Sample Input

5 58 33
2 4 3
0 0 0

Sample Output

9
No Solution

HINT

100%的数据,a,p,b≤1e9。

题解:EXBSGS

因为A和C不互质,所以我们令A,B,C同时除以gcd(A,C),得到的C'可能与A还不互质,所以我们再除上gcd(A,C'),直到A,C互质。

此时得到方程$A^{x-k}*{A^k\over g_1*g_2...g_k}=B'(mod C')$

将${A^k\over g_1*g_2...g_k}$除过去,然后套用BSGS即可。

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <map>
  5. #include <cmath>
  6. using namespace std;
  7. typedef long long ll;
  8. map<int,int> mp;
  9. int pm(int a,int b,int c)
  10. {
  11. int ret=1;
  12. while(b)
  13. {
  14. if(b&1) ret=(ll)ret*a%c;
  15. a=(ll)a*a%c,b>>=1;
  16. }
  17. return ret;
  18. }
  19. int gcd(int a,int b)
  20. {
  21. return b?gcd(b,a%b):a;
  22. }
  23. int BSGS(int a,int b,int c,int d)
  24. {
  25. int x,y,i;
  26. mp.clear();
  27. int M=ceil(sqrt(c));
  28. for(x=b,i=0;i<=M;i++,x=(ll)x*a%c) mp[x]=i;
  29. for(y=d,x=pm(a,M,c),i=1;i<=M;i++)
  30. {
  31. y=(ll)y*x%c;
  32. int tmp=mp[y];
  33. if(tmp) return (ll)i*M-tmp;
  34. }
  35. return -1;
  36. }
  37. void work(int a,int b,int c)
  38. {
  39. int A=1,k=0;
  40. for(int i=0;(1<<i)<=c;i++)
  41. {
  42. if(pm(a,i,c)==b)
  43. {
  44. printf("%d\n",i);
  45. return ;
  46. }
  47. }
  48. while(1)
  49. {
  50. int g=gcd(a,c);
  51. if(g==1) break;
  52. if(b%g!=0)
  53. {
  54. printf("No Solution\n");
  55. return ;
  56. }
  57. b/=g,c/=g,A=((ll)A*a/g)%c,k++;
  58. }
  59. int tmp=BSGS(a,b,c,A);
  60. if(tmp==-1) printf("No Solution\n");
  61. else printf("%d\n",tmp+k);
  62. }
  63. int main()
  64. {
  65. int a,b,c;
  66. while(1)
  67. {
  68. scanf("%d%d%d",&a,&c,&b);
  69. if(!a&&!b&&!c) return 0;
  70. if(!a&&!b) printf("1\n");
  71. else a%=c,b%=c,work(a,b,c);
  72. }
  73. }

【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS的更多相关文章

  1. 【EX_BSGS】BZOJ1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 425  Solved: 238[Submit][Status ...

  2. bzoj1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 313  Solved: 181[Submit][Status ...

  3. bzoj 1467: Pku3243 clever Y 扩展BSGS

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...

  4. poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】

    扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...

  5. 【POJ】3243 Clever Y

    http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...

  6. 【数论】【ex-BSGS】poj3243 Clever Y

    用于求解高次同余方程A^x≡B(mod C),其中C不一定是素数. http://blog.csdn.net/tsaid/article/details/7354716 这篇题解写得最好. 那啥,这题 ...

  7. 【iOS开发】iOS CGRectGetMaxX/Y 使用

    在iOS的界面布局中我们可以使用CGRectGetMaxX 这个方法来方便的获取当前控件的x坐标值+宽度的数值,这样便可以方便布局. 同理CGRectGetMaxY是获取y坐标值+控件高度的值,当然这 ...

  8. 【小知识】比较 x^y 和 y^x 的大小

    往前翻几个编号相邻的题目翻到了这么一道题,感觉很好奇就做了一下 (upd:我下午问了下出题人做法,他就把题隐藏了……这不太友好啊……所以我补一下题意:) 题意 给你两个整数 $x$ 和 $y$,求 $ ...

  9. 【POJ 2480】Longge's problem(欧拉函数)

    题意 求$ \sum_{i=1}^n gcd(i,n) $ 给定 $n(1\le n\le 2^{32}) $. 链接 题解 欧拉函数 $φ(x)$ :1到x-1有几个和x互质的数. gcd(i,n) ...

随机推荐

  1. net4:GridView中的重要操作(添加checkbox,以及鼠标动作,行颜色等)

    原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...

  2. react-native 判断是不是IPhone X

    import { Platform, Dimensions } from 'react-native'; // iPhoneX const X_WIDTH = 375; const X_HEIGHT ...

  3. est6 -- Object.is()、Object.assign()、Object.defineProperty()、Symbol、Proxy

    Object.is()用来比较两个值是否严格相等.它与严格比较运算符(===)的行为基本一致,不同之处只有两个:一是+0不等于-0,二是NaN等于自身. + === - //true NaN === ...

  4. 第三章 poj 1064——关于带精度的二分法

    /* 题意:给定n个实数l[i],给定一个k 问:求最大的ans,使得sum l[i]/ans i=1 to n >=k,且ans最大*/ #include <iostream> # ...

  5. Java多线程中Lock的使用

    Jdk1.5以后,在java.util.concurrent.locks包下,有一组实现线程同步的接口和类,说到线程的同步,可能大家都会想到synchronized关键字, 这是java内置的关键字, ...

  6. BZOJ2002弹飞绵羊

    动态树LCT模板题 #include<cstdio> #include<cctype> #include<algorithm> using namespace st ...

  7. ApplicationContext介绍

    如果说BeanFactory是Spring的心脏,那么ApplicationContext就是完整的身躯了.ApplicationContext由BeanFactory派生而来,提供了更多面向实际应用 ...

  8. GitBook一个专注于帮助文档的工具

    官网:https://www.gitbook.com GitHub组织:https://github.com/gitbookio GitBook一个专注于帮助文档的工具,比如: 1.简单的左侧列表右侧 ...

  9. java后4位打成*显示

    /** * [固定电话] 后四位,其他隐藏<例子:****1234> * * @param num * @return */ public static String fixedPhone ...

  10. 提交IOS开发效率的几个插件(Xcode神器推荐贴)

    Code Pilot 2 Xcode上的Command-T,讓你快速跳轉到某個文件或某個符號 XVim 讓Xcode使用Vim的鍵綁定,Vim党必備 Injection for Xcode 調試利器, ...