bzoj 2242 [SDOI2011]计算器(数论知识)
Description
Input
输入包含多组数据。
Output
Sample Input
3 1
2 1 3
2 2 3
2 3 3
【样例输入2】
3 2
2 1 3
2 2 3
2 3 3
【数据规模和约定】
对于100%的数据,1<=y,z,p<=10^9,为质数,1<=T<=10。
Sample Output
2
1
2
【样例输出2】
2
1
0
【思路】
快速幂,拓展欧几里得,BSGS
第一问快速幂求得。
第二问求axΞ b(mod n),转化为ax=ny+b,转化为ax+ny=b,利用拓展欧几里得算法求出ax+ny=gcd(a,n),如果b不是gcd的倍数则无解否则为x/gcd*b。
第三问求ax Ξb(mod n),BSGS算法。我们需要验证0..n-1内的数。分块,设每块大小为m,求出0..m-1内的ai % n保存为ei,对于m..2m-1内的数,我们只需要验证是否存在有am *ei=b(mod n),即判断是否存在ei=a-m *b (mod n),这样用一个hash表存一下ei然后求一下在模n下am的逆元就可以了。
时间复杂度为O((m+n/m)logm),当m取n½的时候复杂度较优为O(n½logn)
【代码】
#include<map>
#include<cmath>
#include<cstdio>
using namespace std; typedef long long LL;
LL a,b,c,T,k; LL pow(LL x,LL p,LL MOD) {
LL tmp=x,ans=;
while(p) {
if(p&) ans=(ans*tmp)%MOD;
tmp=(tmp*tmp)%MOD;
p>>=;
}
return ans;
}
void gcd(LL a,LL b,LL& d,LL& x,LL& y) {
if(!b) d=a,x=,y=;
else gcd(b,a%b,d,y,x),y-=x*(a/b);
}
LL inv(LL a,LL n) {
LL d,x,y;
gcd(a,n,d,x,y);
return d==? (x+n)%n:-;
}
LL log_mod(LL a,LL b,LL n) {
LL m,v,e=,i;
m=sqrt(n+0.5);
v=inv(pow(a,m,n),n);
map<LL,LL> mp;
mp[]=;
for(LL i=;i<m;i++) {
e=(e*a)%n;
if(!mp.count(e)) mp[e]=i;
}
for(LL i=;i<m;i++) {
if(mp.count(b)) return i*m+mp[b];
b=(b*v)%n;
}
return -;
} int main() {
scanf("%lld%lld",&T,&k);
while(T--) {
scanf("%lld%lld%lld",&a,&b,&c);
if(k==) {
printf("%lld\n",pow(a,b,c));
} else
if(k==) {
LL d,x,y;
gcd(a,c,d,x,y);
if(b%d) puts("Orz, I cannot find x!");
else {
LL ans=((x*b/d)%c+c)%c;
printf("%lld\n",ans);
}
} else {
LL ans=log_mod(a,b,c);
if(ans==-) puts("Orz, I cannot find x!");
else printf("%lld\n",ans);
}
}
return ;
}
bzoj 2242 [SDOI2011]计算器(数论知识)的更多相关文章
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
- BZOJ 2242: [SDOI2011]计算器( 快速幂 + 扩展欧几里德 + BSGS )
没什么好说的... --------------------------------------------------------------------- #include<cstdio&g ...
- BZOJ 2242: [SDOI2011]计算器 [快速幂 BSGS]
2242: [SDOI2011]计算器 题意:求\(a^b \mod p,\ ax \equiv b \mod p,\ a^x \equiv b \mod p\),p是质数 这种裸题我竟然WA了好多次 ...
- [原博客] BZOJ 2242 [SDOI2011] 计算器
题目链接 noip级数论模版题了吧.让求三个东西: 给定y,z,p,计算`Y^Z Mod P` 的值. 给定y,z,p,计算满足`xy≡ Z ( mod P )`的最小非负整数. 给定y,z,p,计算 ...
- BZOJ.2242.[SDOI2011]计算器(扩展欧几里得 BSGS)
同余方程都不会写了..还一直爆int /* 2.关于同余方程ax ≡b(mod p),可以用Exgcd做,但注意到p为质数,y一定有逆元 首先a%p=0时 仅当b=0时有解:然后有x ≡b*a^-1( ...
- BZOJ 2242 [SDOI2011]计算器(快速幂+Exgcd+BSGS)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2242 [题目大意] 给出T和K 对于K=1,计算 Y^Z Mod P 的值 对于K=2 ...
- bzoj 2242 [SDOI2011]计算器——BSGS模板
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2242 第一道BSGS! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...
- BZOJ 2242 [SDOI2011]计算器 BSGS+高速幂+EXGCD
题意:id=2242">链接 方法: BSGS+高速幂+EXGCD 解析: BSGS- 题解同上.. 代码: #include <cmath> #include <c ...
- bzoj 2242: [SDOI2011]计算器
#include<cstdio> #include<iostream> #include<map> #include<cmath> #define ll ...
随机推荐
- MySQL的EXPLAIN命令详解(转)
explain显示了mysql如何使用索引来处理select语句以及连接表.可以帮助选择更好的索引和写出更优化的查询语句. 使用方法,在select语句前加上explain就可以了: 如: expla ...
- Delphi新语法和ifthen的扩展联想
Delphi之前已经重载了好多个ifthen函数 Math单元 ): Integer; overload; inline; ): Int64; overload; inline; ): UInt64; ...
- boost linux 下安装
1. 在boost 官网 http://www.boost.org/doc/libs/ 下载最新的boost 安装包 2. 解压至 /usr/local/ 目录下 3. cd /usr/local/b ...
- Timer组件
1.常用属性 Interval 用于获取或设置Timer组件Tick事件发生的时间间隔,属性值不能小于1 制作左右飘摇窗体 private void timer1_Tick(object sender ...
- windows azure programing
http://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-get-started-vs2012/ http:// ...
- js Touch事件(向左滑动,后退)
js Touch事件(向左滑动,后退) 代码如下 var touch_p = { c_x : 0, c_y : 0, hasbacked : false }; function touches(ev) ...
- 敏捷开发的特点(转自MBAlib)
敏捷开发的特点 敏捷方法主要有两个特点,这也是其区别于其他方法,尤其是重型方法的最主要特征: (1)敏捷开发方法是“适应性”(Adaptive)而非“预设性” (Predictive). 这里说的预设 ...
- ios音频视频资料--备用
视频播放 MediaPlayer.framework MPMoviePlayerViewController VS MPMoviePlayerController MPMoviePlayerViewC ...
- poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)
http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS Memory Limit: 65536K Total Submiss ...
- How to Run Node.js with Express on Mobile Devices
We released a JXcore plugin for Apache Cordova recently and in this article I will show how to run a ...