【bzoj2242】: [SDOI2011]计算器

1.快速幂

2.扩展欧几里得(费马小定理)

3.BSGS

 /* http://www.cnblogs.com/karl07/ */
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
using namespace std; #define LL long long
int T,K;
LL a,b,p; LL Q_tim(LL x,LL y,LL p){
LL ans=;
while (y){
if (y&) ans=(ans+x)%p;
x=(x+x)%p;
y=(y>>);
}
return ans;
} LL Q_pow(LL x,LL y,LL p){
LL ans=;
while (y){
if (y&) ans=ans*x%p;
x=x*x%p;
y=(y>>);
}
return ans;
} void ex_gcd(LL a,LL b,LL &x,LL &y,LL &gcd){
if (b==) {gcd=a;x=;y=;return;}
ex_gcd(b,a%b,y,x,gcd);
y-=x*(a/b);
} void p2(LL a,LL b,LL p){
LL x,y,gcd,g,ans=p+;
ex_gcd(a,p,x,y,gcd);
if (b%gcd!=){ puts("Orz, I cannot find x!"); return;}
ex_gcd(a/gcd,p/gcd,x,y,g);
x=x*(b/gcd)%p;
x=(x+p)%p;
printf("%lld\n",x);
} void BSGS(LL a,LL b,LL p){
if ((a== && b!=) || (a== && b!=)) { puts("Orz, I cannot find x!"); return;}
LL sz=(LL)ceil(sqrt(p)),inv,e=;
map<LL,LL> x;
x.clear();
x[]=;inv=Q_pow(Q_pow(a,sz,p),p-,p);
for (int i=;i<sz;i++) {e=Q_tim(e,a,p); if (!x.count(e)) x[e]=i;}
for (int i=;i<sz;i++) {
if (x.count(b)) {printf("%lld\n",i*sz+x[b]); return;}
b=Q_tim(b,inv,p);
}
puts("Orz, I cannot find x!");
} int main(){
scanf("%d%d",&T,&K);
for (int i=;i<=T;i++){
scanf("%lld%lld%lld",&a,&b,&p);
if (K==) printf("%lld\n",Q_pow(a%p,b,p));
if (K==) p2(a%p,b%p,p);
if (K==) BSGS(a%p,b%p,p);
}
return ;
}

最新文章

  1. [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
  2. python版本升级
  3. dp or 贪心 --- hdu : Road Trip
  4. MTF(Move-to-front transform)数据转换
  5. C#中dataGridView用法集
  6. IE11的API变化
  7. 关于JDBC
  8. 51nod1586 约数和
  9. iOS 非ARC基本内存管理系列 2-多对象内存管理(2)
  10. Win8 移除右键菜单中的SkyDrive Pro选项
  11. c++在函数后面加const
  12. hdu1506——Largest Rectangle in a Histogram
  13. jsScript中的一些操作方法
  14. Leaving Auction
  15. JS第二弹:用Jquery组装html标签,输出到页面
  16. LeetCode 204. Count Primes (质数的个数)
  17. EBS开发技术之Patch安装
  18. 人生第一个过万 Star 的 github 项目诞生
  19. 整理一下python中with的用法
  20. recovery&amp;linux系统升级数据更新分析总结

热门文章

  1. [转载]嵌入式linux下操作GPIO
  2. c# OrderBy 实现List升序降序
  3. CentOS 配置XWIN/VNC
  4. 生产环境该如何选择lvs的工作模式,和哪一种算法
  5. ORACLE——日期时间格式化参数详解 之二
  6. zookeeper 安装配置注意事项
  7. checked多选,取消,反选
  8. oracle create user &amp;tablespace &amp; imp
  9. php中COM函数的使用
  10. [poj2398]Toy Storage