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

题意:裸题。。。

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

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

 #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std; typedef long long LL;
const int SIZE=;
LL K,P,N;
int bl;
struct node{
LL d,id;
}bit[SIZE]; bool cmp(node x,node y){
if(x.d==y.d) return x.id<y.id;
return x.d<y.d;
} LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(b==) {x=,y=;return a;}
LL tx,ty;
LL d=exgcd(b,a%b,tx,ty);
x=ty;y=tx-(a/b)*ty;
return d;
} LL find(LL x)
{
int l=,r=bl;
while(l<=r)
{
int mid=(l+r)>>;
if(bit[mid].d==x) return bit[mid].id;
if(bit[mid].d<x) l=mid+;
if(bit[mid].d>x) r=mid-;
}
return -;
} LL exBSGS()
{
if(N>=P) return -;
LL t,m,a,b,c,g,k,x,y,add,pm,am;
k=;a=K;b=N;c=P;add=;t=;
for(int i=;i<=;i++)//在约分前
{
if(t%c==b) return i;
t=t*a%c;
}
while((g=exgcd(K,c,x,y)) != )
{
k=(k*K/g)%c;
c/=g;//不是mod
if(b%g) return -;
b/=g;add++;
}
m=(LL)(ceil((double)sqrt((double)c)));
pm=%c;bit[].d=k%c;bit[].id=;
for(int i=;i<=m;i++)
{
bit[i].d=bit[i-].d*a%c;
bit[i].id=i;
pm=pm*a%c;
}
sort(bit,bit++m,cmp);
bl=;
for(int i=;i<=m;i++)
{
if(bit[i].d!=bit[bl].d) bit[++bl]=bit[i];
}
exgcd(pm,c,x,y);
am=x%c+c;//x
t=b%c;
for(int i=;i<=m;i++)
{
x=find(t);
if(x!=-) return i*m+x+add;
t=t*am%c;
}
return -;
} int main()
{
// freopen("a.in","r",stdin);
// freopen("a.out","w",stdout);
while(scanf("%I64d%I64d%I64d",&K,&P,&N)!=EOF)
{
LL ans=exBSGS();
if(ans==-) printf("Orz,I can’t find D!\n");
else printf("%I64d\n",ans);
}
return ;
}

hdu2815

最新文章

  1. 在Node.js使用mysql模块时遇到的坑
  2. linux添加私有的ip
  3. [转]使用Stopwatch类实现高精度计时
  4. TCP控制拥塞的四种算法:慢开始,拥塞避免,快重传,快恢复
  5. 成功在神舟K650c-i7 d2(i7-4700MQ、HM87)上装好了Windows XP
  6. 【推荐】iOS集合视图的可重新排序的layout
  7. Nginx 实现AJAX跨域请求
  8. 新鲜出炉的30个精美的 jQuery &amp; CSS3 效果【附演示和教程】
  9. alloc &amp; init &amp; dealloc
  10. WPF&nbsp;接收exe传的值
  11. C#委托,事件,匿名委托
  12. Android自定义View之音频条形图
  13. WCF(远程服务器返回错误: 400 错误的请求)
  14. QtCreator 断点不起作用
  15. 地址栏输入url按回车发生了什么
  16. wzyxidian Scanner 与 Readable 的read()方法
  17. about Version Control(版本控制)
  18. TCP三路握手,本质是一个通信原理相关的问题
  19. python-opencv 图像二值化,自适应阈值处理
  20. shell_sctipts: 删除mysql备份到最后7日

热门文章

  1. 找到一个学习bootstrap的好网站
  2. Linq之查询表达式语法详解
  3. 11.Warning (332060): Node: pi_fck3p was determined to be a clock but was found without an associated clock assignment.
  4. 查找bad sql的方法:
  5. Linux C 文件与目录3 文件读写
  6. objective-c自学总结(一)---面向对象
  7. 微软职位内部推荐-Software Engineer II-SDP
  8. xml之XSLT
  9. linux free 命令
  10. htop