A^x = D (mod P)

0 <= x <= M, here M is a given integer.

1 <= A, P < 2^31, 0 <= D < P, 1 <= M < 2^63

----------------------------------------------------------

裸拓展baby step giant step

先转成非拓展版,然后如果在转的过程中就出解了,则return 1,否则就找出=D的起点和循环节长度,然后直接求解。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
const int Hash_MOD = 1000003;
typedef long long LL;
typedef unsigned long long ULL;
LL times;
struct link
{
LL link;
int val,next;
}es[100000];
struct triple
{
LL x,y,g;
triple(const LL _x = 0,const LL _y = 0,const LL _g = 0):
x(_x),y(_y),g(_g){}
};
int H[Hash_MOD * 2],ec;
triple exgcd(const LL a,const LL b)
{
if (!b) return triple(1,0,a);
const triple last(exgcd(b,a%b));
return triple(last.y,last.x - a / b * last.y,last.g);
}
LL rem_equ(const LL a,const LL b,const LL c)
{
//ax == c (mod b)
//ax mod b == c
const triple tmp(exgcd(a,b));
const LL MOD = (b / tmp.g);
return ((tmp.x * (c / tmp.g) % MOD) + MOD) % MOD;
}
LL find(const LL x)
{
for (int i = H[x%Hash_MOD]; i; i = es[i].next)
if (es[i].link == x) return es[i].val;
return -1;
}
void insert(LL x,const LL v)
{
if (find(x) != -1) return;
const LL key = x % Hash_MOD;
es[++ec].next = H[key];
H[key] = ec;
es[ec].link = x;
es[ec].val = v;
}
LL BSGS(LL A,LL P,LL D)
{
LL AA = 1 % P,x = 1 % P,MOD = P,DD = D,m = static_cast<LL>(std::ceil(std::sqrt((double)P)));
times = 0;
for (triple tmp; (tmp = exgcd(A,P)).g != 1; ) {
if (x == DD) return times++;
if (D % tmp.g) return -1;
P /= tmp.g;
D /= tmp.g;
(AA *= A / tmp.g) %= P;
++times;
(x *= A) %= MOD;
}
A %= P;
ec = 0;
memset(H,0,sizeof H);
LL tmp = 1 % P;
for (int i = 0; i < m; ++i,(tmp *= A) %= P)
insert(tmp,i);
x = 1 % P;
for (LL i = 0; i < m; ++i,(x *= tmp) %= P) {
const int j = find(rem_equ(AA*x%P,P,D));
if (j != -1) return i * m + j + times;
}
return -1;
}
LL getphi(LL x)
{
LL res = 1;
for (LL i = 2; i * i <= x; ++i)
if (x % i == 0) {
x /= i;
res *= i - 1;
while (x % i == 0) x /= i,res *= i;
}
if (x > 1) res *= x - 1;
return res;
}
LL power(LL a,LL k,LL MOD)
{
//a ^ k % MOD
LL res = 1 % MOD;
for (; k; k/=2,(a *= a) %= MOD)
if (k & 1) (res *= a) %= MOD;
return res;
}
LL calc_len(const LL start,const LL A,const LL P,const LL D)
{
//A ^ (start + *this) == A ^ start == D (mod P)
LL phi = getphi(P),res = phi;
for (LL i = 2; i * i <= phi; ++i) {
for (; phi % i == 0; phi /= i);
for (; res % i == 0 && power(A,start + res/i,P) == D; res /= i);
}
if (phi > 1)
for (; res % phi == 0 && power(A,start + res/phi,P) == D; res /= phi);
return res;
}
ULL work(const LL A,const LL P,const LL D,const ULL M)
{
LL start = BSGS(A,P,D);
//printf("%lld\n",start);
if (start == -1 || start > M) return 0;
else if (start < times) return 1;
// LL phi=getphi(P);
// if (power(A,start + phi,P) != D) return 1;
ULL len = calc_len(start,A,P,D);
return (M - start) / len + 1;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("3254.in","r",stdin);
freopen("3254.out","w",stdout);
#endif
LL A,P,D;
ULL M;
while (~scanf("%lld%lld%lld%llu",&A,&P,&D,&M))
printf("%llu\n",work(A%P,P,D,M));
}

[ZOJ3254] MON 9.2009Secret Code的更多相关文章

  1. 【zoj3254】Secret Code

    题意: 给出a.p.d.m 求a^x=d(mod p) 0<=x<=m 的解的个数 题解: 今天一整天的时间大部分都在调这题Orz BSGS什么的还是太不熟了 我们可以用BSGS拓展版求出 ...

  2. Visual Studio Code 代理设置

    Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器,在十多年的编程经历中,我使用过非常多的的代码编辑器(包括 IDE),例如 Fron ...

  3. 我们是怎么做Code Review的

    前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大家一起分享.探讨.我们为什么要推行Code ...

  4. Code Review 程序员的寄望与哀伤

    一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...

  5. 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM

    刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...

  6. 在Visual Studio Code中配置GO开发环境

    一.GO语言安装 详情查看:GO语言下载.安装.配置 二.GoLang插件介绍 对于Visual Studio Code开发工具,有一款优秀的GoLang插件,它的主页为:https://github ...

  7. 代码的坏味道(14)——重复代码(Duplicate Code)

    坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...

  8. http status code

    属于转载 http status code:200:成功,服务器已成功处理了请求,通常这表示服务器提供了请求的网页 404:未找到,服务器未找到 201-206都表示服务器成功处理了请求的状态代码,说 ...

  9. Visual Studio Code——Angular2 Hello World 之 2.0

    最近看到一篇用Visual Studio Code开发Angular2的文章,也是一篇入门教程,地址为:使用Visual Studio Code開發Angular 2專案.这里按部就班的做了一遍,感觉 ...

随机推荐

  1. 理解 CSS 中的伪元素 :before 和 :after

    CSS 的主要目的是给 HTML 元素添加样式,然而,在一些案例中给文档添加额外的元素是多余的或是不可能的.事实上 CSS 中有一个特性允许我们添加额外元素而不扰乱文档本身,这就是“伪元素”. 你一定 ...

  2. 20155117王震宇 2016-2017-2 《Java程序设计》第十周学习总结

    教材学习内容总结 Java和Android开发学习指南(第二版)(EPUBIT,Java for Android 2nd) 第22章 网络 {{屏幕快照 2017-04-30 下午8.38.06.pn ...

  3. 【洛谷 P2120】 [ZJOI2007]仓库建设(斜率优化)

    题目链接 斜率优化+1,好吧不水分了. 玩具装箱那题以后再做,当作复习吧. \(f[i]=f[j]-(sum[i]-sum[j])*dis[i]+p[i]\) \(f[j]=-dis[i]*sum[j ...

  4. 原 jQuery中document的ready和load事件的区别?

    概述: 大家在工作中用jQuery的时候一定会在使用之前这样:   1 2 3 4 5 6 7 8 //document ready $(document).ready(function(){     ...

  5. 如何把一篇Word文档里的所有换行符去掉?

    编辑-查找,查找框输入 ^13替换框不输入点击全部替换

  6. linux的防火墙管理

    换oricle-linux7系统后,发现iptables的管理方法有不小的改动,记录一下遇到的问题. iptables linux系统已经默认安装了iptables和firewalld两款防火墙管理工 ...

  7. django【ORM】 通过外键字段找对应类

    两个方法其实是一种,用哪个都行,看实例:   方法一: 从list_filter中的字符串,找到model对象的字段,然后得到这个外键对应的类 循环,把list_filter中对应的类所有对象 方法二 ...

  8. OTA之流式更新及shell实现

    在OTA升级时,需要从网络下载OTA包,并写到flash上的对应分区中. 最简单的方式是将下载与更新分离,先将完整的数据包下载到本地,再将本地的OTA包更新到flash上.方便可靠. 但这种方式的问题 ...

  9. Machine Learning系列--归一化方法总结

    一.数据的标准化(normalization)和归一化 数据的标准化(normalization)是将数据按比例缩放,使之落入一个小的特定区间.在某些比较和评价的指标处理中经常会用到,去除数据的单位限 ...

  10. Codeforces Round #456 (Div. 2)

    Codeforces Round #456 (Div. 2) A. Tricky Alchemy 题目描述:要制作三种球:黄.绿.蓝,一个黄球需要两个黄色水晶,一个绿球需要一个黄色水晶和一个蓝色水晶, ...