poj3243 Clever Y[扩展BSGS]
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 8666 | Accepted: 2155 |
Description
Little Y finds there is a very interesting formula in mathematics:
XY mod Z = K
Given X, Y, Z, we all know how to figure out K fast. However, given X, Z, K, could you figure out Y fast?
Input
Input file ends with 3 zeros separated by spaces.
Output
Sample Input
5 58 33
2 4 3
0 0 0
Sample Output
9
No Solution
Source
//消除因子,在做普通的BSGS
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
struct Thash{
static const ll N=1e6+,MOD=;
ll tot,val[N],h[N],next[N],head[MOD+];
void clear(){tot=;memset(head,,sizeof head);}
void insert(ll H,ll VAL){
for(ll i=head[H%MOD];i;i=next[i]) if(h[i]==H){val[i]=VAL;return ;}
h[++tot]=H;val[tot]=VAL;next[tot]=head[H%MOD];head[H%MOD]=tot;
}
ll get(ll H){
for(ll i=head[H%MOD];i;i=next[i]) if(h[i]==H) return val[i];
return -;
}
}M;
ll gcd(ll a,ll b){
if(!b) return a;
return gcd(b,a%b);
}
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
if(!b){d=a;x=;y=;return ;}
exgcd(b,a%b,d,y,x);
y-=a/b*x;
}
ll inv(ll a,ll p){
ll d,x,y;
exgcd(a,p,d,x,y);
return d==?(x%p+p)%p:-;
}
ll BSGS(ll a,ll b,ll mod){
M.clear();
ll m=(ll)ceil(sqrt(mod+0.5));
ll t=;
for(ll i=;i<m;i++){
M.insert(t,i);
t=t*a%mod;
}
ll base=inv(t,mod);
ll res=b;
for(ll i=,z;i<m;i++){
if((z=M.get(res))!=-) return i*m+z;
res=res*base%mod;
}
return -;
}
ll solve(ll a,ll b,ll mod){
ll A=,D=,cnt=,ans;
for(int i=;i<=;i++){
if(A==b) return i;
A=A*a%mod;
}
for(ll t;(t=gcd(a,mod))!=;){
if(b%t)return -;
b/=t;
mod/=t;
D*=a/t;D%=mod;
cnt++;
}
b=b*inv(D,mod)%mod;
ans=BSGS(a,b,mod);
if(~ans) return ans+cnt;
return -;
}
int main(){
ll a,b,c,ans;
while(~scanf("%lld%lld%lld",&c,&a,&b)){
ans=solve(a,b%c,c);
if(~ans) printf("%lld\n",ans);
else puts("no solution");
}
return ;
}
poj3243 Clever Y[扩展BSGS]的更多相关文章
- bzoj 1467: Pku3243 clever Y 扩展BSGS
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...
- POJ 3243 Clever Y 扩展BSGS
http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details ...
- luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法
BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...
- 【POJ 3243】Clever Y 拓展BSGS
调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那 ...
- 【数论】【ex-BSGS】poj3243 Clever Y
用于求解高次同余方程A^x≡B(mod C),其中C不一定是素数. http://blog.csdn.net/tsaid/article/details/7354716 这篇题解写得最好. 那啥,这题 ...
- poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】
扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...
- [拓展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 ...
- [POJ 3243]Clever Y
Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...
随机推荐
- 利用nginx做tcp负载均衡
当前nginx-13.1已经支持tcp,ucp,unix域套接字三种负载均衡模式(http肯定支持,这个不用说).最近有需求需要对后端服务做负载均衡,因此考虑使用nginx来做. 1. 下载nginx ...
- MBProgressHUD 设置透明背景
MBProgressHUD 默认使用MBProgressHUDBackgroundStyleBlur 类型 MBProgressHUDBackgroundStyleBlur使用了毛玻璃效果 ,我们要把 ...
- Excel 时间格式相减
https://jingyan.baidu.com/article/3065b3b6e8b9dabecff8a4d6.html datedif函数是excel的隐藏函数,主要用于计算日期之差,不是四舍 ...
- openfireserver和jdk环境删除命令
一.卸载jdk1.8 终端依次运行以下的命令 sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin sudo rm -rf / ...
- window.print
function contentPrint(contentArea) { var initBody = document.body.innerHTML; if ($("div"). ...
- Java并发编程(九):拓展
java多线程死锁理解 Java多线程并发最佳实践 Spring与线程安全 HashMap与ConcurrentHashMap 关于java集合类HashMap的理解 , 数据结构之 ...
- 在linux下运行jmeter
之前在用JMeter做性能测试时基本都用自己的笔记本电脑. 考虑到网络传输和占用系统资源情况时进行了以下分析: 1 在笔记本上,开始-cmd "ping 服务器",得到传输时间.在 ...
- ToStringBuilder学习(二):两种方法用法优缺点及一个问题
研究ApacheCommon源码, 先从一个最简单的开始,即围绕Object类里的toString方法自动化实现的一系列类. 怎么来自动化地实现toString方法, 有两种:反射和手 ...
- poj 3017 Cut the Sequence(单调队列优化 )
题目链接:http://poj.org/problem?id=3017 题意:给你一个长度为n的数列,要求把这个数列划分为任意块,每块的元素和小于m,使得所有块的最大值的和最小 分析:这题很快就能想到 ...
- KVC之-(id)valueForKey:(NSString *)key的实现原理与验证
KVC之-(id)valueForKey:(NSString *)key的实现原理与验证 2.-(id)valueForKey:(NSString *)key的实现原理与验证; #功能:使用一个字符串 ...