http://poj.org/problem?id=3243

求最小的非负整数y满足x^y=k(mod z)

写完板子之后等待了半个小时poj才终于进入……

poj不行啊.jpg

以前一直觉得BSGS太神啦于是就跳了。

结果回头一看发现异常的简单。

(老年化初步体现flag*1)

首先x^y对k取模随着y的变化有周期性,最大周期不超过k(感性证明吧)

那么最小的y一定是在[0,k)之间了。

我们把这段区间分块,大小为n=sqrt(k),令m=k/n,则y=i*m-a,把y^a预处理移项到右面,hash表存一下就成了O(sqrt(k))的问题了。

是的就这么简单(当然如果存在gcd(x,k)!=1不存在逆元的话还得exgcd处理一下才行)

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cctype>
#include<cstdio>
#include<cmath>
#include<stack>
using namespace std;
typedef long long ll;
const int N=;
const int M=;
struct HASH{
int w,to,nxt;
}h[M];
int cnt,head[N];
inline void add(int x,int y){
int t=x%N;
for(int i=head[t];i;i=h[i].nxt){
int v=h[i].to;
if(v==x){
h[i].w=y;//记大的
return;
}
}
h[++cnt].to=x;h[cnt].w=y;h[cnt].nxt=head[t];head[t]=cnt;
}
inline int query(int x){
int t=x%N;
for(int i=head[t];i;i=h[i].nxt){
int v=h[i].to;
if(v==x)return h[i].w;
}
return -;
}
int gcd(int a,int b){
return (!b)?a:gcd(b,a%b);
}
int exgcd(int a,int b,int &x,int &y){
if(!b){
x=,y=;
return a;
}
int ans=exgcd(b,a%b,y,x);
y-=(ll)(a/b)*x;
return ans;
}
int inv(int a,int c){
int x,y;
exgcd(a,c,x,y);
return (x%c+c)%c;
}
//a^x=b(mod c);
int BSGS(int a,int b,int c){
if(!a){
if(!b)return ;
return -;
}
int tot=,g,d=;
while((g=gcd(a,c))!=){
if(b%g)return -;
++tot;b/=g,c/=g;
d=(ll)d*(a/g)%c;
}
b=(ll)b*inv(d,c)%c;
cnt=;memset(head,,sizeof(head));
int s=sqrt(c),p=;
for(int i=;i<s;i++){
if(p==b)return i+tot;
add((ll)p*b%c,i);
p=(ll)p*a%c;
}
int q=p;
for(int i=s;i-s+<c;i+=s){
int t=query(q);
if(t!=-)return i-t+tot;
q=(ll)q*p%c;
}
return -;
}
int main(){
int x,z,k;
while(scanf("%d%d%d",&x,&z,&k)&&(ll)x+z+k>){
x%=z,k%=z;
int ans=BSGS(x,k,z);
if(ans==-)puts("No Solution");
else printf("%d\n",ans);
}
return ;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/ +

+++++++++++++++++++++++++++++++++++++++++++

POJ3243:Clever Y——题解的更多相关文章

  1. poj3243 Clever Y[扩展BSGS]

    Clever Y Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8666   Accepted: 2155 Descript ...

  2. luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法

    BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...

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

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

  4. 【EX_BSGS】BZOJ1467 Pku3243 clever Y

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

  5. 【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS

    [BZOJ1467/2480]Pku3243 clever Y/Spoj3105 Mod Description 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. Input      ...

  6. bzoj 1467: Pku3243 clever Y 扩展BSGS

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

  7. [拓展Bsgs] Clever - Y

    题目链接 Clever - Y 题意 有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\). 解法 如题,是拓展 \(Bsgs\) 板 ...

  8. bzoj1467 Pku3243 clever Y

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

  9. 【POJ3243】【拓展BSGS】Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

随机推荐

  1. PostgreSQL 使用总结

    1. USING的使用 USING是个缩写的概念:它接收一个用逗号分隔的字段名字列表, 这些字段必须是连接表共有的,最终形成一个连接条件,表示这些字段对必须相同. USING (a, b, c) 等效 ...

  2. iOS 测试工具reveal可视化调试工具的使用

    简单翻译一下reveal可视化图形工具插入项目的官方文档(官方英文版file:///Applications/Reveal.app/Contents/SharedSupport/Documentati ...

  3. Appium-原理、设置

    Appium是支持跨平台的移动端自动化测试框架. 下面介绍下Appium的具体工作流程: 首先,Appium server 默认监听4723端口,监听客户端的命令. 客户端指的是 我们编写的自动化测试 ...

  4. 【带 josn参数的测法】

    遇到json 参数的情况这样写  ,否则就会报错 cod 415   nocookie post请求 ,","email":"beihe@163.com&quo ...

  5. ArrayList与LinkedList的普通for循环遍历

    对于大部分Java程序员朋友们来说,可能平时使用得最多的List就是ArrayList,对于ArrayList的遍历,一般用如下写法: public static void main(String[] ...

  6. 【SSH进阶之路】Struts + Spring + Hibernate 进阶开端(一)

    [SSH进阶之路]Struts + Spring + Hibernate 进阶开端(一) 标签: hibernatespringstrutsssh开源框架 2014-08-29 07:56 9229人 ...

  7. Mybatis中resultMap与resultType区别

    MyBatis中在查询进行select映射的时候,返回类型可以用resultType,也可以用resultMap,resultType是直接表示返回类型的,而resultMap则是对外部ResultM ...

  8. [leetcode-676-Implement Magic Dictionary]

    Implement a magic directory with buildDict, and search methods. For the method buildDict, you'll be ...

  9. es6从零学习(三):Class的基本用法

    es6从零学习(三):Class的基本用法 一:定义一个类 //定义类 class Point { constructor(x, y) { this.x = x; this.y = y; } toSt ...

  10. 软工1816 · Alpha冲刺(1/10)

    团队信息 队名:爸爸饿了 组长博客:here 作业博客:here 组员情况 组员1(组长):王彬 过去两天完成了哪些任务 前后端代码规范统一 针对之前的alpha冲刺安排进一步细化任务卡片 明确apl ...