【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,计算 ...
随机推荐
- 【简单dp+模拟】hdu-5375(2015多校#7-1007)
给你一个二进制数,,每一位有一个权值,让你转格雷码,求所对应格雷码位为1的权值的和:二进制位中的某些位为?,你需要给这些问号赋值使得到的和最大. 首先你得知道二进制转格雷码的规则,即格雷码位为[二进制 ...
- CSS—换行
关于文本溢出换行问题,先看下涉及到换行的相关属性,查看:http://www.w3school.com.cn 一.word-break 定义:word-break 属性规定自动换行的处理方法. 值 描 ...
- Ajax--跨域访问的三种方法
一.什么是跨域 我们先回顾一下域名地址的组成: / script/jquery.js http:// (协议号) www (子域名) google (主域名) (端口号) script/jquer ...
- oracle中比较两表表结构差异和数据差异的方法
在工作中需要完成这么一个需求:比较两个表的表结构是否形相同,并找出差异.比较两个表中的数据是否相同,并找出差异数据? 分析:由于表结构中字段比较多,手工比较很浪费时间,而且不能保证不出错误.对于 ...
- ios获取当前语言
上代码: + (NSString*)getPreferredLanguage { NSUserDefaults * defaults = [NSUserDefaults standardUserDef ...
- SQLserver的存储过程
存储过程 [Create是创建存储过程,alter是更改.改变存储过程] [在第一次写存储过程时用create,若修改存储过程程序之后,则alter替换create再执行] [在数据库中begin ...
- [easyui] datebox源码阅读. 批注
jquery.datebox.js 文件. (function($){ /** * create date box */ function createBox(target){ var state = ...
- UvaLive7362 Fare(欧拉函数)
题意:求1~n的素因子之和. 分析:欧拉函数 #include<cstdio> #include<cstring> #include<cctype> #includ ...
- lex&yacc8--wehter use in C++
bintree.h:12:1: error: unknown type name ‘using’ using namespace std; ============== bintree.h:28:1: ...
- MFC中快速应用OpenCV(转)
转载链接:http://wiki.opencv.org.cn/index.php/MFC%E4%B8%AD%E5%BF%AB%E9%80%9F%E5%BA%94%E7%94%A8OpenCV 简介和缘 ...