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 ...
随机推荐
- How to open .ccproj in VS2010?
Q: How to open .ccproj projects types in VS2010, ccproj file type is a Cloud project i suppose. Plea ...
- C++ map映射的使用方法
今天考试做了道题,用上了map,这是一道提高组联赛难度的题目,先发题目: ****************************** 1. A-B problem( dec.c/cpp/pas) . ...
- 【java】Servlet 工程 web.xml 中的 servlet 和 servlet-mapping 标签
摘录某个工程的 web.xml 文件片段: 访问顺序为1—>2—>3—>4,其中2和3的值必须相同. url-pattern 标签中的值是要在浏览器地址栏中输入的 url,可以自己命 ...
- appfabric 简单应用
http://msdn.microsoft.com/en-us/library/ee790941(v=azure.10).aspx Preparing the Visual Studio Projec ...
- Grails的redirect无法跳转时的一个可能原因
由于controller的命名一般首字母大写,如Login 此时如 class LoginController { def index = { redirect(action:Login, param ...
- [转载]初学C#之list
C# List<T>用法 所属命名空间:System.Collections.Generic public class List<T> : IList<T>, IC ...
- 浅谈 iOS 之 Crash log 符号化
其实,对于做移动 APP 开发的同学来说,质量和体验都是同等重要的.一个 APP 应用如果经常「闪退」,是产品质量很差的一个体现,那么用户体验就更不用再提了. *** 上面是笔者截取的国外一家公司对用 ...
- Mybatis bug修正
http://1358440610-qq-com.iteye.com/blog/1827391
- UVA 10608 Friends
题目大意:共有n个人,m对人为已知的朋友关系,而且这种关系具有传递性,也就是A与B,B与C是朋友,可以确定A与C是朋友,求一个人数最多的朋友团体. bfs就可以了,遇到未访问的结点,加入队列并且朋人数 ...
- spring-boot启动debug信息中non-fatal error解决
java.lang.ClassNotFoundException: org.springframework.data.web.config.EnableSpringDataWebSupport添加依赖 ...