计算器(bzoj 2242)
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
/*
Type1:快速幂
Type2:扩展欧几里得
Type3:BSGS
*/
#include<cstdio>
#include<iostream>
#include<map>
#include<cmath>
#define lon long long
using namespace std;
int Q,op;lon A,B,C;
map<lon,int> hash;
lon poww(lon a,lon b){
lon base=a,r=;
while(b){
if(b&) r*=base;r%=C;
base*=base;base%=C;
b>>=;
}
return r;
}
lon exgcd(lon a,lon b,lon &x,lon &y){
if(!b) {
x=;y=;return a;
}
lon r=exgcd(b,a%b,x,y);
lon t=x;x=y;y=t-a/b*y;
return r;
}
void work3(){
hash.clear();
if(A%C==){
printf("Orz, I cannot find x!\n");
return;
}
lon m=ceil(sqrt(C)),ans;
for(int i=;i<=m;i++){
if(!i){
ans=B%C;hash[ans]=i;
continue;
}
ans=(ans*A)%C;hash[ans]=i;
}
ans=;lon t=poww(A,m);bool fl=false;
for(int i=;i<=m;i++){
ans=(ans*t)%C;
if(hash[ans]){
ans=i*m-hash[ans];
ans=(ans+C+C)%C;
printf("%lld\n",ans);
fl=true;
break;
}
}
if(!fl) printf("Orz, I cannot find x!\n");
}
void work2(){
lon x,y,vgcd;
vgcd=exgcd(A,C,x,y);
if(B%vgcd) {
printf("Orz, I cannot find x!\n");
return;
}
x=x*B/vgcd;
lon mod=C/vgcd;
x=((x%mod)+mod)%mod;
printf("%lld\n",x);
}
void work1(){
printf("%lld\n",poww(A,B));
}
int main(){
scanf("%d%d",&Q,&op);
while(Q--){
scanf("%lld%lld%lld",&A,&B,&C);
if(op==) work1();
if(op==) work2();
if(op==) work3();
}
return ;
}
计算器(bzoj 2242)的更多相关文章
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
- BZOJ 2242 计算器
Description 你被要求设计一个计算器完成以下三项任务: \(1.\)给定\(y,z,p\),计算\(y^{z}\;mod\;P\)的值: \(2.\)给定\(y,z,p\),计算满足\(xy ...
- 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]计算器(数论知识)
Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数: 3.给 ...
- [BZOJ 2242] [SDOI 2011] 计算器
Description 你被要求设计一个计算器完成以下三项任务: 给定 \(y,z,p\),计算 \(y^z \bmod p\) 的值: 给定 \(y,z,p\),计算满足 \(xy≡ z \pmod ...
- 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! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...
随机推荐
- 微信iOS多设备多字体适配方案总结
一.背景 2014下半年,微信iOS版先后适配iPad, iPhone6/6plus.随着这些大屏设备的登场,部分用户觉得微信的字体太小,但也有很多用户不喜欢太大的字体.为了满足不同用户的需求,我们做 ...
- Codeforces 517 #B
http://codeforces.com/contest/1072/problem/B 开始想的只有搜索,时间复杂度$O(4^n)$,明显有问题. 想了半个小时没有思路,然后想到了正难则反,就开始步 ...
- UVa 12171 题解
英文题面不怎么友好,大家还是自行通过紫书了解题面吧... 解题思路: 1. 面对500 ^ 3的数据范围,我们需要先用离散化解决掉爆空间的问题. 2. 由于我们要求的总体积包括内空部分的体积,我们可以 ...
- spartan6不能直接把时钟连到IO上
1.问题的提出:spartan6中不允许时钟信号直接连到IO口上面? 2.解决办法: ODDR2的使用 ODDR2Primitive: Double Data Rate Output D Flip-F ...
- 安装pymysql后,import pymysql,pycharm编辑器中报错
cmd 中运行 pip3 install PyMySQL 或者采用git git clone https://github.com/PyMySQL/PyMySQL cd PyMySQL/ python ...
- LeetCode(215) Kth Largest Element in an Array
题目 Find the kth largest element in an unsorted array. Note that it is the kth largest element in the ...
- German Collegiate Programming Contest 2015
// Legacy Code #include <iostream> #include <cstdio> #include <cstring> #include & ...
- 数据挖掘算法——Apriori
在上一篇数据挖掘入门算法整理中提到,Apriori算法是关联规则算法中使用最为广泛的算法,这次我们就来学习下该算法的基本知识. 一.算法概述 Apriori 算法是一种最有影响力的挖掘布尔关联规则的频 ...
- SMP IRQ Affinity
转:非常有用的方法,调式神器 SMP IRQ Affinity Background: Whenever a piece of hardware, such as disk controller or ...
- No identifier specified for entity: XXXX 错误
在运行项目的时候报了下面的错误: by: org.hibernate.AnnotationException: No identifier specified for entity: com.exam ...