BZOJ 2242 计算器
Description
你被要求设计一个计算器完成以下三项任务:
\(1.\)给定\(y,z,p\),计算\(y^{z}\;mod\;P\)的值;
\(2.\)给定\(y,z,p\),计算满足\(xy \equiv z\;mod\;P\)的最小非负整数;
\(3.\)给定\(y,z,p\),计算满足\(y^{x} \equiv z\;mod\;P\)的最小非负整数。
Input
输入包含多组数据。
第一行包含两个正整数\(T,K\)分别表示数据组数和询问类型(对于一个测试点内的所有数据,询问类型相同)。
以下行每行包含三个正整数\(y,z,p\),描述一个询问。
Output
对于每个询问,输出一行答案。对于询问类型\(2\)和\(3\),如果不存在满足条件的,则输出“Orz, I cannot find x!”,注意逗号与“I”之间有一个空格。
Sample Input
3 1
2 1 3
2 2 3
2 3 3
Sample Output
2
1
2
Hint
对于\(100\%\)的数据,\(1 \le y,z,P \le 10^{9}\),\(P\)为质数,\(1 \le T \le 10\)。
数论裸题合集:快速幂,扩展欧几里得,bsgs。
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<map>
using namespace std;
typedef long long ll;
int kind; ll ans;
inline ll qsm(ll a,ll b,ll c)
{
ll ret = 1;
for (;b;b >>= 1,(a *= a) %= c)
if (b & 1) (ret *= a) %= c;
return ret;
}
inline int gcd(int a,int b)
{
if (!b) return a;
return gcd(b,a%b);
}
inline ll bsgs(int a,int b,int c)
{
int i,t = 1;
for (i = 0;i<=50;i++)
{
if (t == b) return i;
t = (ll)t*(ll)a% c;
}
int tmp = 1,k = 1;
t = 1;
while (tmp = gcd(a,c),tmp!=1)
{
if (b % tmp) return -1;
c /= tmp; k++; b /= tmp;
t = (ll)t*(ll)a/tmp%c;
}
int m = (int)sqrt(c+0.5);
map <int,int> hash; hash[1] = 0;
int f = 1;
for (i = 1;i<m;i++)
{
f = (ll)f * (ll)a % c;
hash[f] = i;
}
f = (ll)f*(ll)a%c;
b = qsm(t,c-2,c)*(ll)b%c;
int mod = qsm(f,c-2,c);
for (i = 0;i<m;i++)
{
if (hash.count(b)) return i*m+hash[b]+k-1;
b = (ll)b * (ll)mod % c;
}
return -1;
}
int main()
{
freopen("2242.in","r",stdin);
freopen("2242.out","w",stdout);
int T; scanf("%d %d",&T,&kind);
while (T--)
{
int y,z,p;
scanf("%d %d %d",&y,&z,&p);
if (kind == 1) ans = qsm(y,z,p);
else if (kind == 2)
{
y %= p,z %= p;
int d = gcd(y,p);
if (z % d != 0) ans = -1;
else
{
y /= d, p /= d,z /= d;
ans = z*qsm(y,p-2,p)%p;
}
}
else ans = bsgs(y,z,p);
if (ans >= 0) printf("%lld\n",ans);
else printf("Orz, I cannot find x!\n");
}
fclose(stdin); fclose(stdout);
return 0;
}
BZOJ 2242 计算器的更多相关文章
- 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]计算器(数论知识)
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! 咳咳,我到底改了些什么?…… 感觉和自己的第一版写的差不多……可能是 ...
随机推荐
- GlusterFS源代码解析 —— GlusterFS 日志
Logging.c: /* Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com> This file is part ...
- activity-intent-startActivity-startActivityResult
一. Intent和startActivity 1.在一个Activtiy中可以使用startActivity()去开始另一个制定的Activity 但在android3.0(是针对平板出的产品)之后 ...
- cocos2d-x plist+wen使用
http://zhan.renren.com/tag?value=cocos2dx&from=template http://blog.csdn.net/zhanglongit/article ...
- IIS Shared Configuration
Introduction The Internet changes the ways in which companies handle their day-to-day business and h ...
- SpringMVC注解@RequestMapping
/** * GET 查询 * * @return 视图路径 */ @RequestMapping(value = {"/index&q ...
- 【转】Angular Input格式化
今天在Angular中文群有位同学问到:如何实现对input box的格式化.如下的方式对吗? <input type="text" ng-model="demo. ...
- java.io.EOFException错误
TOmcat启动后报:IOException while loading persisted sessions: Java.io.EOFException错误 - IOException while ...
- JVM垃圾回收理论知识
- SqlServer 三级联动、递归表
SqlServer 省市县三级联动 三张表递归合并成一张表sql如下: insert into table2(area_name,area_parent_id) select province,'0' ...
- 《你不常用的c#之四》:Array的小抽屉ArraySegment
转载自csdn:http://blog.csdn.net/robingaoxb/article/details/6200060 一:)略谈 ArraySegment顾名思义就是Array区块 ...