【BZOJ】【2242】【SDOI2011】计算器
快速幂/扩展欧几里得/BSGS
经典好例题!!
三个问题三种算法……
算法:白书(算法竞赛入门经典——训练指南)全有……
/**************************************************************
Problem: 2242
User: Tunix
Language: C++
Result: Accepted
Time:1824 ms
Memory:2476 kb
****************************************************************/ //BZOJ 2242
#include<map>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std; int getint(){
int v=,sign=; char ch=getchar();
while(ch<''||ch>'') {if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<='') {v=v*+ch-''; ch=getchar();}
return v*=sign;
}
/*******************tamplate********************/
typedef long long LL;
LL x,y,z,P;
LL pow(LL a,LL b,LL P){
LL r=,base=a;
while(b){
if (b&) r=r*base%P;
base=base*base%P;
b>>=;
}
return r;
}
void exgcd(LL a,LL b,LL &d,LL &x,LL &y){
if (!b) {d=a; x=; y=; return;}
else{exgcd(b,a%b,d,y,x); y-=x*(a/b);}
}
LL log_mod(LL a,LL b,LL P){
LL m,v,e=,i;
m=ceil(sqrt(P+0.5));//这里需要用ceil……?
v=pow(a,P-m-,P);//inv
map<LL,LL> x;
x[]=;
for(int i=;i<m;++i){
e=e*a%P;
if (!x.count(e)) x[e]=i;
}
rep(i,m){
if (x.count(b)) return (LL)i*m+x[b];
b=b*v%P;
}
return -;
}
int main(){
int T=getint(),K=getint();
while(T--){
y=getint(); z=getint(); P=getint();
if (K==) printf("%lld\n",pow(y,z,P));
if (K==){
LL d=,X=,Y=;
exgcd(y,P,d,X,Y);
if(z%d){ printf("Orz, I cannot find x!\n"); continue; }
X*=z/d;
LL T=X*d/P;
X-=T*P/d;
if (X<) X+=P/d;
printf("%lld\n",X);
}
if (K==){
y%=P; z%=P;
if(!y && !z) {printf("1\n"); continue;}
if(!y) { printf("Orz, I cannot find x!\n"); continue; }
LL ans=log_mod(y,z,P);
if (ans==-){ printf("Orz, I cannot find x!\n"); continue; }
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]计算器(数论知识)
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,计算 ...
随机推荐
- pandas使用总结
一.pandas简介 Pandas是基于Numpy开发出的,是一款开放源码的BSD许可的Python库,为Python编程语言提供了高性能,易于使用的数据结构和数据分析工具.Pandas用于广泛的领域 ...
- ACM 水果 hdu 1263 一题多解
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1263 文章末有相应的一些测试数据供参考. 传统的数组解题方式 思路一: 三种属性的数据放在一个结构体里面, ...
- [转载] 你所不知道的TIME_WAIT和CLOSE_WAIT
前言 本文转载自 https://mp.weixin.qq.com/s/FrEfx_Yvv0fkLG97dMSTqw.很久前看到Vincent Bernat在博客中写了一遍关于TCP time-wai ...
- [leetcode sort]75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- Xamarin无法调试Android项目
Xamarin无法调试Android项目 项目可以正常编译,生成APK,也可以通过右键菜单部署.但是一旦开启调试,就报错.错误信息如下: 没有为此解决方案配置选中要生成的项目 出现这种问题是因 ...
- 异常:The server committed a protocol violation
异常记录: Exception rethrown at [0]: 在 Wintop.Windows.FrmLogin.btnLogin_Click(Object sender, EventArgs e ...
- windows下安装awstats来分析apache的访问日志
一.啰嗦两句 之前在Windows下用Apache时,也曾经配置过Awstats,然后换了工作,改用Linux+nginx,渐渐把Apache忘记了.又换了工作,又得用Apache,这回版本更新到2. ...
- 20172333 2017-2018-2 《Java程序设计》第6周学习总结
20172333 2017-2018-2 <Java程序设计>第6周学习总结 教材学习内容 1.数组的基本用法,如数组的定义:int[该数组类型] name = new int[X]X为数 ...
- HDU 5682 zxa and leaf 二分 树形dp
zxa and leaf 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5682 Description zxa have an unrooted t ...
- Codeforces Round #131 (Div. 1) A - Game
A. Game time limit per test 1 second memory limit per test 256 megabytes input standard input output ...