【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

#include<cstdio>
#define ll long long
#include<map>
#include<cmath>
using namespace std;
int T,k;
ll pow(ll x,int y,int p){
ll ans=;
while(y>){
if (y&==) ans=(ans*x)%p;
y=y>>;
x=(x*x)%p;
}
return ans;
} int gcd(int x,int y){
if (x%y==) return y;
return gcd(y,x%y);
} void exgcd(int a,int b,int &x,int &y){
if (b==){x=,y=;return;}
exgcd(b,a%b,x,y);
int t=x;x=y;y=t-(a/b)*y;
} void solve2(int a,int z,int b){
int tmp=gcd(a,b),x,y;
if (z%tmp){printf("Orz, I cannot find x!\n");return;}
exgcd(a,b,x,y);
x=((ll)x*(z/tmp))%b;
while (x>) x-=b/tmp;
while (x<) x+=b/tmp;
printf("%d\n",x);
} map<int,int> mp;
void solve3(int y,int z,int p){
y%=p;
if (!y&&!z) {printf("1\n");return;}
if (!y){printf("Orz, I cannot find x!\n");return;}
mp.clear();
ll m=ceil(sqrt(p)),t=;
mp[]=m+;//y^0==1;
for (int i=;i<m;i++){
t=t*y%p;
if (!mp[t]) mp[t]=i;
}
ll tmp=pow(y,p--m,p),ine=;
for (int k=;k<m;k++){
int i=mp[z*ine%p];
if (i){
if (i==m+)i=;
printf("%d\n",k*m+i);
return;
}
ine=ine*tmp%p;
}
printf("Orz, I cannot find x!\n");
} int main(){
scanf("%d%d",&T,&k);
while (T--){
int y,z,p;
scanf("%d%d%d",&y,&z,&p);
if (k==) printf("%lld\n",pow(y,z,p));
if (k==) solve2(y,z,p);
if (k==) solve3(y,z,p);
}
}
【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]计算器(数论知识)
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- 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 ...
- [原博客] BZOJ 2242 [SDOI2011] 计算器
题目链接 noip级数论模版题了吧.让求三个东西: 给定y,z,p,计算`Y^Z Mod P` 的值. 给定y,z,p,计算满足`xy≡ Z ( mod P )`的最小非负整数. 给定y,z,p,计算 ...
随机推荐
- Java计算文件的SHA码和MD5码
可参考:http://blog.csdn.net/hudashi/article/details/8394158 /** * 计算文件的MD5码 * @param file * @return */ ...
- iOS开发者如何提高自己的水平(转)
阅读. 把一大堆的知识塞进脑子里.随着时间流逝,终归有一些会留在脑海里.我觉得有些东西读起来还挺有意思,那么也能算作一种愉快的消遣. 分析. 多去熟悉并了解一些工具,从高层的到底层的,不要害怕去使用他 ...
- Ehcache(2.9.x) - API Developer Guide, Cache Loaders
About Cache Loaders A CacheLoader is an interface that specifies load() and loadAll() methods with a ...
- jquery之音乐均衡器
制作这个音乐均衡器需要一个equalizer插件(插件我已经上传),下面介绍一下网页的BGM的相关属性: hidden="true"表示隐藏播放,即不显示播放器的外观,若要想显示, ...
- 使用Win7+IIS7发布网站或服务步骤
1.安装IIS服务:控制面板=>程序=>打开或关闭WINDOWS 功能=>Internet 信息服务=>WEB服务管理器全选√ 和万维网服务:应用程序开发功能: 2.打开IIS ...
- (转)实战Memcached缓存系统(6)Memcached CAS的多线程程序实例
1. 源程序 package com.sinosuperman.memcached; import java.io.IOException; import java.net.InetSocketAdd ...
- echarts标准饼图解读(一)——提示框(tooltip)配置
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- css3学习笔记之边框
CSS3 圆角 border-radius 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <!DOCTYPE html> <h ...
- javascript笔记——jikeytang javascript前端群 389875212 精华总结
网址: https://github.com/jsfront // http://www.kancloud.cn/jsfront/month/82796 内容: 前端js github总结, ...
- poj 3616 Milking Time
Milking ...