POJ3243:Clever Y——题解
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——题解的更多相关文章
- poj3243 Clever Y[扩展BSGS]
Clever Y Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8666 Accepted: 2155 Descript ...
- luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法
BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...
- 【数论】【ex-BSGS】poj3243 Clever Y
用于求解高次同余方程A^x≡B(mod C),其中C不一定是素数. http://blog.csdn.net/tsaid/article/details/7354716 这篇题解写得最好. 那啥,这题 ...
- 【EX_BSGS】BZOJ1467 Pku3243 clever Y
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 425 Solved: 238[Submit][Status ...
- 【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 ...
- bzoj 1467: Pku3243 clever Y 扩展BSGS
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...
- [拓展Bsgs] Clever - Y
题目链接 Clever - Y 题意 有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\). 解法 如题,是拓展 \(Bsgs\) 板 ...
- bzoj1467 Pku3243 clever Y
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 313 Solved: 181[Submit][Status ...
- 【POJ3243】【拓展BSGS】Clever Y
Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...
随机推荐
- Mysql忘记密码处理办法
找回密码的步骤如下: 1.停止mysql服务器 sudo /opt/lampp/lampp stopmysql 2.使用`--skip-grant-tables' 参数来启动 mysqld sudo ...
- angualarjs $location服务
$location服务 angular使用内置的$location服务来监听.操作url,包括以下功能: - 获取.监听.改变地址栏的URL: - 与URL实现双向数据绑定(地址栏变动.前进后退或者点 ...
- MySQL☞substr函数
substr函数:截取字符串 格式如下: select substr(参数1,参数2,参数3) from 表名 参数1:列名/字符串 参数2:起始位置,如果为正数,就表示从正数的位置往下截取字符 ...
- lintcode373 奇偶分割数组
奇偶分割数组 分割一个整数数组,使得奇数在前偶数在后. 您在真实的面试中是否遇到过这个题? Yes 样例 给定 [1, 2, 3, 4],返回 [1, 3, 2, 4]. 我的方法:设定两个数组,分别 ...
- 157. Unique Characters 【LintCode by java】
Description Implement an algorithm to determine if a string has all unique characters. Example Given ...
- 【CQOI 2007】 余数求和
题目描述 给出正整数n和k,计算G(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值,其中k mod i表示k除以i的余数.例如G(10, 5)=5 ...
- LeetCode 144 ——二叉树的前序遍历
1. 题目 2. 解答 2.1. 递归法 定义一个存放树中数据的向量 data,从根节点开始,如果节点不为空,那么 将当前节点的数值加入到 data 中 递归得到其左子树的数据向量 temp,将 te ...
- appcan打包后产生的问题总结
以appcan为基础的项目,最终需要打包后进行调试.在调试过程中,主要的样式问题在苹果手机上,下面将这些问题总结起来,以防下次再犯. 1:ios 7 以上的手机中,状态栏与内容重叠: 问题描述:在io ...
- 将footer固定在页面最下方
方法一: HTML结构: <div id="id_wrapper"> <div id="id_header"> Header Block ...
- vue.js 创建组件 子父通信 父子通信 非父子通信
1.创建组件 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...