[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
【样例输出1】
2
1
2
【样例输出2】
2
1
0
1.快速幂
2.拓展欧几里德解线性方程
3.BSGS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long lol;
int MOD=;
lol hash[],id[];
void insert(lol x,lol d)
{
lol pos=x%MOD;
while ()
{
if (hash[pos]==-||hash[pos]==x)
{
hash[pos]=x;
id[pos]=d;
return;
}
pos++;
if (pos>=MOD) pos-=MOD;
}
}
bool count(lol x)
{
lol pos=x%MOD;
while ()
{
if (hash[pos]==-) return ;
if (hash[pos]==x) return ;
pos++;
if (pos>=MOD) pos-=MOD;
}
}
lol query(lol x)
{
lol pos=x%MOD;
while ()
{
if (hash[pos]==x) return id[pos];
pos++;
if (pos>=MOD) pos-=MOD;
}
}
lol qpow(lol x,lol y,lol Mod)
{
lol res=;
while (y)
{
if (y&) res=res*x%Mod;
x=x*x%Mod;
y>>=;
}
return res;
}
lol exgcd(lol a,lol b,lol &x,lol &y)
{
if (!b)
{
x=;y=;
return a;
}
lol d=exgcd(b,a%b,x,y);
lol t=x;x=y;y=t-a/b*y;
return d;
}
lol BSGS(lol a,lol b,lol Mod)
{lol i;
if (b==) return ;
if (a==&&b!=) return -;
memset(hash,-,sizeof(hash));
memset(id,,sizeof(id));
lol tim=sqrt((double)Mod);
lol tmp=b%Mod;
for (i=;i<=tim;i++)
{
insert(tmp,i);
tmp=tmp*a%Mod;
}
lol t=tmp=qpow(a,tim,Mod);
for (i=;i<=tim;i++)
{
if (count(tmp))
return i*tim-query(tmp);
tmp=tmp*t%Mod;
}
return -;
}
int main()
{int T,k,i;
lol x,y,p,ans;
while (cin>>T>>k)
{
for (i=;i<=T;i++)
{
scanf("%lld%lld%lld",&x,&y,&p);
if (k==)
{
printf("%lld\n",qpow(x,y,p));
}
else if (k==)
{
lol a,b;
lol d=exgcd(x,p,a,b);
if (y%d) printf("Orz, I cannot find x!\n");
else
{
lol t=y/d;
a=a*t;
d=p/d;
printf("%lld\n",(a%d+d)%d);
}
}
else if (k==)
{
ans=BSGS(x%p,y%p,p);
if (ans==-) printf("Orz, I cannot find x!\n");
else printf("%lld\n",ans);
}
}
}
}
[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]计算器_快速幂+扩展GCD+BSGS
BZOJ_2242_[SDOI2011]计算器_快速幂+扩展GCD+BSGS 题意: 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p, ...
- 【bzoj2242】[SDOI2011]计算器
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 3207 Solved: 1258[Submit][Statu ...
- BZOJ2242 [SDOI2011]计算器 【BSGS】
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 4741 Solved: 1796 [Submit][Sta ...
- 【BZOJ2242】[SDOI2011]计算器 BSGS
[BZOJ2242][SDOI2011]计算器 Description 你被要求设计一个计算器完成以下三项任务: 1.给定y,z,p,计算Y^Z Mod P 的值: 2.给定y,z,p,计算满足xy≡ ...
- 【bzoj2242】: [SDOI2011]计算器 数论-快速幂-扩展欧几里得-BSGS
[bzoj2242]: [SDOI2011]计算器 1.快速幂 2.扩展欧几里得(费马小定理) 3.BSGS /* http://www.cnblogs.com/karl07/ */ #include ...
- 洛谷 P2485 [SDOI2011]计算器 解题报告
P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...
- P2485 [SDOI2011]计算器
P2485 [SDOI2011]计算器 题目描述 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最 ...
随机推荐
- 关于DLL的创建与使用简单描述(C++、C#)
前言 前一段时间在学关于DLL的创建与调用,结果发现网络上一大堆别人分享的经验都有点问题.现在整理分享一下自己的方法. 工具 Microsoft Visual Studio 2017 depends ...
- 从0开始的LeetCode生活—461-Hamming Distance(汉明距离)
题目: The Hamming distance between two integers is the number of positions at which the corresponding ...
- 1013团队Beta冲刺day5
项目进展 李明皇 今天解决的进度 服务器端还未完善,所以无法进行联动调试.对页面样式和逻辑进行优化 明天安排 前后端联动调试 林翔 今天解决的进度 完成维护登录态,实现图片上传,微信开发工具上传图片不 ...
- iOS开发-即时通信XMPP
1. 即时通信 1> 概述 即时通讯(Instant Messaging)是目前Internet上最为流行的通讯方式,各种各样的即时通讯软件也层出不穷,服务提供商也提供了越来越丰富的通讯服务功能 ...
- iOS开发之UITextView,设置textViewplaceholder
一.设置textView的placeholder UITextView上如何加上类似于UITextField的placeholder呢,其实在UITextView上加上一个UILabel或者UITex ...
- JAVA_SE基础——19.数组的定义
数组是一组相关数据的集合,数组按照使用可以分为一维数组.二维数组.多维数组 本章先讲一维数组 不同点: 不使用数组定义100个整形变量:int1,int2,int3;;;;;; 使用数组定义 int ...
- Ubuntu Desktop 16.04 LTS 下成功配置Jupyter的两个python内核版本(2.7x,3.5x)
Ubuntu Desktop 16.04 LTS 安装好系统默认就有python两个不同版本(2.7.12和3.5.2) 现在来熟悉一下jupyter的对python这两个不同python版本的内核 ...
- 从一个事件绑定说起 - DOM
事件绑定的方式 给 DOM 元素绑定事件分为两大类:在 html 中直接绑定 和 在 JavaScript 中绑定. Bind in HTML 在 HTML 中绑定事件叫做内联绑定事件,HTML 的元 ...
- 基于python的统计公报关键数据爬取
# -*- coding: utf-8 -*- """ Created on Wed Nov 8 14:23:14 2017 @author: 123 "&qu ...
- [52ABP实战课程系列]Docker&Ubuntu从入门到实战开课啦~
任何的课程都逃不开理论的支持 久等了各位,在Asp.NET Core2.0 项目实战入门视频课程结束后,根据发起的投票信息.Docker 排在首位.按照结果,我们开始进行Docker视频课程的录制. ...